From 5345523eec6ce6f4d1b2a35d5e0ab23ac7268096 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 12 Feb 2024 02:50:23 -0500 Subject: [PATCH] feat: add create and update comment api's --- api/routes_rest_api.go | 79 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/api/routes_rest_api.go b/api/routes_rest_api.go index dba0523..c306fa3 100644 --- a/api/routes_rest_api.go +++ b/api/routes_rest_api.go @@ -164,6 +164,83 @@ 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) { + vars := mux.Vars(request) + owner := vars["owner"] + repo := vars["repo"] + issueNumber := vars["issue_number"] + + client := r.getClientOrError(writer) + + 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 + } + + issueNumberInt, err := strconv.Atoi(issueNumber) + if err != nil { + http.Error(writer, "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"), + } + + commentResponse, _, err := client.CreateIssueComment(owner, repo, int64(issueNumberInt), comment) + if err != nil { + http.Error(writer, "Failed to create comment", http.StatusInternalServerError) + r.logger.Error("Failed to create comment", zap.Error(err)) + return + } + + r.respond(writer, http.StatusCreated, commentResponse) +} + +func (r restApi) handlerUpdateIssueComment(w http.ResponseWriter, request *http.Request) { + vars := mux.Vars(request) + owner := vars["owner"] + repo := vars["repo"] + commentID := vars["comment_id"] + + client := r.getClientOrError(w) + + if client == nil { + 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 + } + + commentIDInt, err := strconv.Atoi(commentID) + if err != nil { + http.Error(w, "Failed to parse comment ID", http.StatusBadRequest) + r.logger.Error("Failed to parse comment ID", zap.Error(err)) + return + } + + comment := gitea.EditIssueCommentOption{ + Body: request.FormValue("body"), + } + + commentResponse, _, err := client.EditIssueComment(owner, repo, int64(commentIDInt), comment) + if err != nil { + http.Error(w, "Failed to update comment", http.StatusInternalServerError) + r.logger.Error("Failed to update comment", zap.Error(err)) + return + } + + r.respond(w, http.StatusOK, commentResponse) +} + func setupRestRoutes(params RouteParams) { logger := params.Logger cfg := params.Config @@ -174,6 +251,8 @@ func setupRestRoutes(params RouteParams) { setupRoutes := func(r *mux.Router) { r.HandleFunc("/repos/{owner}/{repo}/pulls/{pull_number}/files", restApi.handlerGetPullRequestFiles).Methods("GET") r.HandleFunc("/repos/{owner}/{repo}/git/trees/{tree_sha}", restApi.handlerGetTree).Methods("GET") + r.HandleFunc("/repos/{owner}/{repo}/issues/{issue_number}/comments", restApi.handlerCreateIssueComment).Methods("POST") + r.HandleFunc("/repos/{owner}/{repo}/issues/{issue_number}/comments/{comment_id}", restApi.handlerUpdateIssueComment).Methods("PATCH") } restRouter := r.PathPrefix("/api").Subrouter()