Skip to content

Commit

Permalink
feat: use keyset pagination for retrieving project CI jobs (#744)
Browse files Browse the repository at this point in the history
* build(deps): bump github.com/xanzy/go-gitlab from 0.92.3 to 0.94.0

Bumps [github.com/xanzy/go-gitlab](https://github.com/xanzy/go-gitlab) from 0.92.3 to 0.94.0.
- [Changelog](https://github.com/xanzy/go-gitlab/blob/main/releases_test.go)
- [Commits](xanzy/go-gitlab@v0.92.3...v0.94.0)

---
updated-dependencies:
- dependency-name: github.com/xanzy/go-gitlab
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* feat: use keyset pagination for retrieving project CI jobs

With xanzy/go-gitlab#1827, go-gitlab now
supports keyset pagination, which is much more efficient for iterating
through many pages of data.  This commit switches the GitLab CI
`/api/v4/projects/:id/jobs` API to use keyset pagination.

The other pipeline API endpoints need keyset pagination support:
https://gitlab.com/gitlab-org/gitlab/-/issues/431632

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
stanhu and dependabot[bot] committed Nov 29, 2023
1 parent ec967df commit c4d9649
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 11 additions & 6 deletions pkg/gitlab/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,19 @@ func (c *Client) ListRefMostRecentJobs(ctx context.Context, ref schemas.Ref) (jo
resp *goGitlab.Response
)

options := &goGitlab.ListJobsOptions{
opt := &goGitlab.ListJobsOptions{
ListOptions: goGitlab.ListOptions{
Page: 1,
PerPage: 100,
Pagination: "keyset",
PerPage: 100,
},
}

options := []goGitlab.RequestOptionFunc{goGitlab.WithContext(ctx)}

for {
c.rateLimit(ctx)

foundJobs, resp, err = c.Jobs.ListProjectJobs(ref.Project.Name, options, goGitlab.WithContext(ctx))
foundJobs, resp, err = c.Jobs.ListProjectJobs(ref.Project.Name, opt, options...)
if err != nil {
return
}
Expand Down Expand Up @@ -272,7 +274,7 @@ func (c *Client) ListRefMostRecentJobs(ctx context.Context, ref schemas.Ref) (jo
}
}

if resp.CurrentPage >= resp.NextPage {
if resp.NextLink == "" {
var notFoundJobs []string

for k := range jobsToRefresh {
Expand All @@ -293,7 +295,10 @@ func (c *Client) ListRefMostRecentJobs(ctx context.Context, ref schemas.Ref) (jo
break
}

options.Page = resp.NextPage
options = []goGitlab.RequestOptionFunc{
goGitlab.WithContext(ctx),
goGitlab.WithKeysetPaginationParameters(resp.NextLink),
}
}

return
Expand Down
4 changes: 2 additions & 2 deletions pkg/gitlab/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ func TestListRefMostRecentJobs(t *testing.T) {
func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
expectedQueryParams := url.Values{
"page": []string{"1"},
"per_page": []string{"100"},
"pagination": []string{"keyset"},
"per_page": []string{"100"},
}
assert.Equal(t, expectedQueryParams, r.URL.Query())
fmt.Fprint(w, `[{"id":3,"name":"foo","ref":"yay"},{"id":4,"name":"bar","ref":"yay"}]`)
Expand Down

0 comments on commit c4d9649

Please sign in to comment.