feat: add list issue comments
This commit is contained in:
parent
dc9a0acb8e
commit
6ca3e8945c
|
@ -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 := ""
|
||||
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue