-
Notifications
You must be signed in to change notification settings - Fork 55
fix(scan github): surface GitHub rate limits instead of silent success #1426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
John-David Dalton (jdalton)
wants to merge
1
commit into
v1.x
Choose a base branch
from
jdalton/ask-167-improve-cli-to-handle-github-api-rate-limiting-issues
base: v1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+786
−48
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| import { runGithubScanLoop } from './create-scan-from-github.mts' | ||
| import { | ||
| GITHUB_ERR_AUTH_FAILED, | ||
| GITHUB_ERR_RATE_LIMIT, | ||
| } from '../../utils/github-errors.mts' | ||
|
|
||
| import type { CResult } from '../../types.mts' | ||
|
|
||
| type ScanResult = CResult<{ scanCreated: boolean }> | ||
|
|
||
| // Build a scanRepoFn from a map of repo -> canned result, recording the | ||
| // order of calls so we can assert the loop stops early on blocking errors. | ||
| // No module mocking — this is a plain function injected into the loop. | ||
| function fakeScanner(results: Record<string, ScanResult>): { | ||
| fn: (repoSlug: string) => Promise<ScanResult> | ||
| calls: string[] | ||
| } { | ||
| const calls: string[] = [] | ||
| return { | ||
| calls, | ||
| fn: (repoSlug: string) => { | ||
| calls.push(repoSlug) | ||
| return Promise.resolve(results[repoSlug]!) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| const scanned: ScanResult = { ok: true, data: { scanCreated: true } } | ||
| const emptyRepo: ScanResult = { ok: true, data: { scanCreated: false } } | ||
| const noManifests: ScanResult = { | ||
| ok: false, | ||
| message: 'No manifest files found', | ||
| cause: 'No supported manifest files were found.', | ||
| } | ||
| const rateLimited: ScanResult = { | ||
| ok: false, | ||
| message: GITHUB_ERR_RATE_LIMIT, | ||
| cause: 'GitHub API rate limit exceeded on the supplied token.', | ||
| } | ||
| const authFailed: ScanResult = { | ||
| ok: false, | ||
| message: GITHUB_ERR_AUTH_FAILED, | ||
| cause: 'GitHub authentication failed.', | ||
| } | ||
|
|
||
| describe('runGithubScanLoop GitHub blocking-error handling', () => { | ||
| it('stops and returns ok:false on a GitHub rate limit', async () => { | ||
| const scanner = fakeScanner({ | ||
| 'repo-a': rateLimited, | ||
| 'repo-b': scanned, | ||
| 'repo-c': scanned, | ||
| }) | ||
| const result = await runGithubScanLoop( | ||
| ['repo-a', 'repo-b', 'repo-c'], | ||
| scanner.fn, | ||
| ) | ||
| expect(result.ok).toBe(false) | ||
| expect(result.ok ? '' : result.message).toBe(GITHUB_ERR_RATE_LIMIT) | ||
| // Loop stopped after the first repo — no quota burned on the rest. | ||
| expect(scanner.calls).toEqual(['repo-a']) | ||
| }) | ||
|
|
||
| it('stops and returns ok:false on a GitHub auth failure', async () => { | ||
| const scanner = fakeScanner({ 'repo-a': authFailed, 'repo-b': scanned }) | ||
| const result = await runGithubScanLoop(['repo-a', 'repo-b'], scanner.fn) | ||
| expect(result.ok).toBe(false) | ||
| expect(result.ok ? '' : result.message).toBe(GITHUB_ERR_AUTH_FAILED) | ||
| expect(scanner.calls).toEqual(['repo-a']) | ||
| }) | ||
|
|
||
| it('carries the rate-limit cause through so the CLI can print it', async () => { | ||
| const scanner = fakeScanner({ 'repo-a': rateLimited }) | ||
| const result = await runGithubScanLoop(['repo-a'], scanner.fn) | ||
| expect(result.ok ? undefined : result.cause).toContain('rate limit') | ||
| }) | ||
|
|
||
| it('succeeds when a repo has no manifests but the tree was empty', async () => { | ||
| const scanner = fakeScanner({ 'repo-a': emptyRepo, 'repo-b': emptyRepo }) | ||
| const result = await runGithubScanLoop(['repo-a', 'repo-b'], scanner.fn) | ||
| expect(result.ok).toBe(true) | ||
| expect(scanner.calls).toEqual(['repo-a', 'repo-b']) | ||
| }) | ||
|
|
||
| it('succeeds when at least one repo produced a scan', async () => { | ||
| const scanner = fakeScanner({ 'repo-a': noManifests, 'repo-b': scanned }) | ||
| const result = await runGithubScanLoop(['repo-a', 'repo-b'], scanner.fn) | ||
| expect(result.ok).toBe(true) | ||
| // Both repos are attempted; a non-blocking failure does not stop the loop. | ||
| expect(scanner.calls).toEqual(['repo-a', 'repo-b']) | ||
| }) | ||
|
|
||
| it('fails when every repo errored for a non-blocking reason', async () => { | ||
| const scanner = fakeScanner({ | ||
| 'repo-a': noManifests, | ||
| 'repo-b': noManifests, | ||
| }) | ||
| const result = await runGithubScanLoop(['repo-a', 'repo-b'], scanner.fn) | ||
| expect(result.ok).toBe(false) | ||
| expect(result.ok ? '' : result.message).toBe('All repos failed to scan') | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.