Skip to content

Commit 39338ae

Browse files
committed
fix: break discovery loop if unexpected number of pages
1 parent d6addbb commit 39338ae

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

providers/github/resources/github.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,4 @@ func githubTimestamp(ts *github.Timestamp) *time.Time {
7777

7878
const (
7979
paginationPerPage = 100
80-
workers = 10
8180
)

providers/github/resources/github_org.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,15 @@ func (g *mqlGithubOrganization) repositories() ([]any, error) {
315315
}
316316

317317
repoCount := g.TotalPrivateRepos.Data + g.TotalPublicRepos.Data
318-
workerPool := workerpool.New[[]*github.Repository](workers)
318+
expectedPages := int(repoCount)/paginationPerPage + 1
319+
workerCount := int(repoCount)/paginationPerPage + 1
320+
workerPool := workerpool.New[[]*github.Repository](workerCount)
319321
workerPool.Start()
320322
defer workerPool.Close()
321323

322324
log.Debug().
323-
Int("workers", workers).
324-
Int64("total_repos", repoCount).
325+
Int("workers", workerCount).
326+
Int64("total-repos", repoCount).
325327
Str("organization", g.Name.Data).
326328
Msg("list repositories")
327329

@@ -332,6 +334,19 @@ func (g *mqlGithubOrganization) repositories() ([]any, error) {
332334
break
333335
}
334336

337+
// failsafe: when total count is correct but some repos aren't returned from ListByOrg
338+
// (e.g., due to permission issues), we stop after enough pages have been requested
339+
// plus the number of pending workers to account for concurrency
340+
if listOpts.Page > (int(workerPool.PendingRequests()) + expectedPages) {
341+
log.Warn().
342+
Int("found-repos", reposLen).
343+
Int64("total-repos", repoCount).
344+
Int("page", listOpts.Page).
345+
Int("per-page", listOpts.PerPage).
346+
Msg("Failsafe triggered, no more repos are returned")
347+
break
348+
}
349+
335350
// send requests to workers
336351
opts := listOpts
337352
workerPool.Submit(func() ([]*github.Repository, error) {

0 commit comments

Comments
 (0)