Fix MR Selection, Go Code Refactor (#358)

refactor: Refactors the Go codebase into a more modular and idiomatic approach
fix: require selection of specific MR when there are multiple targets for a given source branch
feat: Allows for the passing of Gitlab's filter options when choosing an MR, improves MR selection
feat: API to choose an MR from a list based on the provided username's involvement as an assignee/reviewer/author

This is a #MINOR release
This commit is contained in:
Harrison (Harry) Cramer
2024-09-08 16:45:09 -04:00
committed by GitHub
parent 6500ef1f2c
commit ea2b2b2f5c
81 changed files with 2559 additions and 3035 deletions

124
cmd/app/comment_test.go Normal file
View File

@@ -0,0 +1,124 @@
package app
import (
"net/http"
"testing"
"github.com/xanzy/go-gitlab"
)
type fakeCommentClient struct {
testBase
}
func (f fakeCommentClient) CreateMergeRequestDiscussion(pid interface{}, mergeRequest int, opt *gitlab.CreateMergeRequestDiscussionOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error) {
resp, err := f.handleGitlabError()
if err != nil {
return nil, nil, err
}
return &gitlab.Discussion{Notes: []*gitlab.Note{{}}}, resp, err
}
func (f fakeCommentClient) UpdateMergeRequestDiscussionNote(pid interface{}, mergeRequest int, discussion string, note int, opt *gitlab.UpdateMergeRequestDiscussionNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
resp, err := f.handleGitlabError()
if err != nil {
return nil, nil, err
}
return &gitlab.Note{}, resp, err
}
func (f fakeCommentClient) DeleteMergeRequestDiscussionNote(pid interface{}, mergeRequest int, discussion string, note int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
resp, err := f.handleGitlabError()
if err != nil {
return nil, err
}
return resp, err
}
func TestPostComment(t *testing.T) {
var testCommentCreationData = PostCommentRequest{Comment: "Some comment"}
t.Run("Creates a new note (unlinked comment)", func(t *testing.T) {
request := makeRequest(t, http.MethodPost, "/mr/comment", testCommentCreationData)
svc := commentService{testProjectData, fakeCommentClient{}}
data := getSuccessData(t, svc, request)
assert(t, data.Message, "Comment created successfully")
assert(t, data.Status, http.StatusOK)
})
t.Run("Creates a new comment", func(t *testing.T) {
testCommentCreationData := PostCommentRequest{ // Re-create comment creation data to avoid mutating this variable in other tests
Comment: "Some comment",
PositionData: PositionData{
FileName: "file.txt",
},
}
request := makeRequest(t, http.MethodPost, "/mr/comment", testCommentCreationData)
svc := commentService{testProjectData, fakeCommentClient{}}
data := getSuccessData(t, svc, request)
assert(t, data.Message, "Comment created successfully")
assert(t, data.Status, http.StatusOK)
})
t.Run("Handles errors from Gitlab client", func(t *testing.T) {
request := makeRequest(t, http.MethodPost, "/mr/comment", testCommentCreationData)
svc := commentService{testProjectData, fakeCommentClient{testBase{errFromGitlab: true}}}
data := getFailData(t, svc, request)
checkErrorFromGitlab(t, data, "Could not create discussion")
})
t.Run("Handles non-200s from Gitlab client", func(t *testing.T) {
request := makeRequest(t, http.MethodPost, "/mr/comment", testCommentCreationData)
svc := commentService{testProjectData, fakeCommentClient{testBase{status: http.StatusSeeOther}}}
data := getFailData(t, svc, request)
checkNon200(t, data, "Could not create discussion", "/mr/comment")
})
}
func TestDeleteComment(t *testing.T) {
var testCommentDeletionData = DeleteCommentRequest{NoteId: 3, DiscussionId: "abc123"}
t.Run("Deletes a comment", func(t *testing.T) {
request := makeRequest(t, http.MethodDelete, "/mr/comment", testCommentDeletionData)
svc := commentService{testProjectData, fakeCommentClient{}}
data := getSuccessData(t, svc, request)
assert(t, data.Message, "Comment deleted successfully")
assert(t, data.Status, http.StatusOK)
})
t.Run("Handles errors from Gitlab client", func(t *testing.T) {
request := makeRequest(t, http.MethodDelete, "/mr/comment", testCommentDeletionData)
svc := commentService{testProjectData, fakeCommentClient{testBase{errFromGitlab: true}}}
data := getFailData(t, svc, request)
checkErrorFromGitlab(t, data, "Could not delete comment")
})
t.Run("Handles non-200s from Gitlab client", func(t *testing.T) {
request := makeRequest(t, http.MethodDelete, "/mr/comment", testCommentDeletionData)
svc := commentService{testProjectData, fakeCommentClient{testBase{status: http.StatusSeeOther}}}
data := getFailData(t, svc, request)
checkNon200(t, data, "Could not delete comment", "/mr/comment")
})
}
func TestEditComment(t *testing.T) {
var testEditCommentData = EditCommentRequest{Comment: "Some comment", NoteId: 3, DiscussionId: "abc123"}
t.Run("Edits a comment", func(t *testing.T) {
request := makeRequest(t, http.MethodPatch, "/mr/comment", testEditCommentData)
svc := commentService{testProjectData, fakeCommentClient{}}
data := getSuccessData(t, svc, request)
assert(t, data.Message, "Comment updated successfully")
assert(t, data.Status, http.StatusOK)
})
t.Run("Handles errors from Gitlab client", func(t *testing.T) {
request := makeRequest(t, http.MethodPatch, "/mr/comment", testEditCommentData)
svc := commentService{testProjectData, fakeCommentClient{testBase{errFromGitlab: true}}}
data := getFailData(t, svc, request)
checkErrorFromGitlab(t, data, "Could not update comment")
})
t.Run("Handles non-200s from Gitlab client", func(t *testing.T) {
request := makeRequest(t, http.MethodPatch, "/mr/comment", testEditCommentData)
svc := commentService{testProjectData, fakeCommentClient{testBase{status: http.StatusSeeOther}}}
data := getFailData(t, svc, request)
checkNon200(t, data, "Could not update comment", "/mr/comment")
})
}