From 6ca3e8945c46c02f30af65bcd6e6a8c8dd502f59 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 12 Feb 2024 03:49:51 -0500 Subject: [PATCH] feat: add list issue comments --- api/convert.go | 40 ++++++++++++++++++++++++++++++++++++++++ api/routes_rest_api.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/api/convert.go b/api/convert.go index 18fce69..cb2c748 100644 --- a/api/convert.go +++ b/api/convert.go @@ -349,6 +349,46 @@ func convertGitEntry(s *gitea.GitEntry) *github.TreeEntry { } } +func convertIssueComment(comment *gitea.Comment, reactions []*gitea.Reaction) *github.IssueComment { + if comment == nil { + return &github.IssueComment{} + } + return &github.IssueComment{ + ID: int64Ptr(comment.ID), + Body: stringPtr(comment.Body), + CreatedAt: timePtr(comment.Created), + UpdatedAt: timePtr(comment.Updated), + User: convertUser(comment.Poster), + Reactions: convertReactions(reactions), + } +} + +func convertReactions(reactions []*gitea.Reaction) *github.Reactions { + if reactions == nil { + return &github.Reactions{} + } + return &github.Reactions{ + TotalCount: intPtr(len(reactions)), + PlusOne: intPtr(countReaction(reactions, "+1")), + MinusOne: intPtr(countReaction(reactions, "-1")), + Laugh: intPtr(countReaction(reactions, "laugh")), + Confused: intPtr(countReaction(reactions, "confused")), + Heart: intPtr(countReaction(reactions, "heart")), + Rocket: intPtr(countReaction(reactions, "rocket")), + Eyes: intPtr(countReaction(reactions, "eyes")), + } +} + +func countReaction(reactions []*gitea.Reaction, reactionType string) int { + count := 0 + for _, reaction := range reactions { + if reaction.Reaction == reactionType { + count++ + } + } + return count +} + func translatePrAction(action structs.HookIssueAction) string { translatedAction := "" diff --git a/api/routes_rest_api.go b/api/routes_rest_api.go index 8ac0774..9f66163 100644 --- a/api/routes_rest_api.go +++ b/api/routes_rest_api.go @@ -164,6 +164,44 @@ func (r restApi) handlerGetTree(w http.ResponseWriter, request *http.Request) { r.respond(w, http.StatusOK, treeResponse) } +func (r restApi) handleeGetIssueComments(w http.ResponseWriter, request *http.Request) { + vars := mux.Vars(request) + owner := vars["owner"] + repo := vars["repo"] + issueNumber := vars["issue_number"] + + client := r.getClientOrError(w) + + if client == nil { + return + } + + issueNumberInt, err := strconv.Atoi(issueNumber) + if err != nil { + http.Error(w, "Failed to parse issue number", http.StatusBadRequest) + r.logger.Error("Failed to parse issue number", zap.Error(err)) + return + } + + comments, r2, err := client.ListIssueComments(owner, repo, int64(issueNumberInt), gitea.ListIssueCommentOptions{ListOptions: r.getPagingOptions(request)}) + if err != nil { + http.Error(w, "Failed to get issue comments", http.StatusInternalServerError) + r.logger.Error("Failed to get issue comments", zap.Error(err)) + return + } + + ghComments := make([]*github.IssueComment, len(comments)) + + for i, comment := range comments { + reactions, _, _ := client.GetIssueCommentReactions(owner, repo, comment.ID) + ghComments[i] = convertIssueComment(comment, reactions) + } + + r.sendPagingHeaders(w, r2) + r.respond(w, http.StatusOK, ghComments) + +} + func (r restApi) handlerCreateIssueComment(w http.ResponseWriter, request *http.Request) { vars := mux.Vars(request) owner := vars["owner"] @@ -262,6 +300,9 @@ 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") + + // Comment routes + r.HandleFunc("/repos/{owner}/{repo}/issues/{issue_number}/comments", restApi.handleeGetIssueComments).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") }