Skip to content

Commit

Permalink
Add gitlab pagination for diff listing
Browse files Browse the repository at this point in the history
In #1800,
two other places in the code were identified that performed list
requests to gitlab.

This change updates one of those two locations to use pagination.

The other place (acl.go) already had pagination introduced a while ago
in commit 12ba5c5.

Signed-off-by: Ralph Bean <[email protected]>
  • Loading branch information
ralphbean authored and chmouel committed Nov 5, 2024
1 parent 29db090 commit eed98d8
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions pkg/provider/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,25 +354,47 @@ func (v *Provider) GetFiles(_ context.Context, runevent *info.Event) (changedfil
"exiting... (hint: did you forget setting a secret on your repo?)")
}
if runevent.TriggerTarget == triggertype.PullRequest {
mrchanges, _, err := v.Client.MergeRequests.ListMergeRequestDiffs(v.targetProjectID, runevent.PullRequestNumber, &gitlab.ListMergeRequestDiffsOptions{})
if err != nil {
return changedfiles.ChangedFiles{}, err
opt := &gitlab.ListMergeRequestDiffsOptions{
ListOptions: gitlab.ListOptions{
OrderBy: "id",
Pagination: "keyset",
PerPage: 20,
Sort: "asc",
},
}

options := []gitlab.RequestOptionFunc{}
changedFiles := changedfiles.ChangedFiles{}
for _, change := range mrchanges {
changedFiles.All = append(changedFiles.All, change.NewPath)
if change.NewFile {
changedFiles.Added = append(changedFiles.Added, change.NewPath)

for {
mrchanges, resp, err := v.Client.MergeRequests.ListMergeRequestDiffs(v.targetProjectID, runevent.PullRequestNumber, opt, options...)
if err != nil {
return changedfiles.ChangedFiles{}, err
}
if change.DeletedFile {
changedFiles.Deleted = append(changedFiles.Deleted, change.NewPath)

for _, change := range mrchanges {
changedFiles.All = append(changedFiles.All, change.NewPath)
if change.NewFile {
changedFiles.Added = append(changedFiles.Added, change.NewPath)
}
if change.DeletedFile {
changedFiles.Deleted = append(changedFiles.Deleted, change.NewPath)
}
if !change.RenamedFile && !change.DeletedFile && !change.NewFile {
changedFiles.Modified = append(changedFiles.Modified, change.NewPath)
}
if change.RenamedFile {
changedFiles.Renamed = append(changedFiles.Renamed, change.NewPath)
}
}
if !change.RenamedFile && !change.DeletedFile && !change.NewFile {
changedFiles.Modified = append(changedFiles.Modified, change.NewPath)

// Exit the loop when we've seen all pages.
if resp.NextLink == "" {
break
}
if change.RenamedFile {
changedFiles.Renamed = append(changedFiles.Renamed, change.NewPath)

// Otherwise, set param to query the next page
options = []gitlab.RequestOptionFunc{
gitlab.WithKeysetPaginationParameters(resp.NextLink),
}
}
return changedFiles, nil
Expand Down

0 comments on commit eed98d8

Please sign in to comment.