fix: add parseJsonBody helper and update comment methods to parse json

This commit is contained in:
Derrick Hammer 2024-02-12 03:15:23 -05:00
parent 5345523eec
commit dc9a0acb8e
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 25 additions and 14 deletions

View File

@ -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