From dc9a0acb8e2161fc9ca35c3b6e874ed48f469e46 Mon Sep 17 00:00:00 2001
From: Derrick Hammer <derrick@derrickhammer.com>
Date: Mon, 12 Feb 2024 03:15:23 -0500
Subject: [PATCH] fix: add parseJsonBody helper and update comment methods to
 parse json

---
 api/routes_rest_api.go | 39 +++++++++++++++++++++++++--------------
 1 file changed, 25 insertions(+), 14 deletions(-)

diff --git a/api/routes_rest_api.go b/api/routes_rest_api.go
index c306fa3..8ac0774 100644
--- a/api/routes_rest_api.go
+++ b/api/routes_rest_api.go
@@ -164,42 +164,43 @@ func (r restApi) handlerGetTree(w http.ResponseWriter, request *http.Request) {
 	r.respond(w, http.StatusOK, treeResponse)
 }
 
-func (r restApi) handlerCreateIssueComment(writer http.ResponseWriter, request *http.Request) {
+func (r restApi) handlerCreateIssueComment(w http.ResponseWriter, request *http.Request) {
 	vars := mux.Vars(request)
 	owner := vars["owner"]
 	repo := vars["repo"]
 	issueNumber := vars["issue_number"]
 
-	client := r.getClientOrError(writer)
+	client := r.getClientOrError(w)
 
 	if client == nil {
 		return
 	}
 
-	if err := request.ParseForm(); err != nil {
-		http.Error(writer, "Failed to parse form", http.StatusBadRequest)
-		r.logger.Error("Failed to parse form", zap.Error(err))
-		return
+	ghComment := github.IssueComment{}
+
+	if err := r.parseJsonBody(request, &ghComment); err != nil {
+		http.Error(w, "Failed to parse request body", http.StatusBadRequest)
+		r.logger.Error("Failed to parse request body", zap.Error(err))
 	}
 
 	issueNumberInt, err := strconv.Atoi(issueNumber)
 	if err != nil {
-		http.Error(writer, "Failed to parse issue number", http.StatusBadRequest)
+		http.Error(w, "Failed to parse issue number", http.StatusBadRequest)
 		r.logger.Error("Failed to parse issue number", zap.Error(err))
 		return
 	}
 	comment := gitea.CreateIssueCommentOption{
-		Body: request.FormValue("body"),
+		Body: ghComment.GetBody(),
 	}
 
 	commentResponse, _, err := client.CreateIssueComment(owner, repo, int64(issueNumberInt), comment)
 	if err != nil {
-		http.Error(writer, "Failed to create comment", http.StatusInternalServerError)
+		http.Error(w, "Failed to create comment", http.StatusInternalServerError)
 		r.logger.Error("Failed to create comment", zap.Error(err))
 		return
 	}
 
-	r.respond(writer, http.StatusCreated, commentResponse)
+	r.respond(w, http.StatusCreated, commentResponse)
 }
 
 func (r restApi) handlerUpdateIssueComment(w http.ResponseWriter, request *http.Request) {
@@ -214,10 +215,11 @@ func (r restApi) handlerUpdateIssueComment(w http.ResponseWriter, request *http.
 		return
 	}
 
-	if err := request.ParseForm(); err != nil {
-		http.Error(w, "Failed to parse form", http.StatusBadRequest)
-		r.logger.Error("Failed to parse form", zap.Error(err))
-		return
+	ghComment := github.IssueComment{}
+
+	if err := r.parseJsonBody(request, &ghComment); err != nil {
+		http.Error(w, "Failed to parse request body", http.StatusBadRequest)
+		r.logger.Error("Failed to parse request body", zap.Error(err))
 	}
 
 	commentIDInt, err := strconv.Atoi(commentID)
@@ -241,6 +243,15 @@ func (r restApi) handlerUpdateIssueComment(w http.ResponseWriter, request *http.
 	r.respond(w, http.StatusOK, commentResponse)
 }
 
+func (r restApi) parseJsonBody(request *http.Request, obj interface{}) error {
+	if obj == nil {
+		obj = make(map[string]interface{})
+	}
+
+	decoder := json.NewDecoder(request.Body)
+	return decoder.Decode(obj)
+}
+
 func setupRestRoutes(params RouteParams) {
 	logger := params.Logger
 	cfg := params.Config