Skip to content

Commit e313828

Browse files
committed
paginate to get all open pull requests
1 parent de29db1 commit e313828

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

internal/cmd/root.go

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,10 @@ func processRepository(ctx context.Context, client *api.RESTClient, graphQlClien
235235
// Continue processing
236236
}
237237

238-
// Get open PRs for the repository
239-
var pulls github.Pulls
240-
241-
if err := client.Get(repo.PullsEndpoint(), &pulls); err != nil {
242-
return err
238+
// Fetch all open pull requests for the repository
239+
pulls, err := fetchOpenPullRequests(ctx, client, repo)
240+
if err != nil {
241+
return fmt.Errorf("failed to fetch open pull requests: %w", err)
243242
}
244243

245244
// Check for cancellation again
@@ -253,9 +252,7 @@ func processRepository(ctx context.Context, client *api.RESTClient, graphQlClien
253252
// Filter PRs based on criteria
254253
var matchedPRs github.Pulls
255254
for _, pull := range pulls {
256-
// Temporary workaround because passing structures is useless in this
257-
// context.
258-
// Eventually the []Labels should have better support.
255+
// Extract labels
259256
labels := []string{}
260257
for _, label := range pull.Labels {
261258
labels = append(labels, label.Name)
@@ -290,10 +287,49 @@ func processRepository(ctx context.Context, client *api.RESTClient, graphQlClien
290287
Logger.Debug("Matched PRs", "repo", repo, "count", len(matchedPRs))
291288

292289
// Combine the PRs
293-
err := CombinePRs(ctx, graphQlClient, client, repo, matchedPRs)
290+
err = CombinePRs(ctx, graphQlClient, client, repo, matchedPRs)
294291
if err != nil {
295292
return fmt.Errorf("failed to combine PRs: %w", err)
296293
}
297294

298295
return nil
299296
}
297+
298+
// fetchOpenPullRequests fetches all open pull requests for a repository, handling pagination
299+
func fetchOpenPullRequests(ctx context.Context, client *api.RESTClient, repo github.Repo) (github.Pulls, error) {
300+
var allPulls github.Pulls
301+
page := 1
302+
303+
for {
304+
// Check for cancellation
305+
select {
306+
case <-ctx.Done():
307+
return nil, ctx.Err()
308+
default:
309+
// Continue processing
310+
}
311+
312+
var pulls github.Pulls
313+
endpoint := fmt.Sprintf("%s?state=open&page=%d&per_page=100", repo.PullsEndpoint(), page)
314+
if err := client.Get(endpoint, &pulls); err != nil {
315+
return nil, fmt.Errorf("failed to fetch pull requests from page %d: %w", page, err)
316+
}
317+
318+
// If the current page is empty, we've reached the end
319+
if len(pulls) == 0 {
320+
break
321+
}
322+
323+
// Append fetched pulls to the result
324+
allPulls = append(allPulls, pulls...)
325+
326+
// If fewer than 100 PRs are returned, we've reached the last page
327+
if len(pulls) < 100 {
328+
break
329+
}
330+
331+
page++
332+
}
333+
334+
return allPulls, nil
335+
}

0 commit comments

Comments
 (0)