Feat: Notes (Non-Linked Comments) (#52)
Adds support for notes. These are comments that are not linked to specific lines of code in the MR.
This commit is contained in:
committed by
GitHub
parent
d92cf39dd7
commit
152c55fd57
@@ -36,7 +36,8 @@ type EditCommentRequest struct {
|
||||
|
||||
type CommentResponse struct {
|
||||
SuccessResponse
|
||||
Comment *gitlab.Note `json:"note"`
|
||||
Comment *gitlab.Note `json:"note"`
|
||||
Discussion *gitlab.Discussion `json:"discussion"`
|
||||
}
|
||||
|
||||
func CommentHandler(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -107,24 +108,26 @@ func PostComment(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
position := &gitlab.NotePosition{
|
||||
PositionType: "text",
|
||||
StartSHA: postCommentRequest.StartCommitSHA,
|
||||
HeadSHA: postCommentRequest.HeadCommitSHA,
|
||||
BaseSHA: postCommentRequest.BaseCommitSHA,
|
||||
NewPath: postCommentRequest.FileName,
|
||||
OldPath: postCommentRequest.FileName,
|
||||
NewLine: postCommentRequest.NewLine,
|
||||
OldLine: postCommentRequest.OldLine,
|
||||
opt := gitlab.CreateMergeRequestDiscussionOptions{
|
||||
Body: &postCommentRequest.Comment,
|
||||
}
|
||||
|
||||
discussion, _, err := c.git.Discussions.CreateMergeRequestDiscussion(
|
||||
c.projectId,
|
||||
c.mergeId,
|
||||
&gitlab.CreateMergeRequestDiscussionOptions{
|
||||
Body: &postCommentRequest.Comment,
|
||||
Position: position,
|
||||
})
|
||||
/* If we are leaving a comment on a line, leave position. Otherwise,
|
||||
we are leaving a note (unlinked comment) */
|
||||
if postCommentRequest.FileName != "" {
|
||||
opt.Position = &gitlab.NotePosition{
|
||||
PositionType: "text",
|
||||
StartSHA: postCommentRequest.StartCommitSHA,
|
||||
HeadSHA: postCommentRequest.HeadCommitSHA,
|
||||
BaseSHA: postCommentRequest.BaseCommitSHA,
|
||||
NewPath: postCommentRequest.FileName,
|
||||
OldPath: postCommentRequest.FileName,
|
||||
NewLine: postCommentRequest.NewLine,
|
||||
OldLine: postCommentRequest.OldLine,
|
||||
}
|
||||
}
|
||||
|
||||
discussion, _, err := c.git.Discussions.CreateMergeRequestDiscussion(c.projectId, c.mergeId, &opt)
|
||||
|
||||
if err != nil {
|
||||
c.handleError(w, err, "Could not create comment", http.StatusBadRequest)
|
||||
@@ -136,7 +139,8 @@ func PostComment(w http.ResponseWriter, r *http.Request) {
|
||||
Message: "Comment updated succesfully",
|
||||
Status: http.StatusOK,
|
||||
},
|
||||
Comment: discussion.Notes[0],
|
||||
Comment: discussion.Notes[0],
|
||||
Discussion: discussion,
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(response)
|
||||
|
||||
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"sort"
|
||||
|
||||
@@ -11,13 +12,18 @@ import (
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
type SortableDiscussions []*gitlab.Discussion
|
||||
type DiscussionsRequest struct {
|
||||
Blacklist []string `json:"blacklist"`
|
||||
}
|
||||
|
||||
type DiscussionsResponse struct {
|
||||
SuccessResponse
|
||||
Discussions []*gitlab.Discussion `json:"discussions"`
|
||||
Discussions []*gitlab.Discussion `json:"discussions"`
|
||||
UnlinkedDiscussions []*gitlab.Discussion `json:"unlinked_discussions"`
|
||||
}
|
||||
|
||||
type SortableDiscussions []*gitlab.Discussion
|
||||
|
||||
func (n SortableDiscussions) Len() int {
|
||||
return len(n)
|
||||
}
|
||||
@@ -26,14 +32,13 @@ func (d SortableDiscussions) Less(i int, j int) bool {
|
||||
iTime := d[i].Notes[len(d[i].Notes)-1].CreatedAt
|
||||
jTime := d[j].Notes[len(d[j].Notes)-1].CreatedAt
|
||||
return iTime.After(*jTime)
|
||||
|
||||
}
|
||||
|
||||
func (n SortableDiscussions) Swap(i, j int) {
|
||||
n[i], n[j] = n[j], n[i]
|
||||
}
|
||||
|
||||
func (c *Client) ListDiscussions() ([]*gitlab.Discussion, int, error) {
|
||||
func (c *Client) ListDiscussions(blacklist []string) ([]*gitlab.Discussion, []*gitlab.Discussion, int, error) {
|
||||
|
||||
mergeRequestDiscussionOptions := gitlab.ListMergeRequestDiscussionsOptions{
|
||||
Page: 1,
|
||||
@@ -42,36 +47,59 @@ func (c *Client) ListDiscussions() ([]*gitlab.Discussion, int, error) {
|
||||
discussions, res, err := c.git.Discussions.ListMergeRequestDiscussions(c.projectId, c.mergeId, &mergeRequestDiscussionOptions, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, res.Response.StatusCode, fmt.Errorf("Listing discussions failed: %w", err)
|
||||
return nil, nil, res.Response.StatusCode, fmt.Errorf("Listing discussions failed: %w", err)
|
||||
}
|
||||
|
||||
var realDiscussions []*gitlab.Discussion
|
||||
for i := 0; i < len(discussions); i++ {
|
||||
notes := discussions[i].Notes
|
||||
for j := 0; j < len(notes); j++ {
|
||||
if notes[j].Type == gitlab.NoteTypeValue("DiffNote") {
|
||||
realDiscussions = append(realDiscussions, discussions[i])
|
||||
/* Filter out any discussions started by a blacklisted user
|
||||
and system discussions, then return them sorted by created date */
|
||||
var unlinkedDiscussions []*gitlab.Discussion
|
||||
var linkedDiscussions []*gitlab.Discussion
|
||||
for _, discussion := range discussions {
|
||||
if Contains(blacklist, discussion.Notes[0].Author.Username) > -1 {
|
||||
continue
|
||||
}
|
||||
for _, note := range discussion.Notes {
|
||||
if note.Type == gitlab.NoteTypeValue("DiffNote") {
|
||||
linkedDiscussions = append(linkedDiscussions, discussion)
|
||||
break
|
||||
} else if note.System == false && note.Position == nil {
|
||||
unlinkedDiscussions = append(unlinkedDiscussions, discussion)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sortedDiscussions := SortableDiscussions(realDiscussions)
|
||||
sort.Sort(sortedDiscussions)
|
||||
sortedLinkedDiscussions := SortableDiscussions(linkedDiscussions)
|
||||
sortedUnlinkedDiscussions := SortableDiscussions(unlinkedDiscussions)
|
||||
|
||||
return sortedDiscussions, http.StatusOK, nil
|
||||
sort.Sort(sortedLinkedDiscussions)
|
||||
sort.Sort(sortedUnlinkedDiscussions)
|
||||
|
||||
return sortedLinkedDiscussions, sortedUnlinkedDiscussions, http.StatusOK, nil
|
||||
}
|
||||
|
||||
func ListDiscussionsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
c := r.Context().Value("client").(Client)
|
||||
|
||||
if r.Method != http.MethodGet {
|
||||
if r.Method != http.MethodPost {
|
||||
c.handleError(w, errors.New("Invalid request type"), "That request type is not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
msg, status, err := c.ListDiscussions()
|
||||
body, err := io.ReadAll(r.Body)
|
||||
|
||||
if err != nil {
|
||||
c.handleError(w, err, "Could not read request body", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
var requestBody DiscussionsRequest
|
||||
err = json.Unmarshal(body, &requestBody)
|
||||
if err != nil {
|
||||
c.handleError(w, err, "Could not unmarshal request body", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
linkedDiscussions, unlinkedDiscussions, status, err := c.ListDiscussions(requestBody.Blacklist)
|
||||
|
||||
if err != nil {
|
||||
c.handleError(w, err, "Could not list discussions", http.StatusBadRequest)
|
||||
@@ -85,7 +113,8 @@ func ListDiscussionsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
Message: "Discussions successfully fetched.",
|
||||
Status: http.StatusOK,
|
||||
},
|
||||
Discussions: msg,
|
||||
Discussions: linkedDiscussions,
|
||||
UnlinkedDiscussions: unlinkedDiscussions,
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(response)
|
||||
|
||||
10
cmd/utils.go
Normal file
10
cmd/utils.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
func Contains[T comparable](elems []T, v T) int {
|
||||
for i, s := range elems {
|
||||
if v == s {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
Reference in New Issue
Block a user