Skip to content

Commit

Permalink
feat: use keyset pagination for retrieving project CI jobs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
stanhu committed Nov 13, 2023
1 parent 66bcb68 commit 7ac3e79
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 7ac3e79

Please sign in to comment.