Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: comment list pagination #21

Merged
merged 8 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ jobs:
with:
go-version-file: "go.mod"

- uses: aquaproj/aqua-installer@v2.1.2
- uses: aquaproj/aqua-installer@v3.0.0
with:
aqua_version: v2.10.1
aqua_version: v2.27.0
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


- uses: suzuki-shunsuke/github-action-golangci-lint@v0.1.4
- name: Test
Expand Down
42 changes: 37 additions & 5 deletions pkg/notifier/gitlab/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
gitlab "github.com/xanzy/go-gitlab"
)

const (
listPerPage = 100
maxPages = 100
)

// CommentService handles communication with the comment related
// methods of GitLab API
type CommentService service
Expand Down Expand Up @@ -59,9 +64,36 @@ func (g *CommentService) Patch(note int, body string, opt PostOptions) error {

// List lists comments on GitLab merge requests
func (g *CommentService) List(number int) ([]*gitlab.Note, error) {
comments, _, err := g.client.API.ListMergeRequestNotes(
number,
&gitlab.ListMergeRequestNotesOptions{},
)
return comments, err
allComments := make([]*gitlab.Note, 0)

opt := &gitlab.ListMergeRequestNotesOptions{
ListOptions: gitlab.ListOptions{
Page: 1,
PerPage: listPerPage,
},
}

for sentinel := 1; ; sentinel++ {
comments, resp, err := g.client.API.ListMergeRequestNotes(
number,
opt,
)
if err != nil {
return nil, err
}

allComments = append(allComments, comments...)

if resp.NextPage == 0 {
break
}

opt.Page = resp.NextPage

if sentinel > maxPages {
return nil, fmt.Errorf("gitlab.comment.list: too many pages, something went wrong")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not necessary to return error. It is better to print log.

}
}

return allComments, nil
}
10 changes: 10 additions & 0 deletions pkg/notifier/gitlab/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ func TestCommentPost(t *testing.T) {
func TestCommentList(t *testing.T) {
t.Parallel()
comments := []*gitlab.Note{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you this variables as inline in testcase?

// page1
{
ID: 371748792,
Body: "comment 1",
},
{
ID: 371765743,
Body: "comment 2",
},
// page2
{
ID: 371748792,
Body: "comment 1",
Expand Down
12 changes: 11 additions & 1 deletion pkg/notifier/gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func newFakeAPI() fakeAPI {
FakeListMergeRequestNotes: func(mergeRequest int, opt *gitlab.ListMergeRequestNotesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Note, *gitlab.Response, error) {
var comments []*gitlab.Note
comments = []*gitlab.Note{
// same response to any page for now
{
ID: 371748792,
Body: "comment 1",
Expand All @@ -54,7 +55,16 @@ func newFakeAPI() fakeAPI {
Body: "comment 2",
},
}
return comments, nil, nil

// fake pagination with 2 pages
resp := &gitlab.Response{
NextPage: 0,
}
if opt.Page == 1 {
resp.NextPage = 2
}

return comments, resp, nil
},
FakePostCommitComment: func(sha string, opt *gitlab.PostCommitCommentOptions, options ...gitlab.RequestOptionFunc) (*gitlab.CommitComment, *gitlab.Response, error) {
return &gitlab.CommitComment{
Expand Down
Loading