Skip to content

Commit

Permalink
fix: comment to associated MR when push pipeline (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitagry authored Nov 6, 2023
1 parent 3dc1f21 commit 8ca8d39
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 17 deletions.
38 changes: 25 additions & 13 deletions pkg/notifier/gitlab/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,33 @@ type PostOptions struct {

// Post posts comment
func (g *CommentService) Post(body string, opt PostOptions) error {
if opt.Number != 0 {
_, _, err := g.client.API.CreateMergeRequestNote(
opt.Number,
&gitlab.CreateMergeRequestNoteOptions{Body: gitlab.String(body)},
)
return err
if opt.Number == 0 && opt.Revision == "" {
return fmt.Errorf("gitlab.comment.post: Number or Revision is required")
}
if opt.Revision != "" {
_, _, err := g.client.API.PostCommitComment(
opt.Revision,
&gitlab.PostCommitCommentOptions{Note: gitlab.String(body)},
)
return err

if opt.Number == 0 {
mrs, err := g.client.Commits.ListMergeRequestIIDsByRevision(opt.Revision)
if err != nil || len(mrs) == 0 {
return g.postForRevision(body, opt.Revision)
}

// Rewrite the MR number to the first MR which is associated with revision.
opt.Number = mrs[0]
}
return fmt.Errorf("gitlab.comment.post: Number or Revision is required")

_, _, err := g.client.API.CreateMergeRequestNote(
opt.Number,
&gitlab.CreateMergeRequestNoteOptions{Body: gitlab.String(body)},
)
return err
}

func (g *CommentService) postForRevision(body, revision string) error {
_, _, err := g.client.API.PostCommitComment(
revision,
&gitlab.PostCommitCommentOptions{Note: gitlab.String(body)},
)
return err
}

// Patch patches on the specific comment
Expand Down
16 changes: 16 additions & 0 deletions pkg/notifier/gitlab/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ func (g *CommitsService) List(revision string) ([]string, error) {
return s, nil
}

func (g *CommitsService) ListMergeRequestIIDsByRevision(revision string) ([]int, error) {
if revision == "" {
return nil, errors.New("no revision specified")
}
mrs, _, err := g.client.API.ListMergeRequestsByCommit(revision)
if err != nil {
return nil, err
}

result := make([]int, len(mrs))
for i, mr := range mrs {
result[i] = mr.IID
}
return result, nil
}

// lastOne returns the hash of the previous commit of the given commit
func (g *CommitsService) lastOne(commits []string, revision string) (string, error) {
if revision == "" {
Expand Down
5 changes: 5 additions & 0 deletions pkg/notifier/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type API interface {
GetLabel(labelName string, options ...gitlab.RequestOptionFunc) (*gitlab.Label, *gitlab.Response, error)
UpdateLabel(opt *gitlab.UpdateLabelOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Label, *gitlab.Response, error)
GetCommit(sha string, options ...gitlab.RequestOptionFunc) (*gitlab.Commit, *gitlab.Response, error)
ListMergeRequestsByCommit(sha string, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequest, *gitlab.Response, error)
}

// GitLab represents the attribute information necessary for requesting GitLab API
Expand Down Expand Up @@ -115,3 +116,7 @@ func (g *GitLab) UpdateLabel(opt *gitlab.UpdateLabelOptions, options ...gitlab.R
func (g *GitLab) GetCommit(sha string, options ...gitlab.RequestOptionFunc) (*gitlab.Commit, *gitlab.Response, error) {
return g.Client.Commits.GetCommit(fmt.Sprintf("%s/%s", g.namespace, g.project), sha, options...)
}

func (g *GitLab) ListMergeRequestsByCommit(sha string, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequest, *gitlab.Response, error) {
return g.Client.Commits.ListMergeRequestsByCommit(fmt.Sprintf("%s/%s", g.namespace, g.project), sha, options...)
}
20 changes: 16 additions & 4 deletions pkg/notifier/gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (

type fakeAPI struct {
API
FakeCreateMergeRequestNote func(mergeRequest int, opt *gitlab.CreateMergeRequestNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error)
FakeListMergeRequestNotes func(mergeRequest int, opt *gitlab.ListMergeRequestNotesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Note, *gitlab.Response, error)
FakePostCommitComment func(sha string, opt *gitlab.PostCommitCommentOptions, options ...gitlab.RequestOptionFunc) (*gitlab.CommitComment, *gitlab.Response, error)
FakeListCommits func(opt *gitlab.ListCommitsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Commit, *gitlab.Response, error)
FakeCreateMergeRequestNote func(mergeRequest int, opt *gitlab.CreateMergeRequestNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error)
FakeListMergeRequestNotes func(mergeRequest int, opt *gitlab.ListMergeRequestNotesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Note, *gitlab.Response, error)
FakePostCommitComment func(sha string, opt *gitlab.PostCommitCommentOptions, options ...gitlab.RequestOptionFunc) (*gitlab.CommitComment, *gitlab.Response, error)
FakeListCommits func(opt *gitlab.ListCommitsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Commit, *gitlab.Response, error)
FakeListMergeRequestsByCommit func(sha string, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequest, *gitlab.Response, error)
}

func (g *fakeAPI) CreateMergeRequestNote(mergeRequest int, opt *gitlab.CreateMergeRequestNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
Expand All @@ -29,6 +30,10 @@ func (g *fakeAPI) ListCommits(opt *gitlab.ListCommitsOptions, options ...gitlab.
return g.FakeListCommits(opt, options...)
}

func (g *fakeAPI) ListMergeRequestsByCommit(sha string, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequest, *gitlab.Response, error) {
return g.FakeListMergeRequestsByCommit(sha, options...)
}

func newFakeAPI() fakeAPI {
return fakeAPI{
FakeCreateMergeRequestNote: func(mergeRequest int, opt *gitlab.CreateMergeRequestNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
Expand Down Expand Up @@ -71,6 +76,13 @@ func newFakeAPI() fakeAPI {
}
return commits, nil, nil
},
FakeListMergeRequestsByCommit: func(sha string, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequest, *gitlab.Response, error) {
return []*gitlab.MergeRequest{
{
IID: 1,
},
}, nil, nil
},
}
}

Expand Down

0 comments on commit 8ca8d39

Please sign in to comment.