Skip to content

Commit 5b312fd

Browse files
authored
Merge pull request #11 from github/pr-pagination-fixes
Pull Request pagination fixes
2 parents b4c97d6 + 9c87693 commit 5b312fd

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

internal/cmd/match_criteria.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,28 @@ func PrMatchesCriteria(branch string, prLabels []string) bool {
2828

2929
// checks if a branch matches the branch filtering criteria
3030
func branchMatchesCriteria(branch string) bool {
31+
Logger.Debug("Checking branch criteria", "branch", branch)
3132
// Do not attempt to match on existing branches that were created by this CLI
3233
if branch == combineBranchName {
34+
Logger.Debug("Branch is a combine branch, skipping match")
3335
return false
3436
}
3537

3638
// If no branch filters are specified, all branches pass this check
3739
if branchPrefix == "" && branchSuffix == "" && branchRegex == "" {
40+
Logger.Debug("No branch filters specified, passing match")
3841
return true
3942
}
4043

4144
// Apply branch prefix filter if specified
4245
if branchPrefix != "" && !strings.HasPrefix(branch, branchPrefix) {
46+
Logger.Debug("Branch does not match prefix", "prefix", branchPrefix, "branch", branch)
4347
return false
4448
}
4549

4650
// Apply branch suffix filter if specified
4751
if branchSuffix != "" && !strings.HasSuffix(branch, branchSuffix) {
52+
Logger.Debug("Branch does not match suffix", "suffix", branchSuffix, "branch", branch)
4853
return false
4954
}
5055

@@ -57,10 +62,12 @@ func branchMatchesCriteria(branch string) bool {
5762
}
5863

5964
if !regex.MatchString(branch) {
65+
Logger.Debug("Branch does not match regex", "regex", branchRegex, "branch", branch)
6066
return false
6167
}
6268
}
6369

70+
Logger.Debug("Branch matches all branch criteria", "branch", branch)
6471
return true
6572
}
6673

internal/cmd/root.go

Lines changed: 47 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,51 @@ 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

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

0 commit comments

Comments
 (0)