feat: add create and update comment api's

This commit is contained in:
Derrick Hammer 2024-02-12 02:50:23 -05:00
parent f975cf38e0
commit 5345523eec
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 79 additions and 0 deletions

View File

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