Skip to content

Commit 6886f66

Browse files
committed
refactor(github): extract canonical error-message constants
Export `GITHUB_ERR_*` constants from utils/git/github.mts so callers short-circuit on blocking conditions via constant reference instead of matching literal strings. Previously the create-scan-from-github loop hard-coded four copies of the same message text — quietly drifting if the handler's wording ever changed would re-introduce the "ok:true / 0 manifests" silent-failure this PR was opened to fix.
1 parent 7f7f649 commit 6886f66

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

packages/cli/src/commands/scan/create-scan-from-github.mts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ import { REPORT_LEVEL_ERROR } from '../../constants/reporting.mjs'
1313
import { formatErrorWithDetail } from '../../utils/error/errors.mjs'
1414
import { socketHttpRequest } from '../../utils/socket/api.mjs'
1515
import { isReportSupportedFile } from '../../utils/fs/glob.mts'
16-
import { getOctokit, withGitHubRetry } from '../../utils/git/github.mts'
16+
import {
17+
GITHUB_ERR_ABUSE_DETECTION,
18+
GITHUB_ERR_AUTH_FAILED,
19+
GITHUB_ERR_GRAPHQL_RATE_LIMIT,
20+
GITHUB_ERR_RATE_LIMIT,
21+
getOctokit,
22+
withGitHubRetry,
23+
} from '../../utils/git/github.mts'
1724
import { fetchListAllRepos } from '../repository/fetch-list-all-repos.mts'
1825

1926
import type { CResult, OutputKind } from '../../types.mts'
@@ -123,16 +130,14 @@ export async function createScanFromGithub({
123130
repo: repoSlug,
124131
message: scanCResult.message,
125132
})
126-
// Stop the loop if we hit a rate limit or auth failure — every
127-
// subsequent repo will fail for the same reason and continuing
128-
// only burns more quota while delaying the real error. Strings
129-
// here match `handleGitHubApiError` / `handleGraphqlError` in
130-
// utils/git/github.mts.
133+
// Stop on rate-limit / auth failures: every subsequent repo will
134+
// fail for the same reason and continuing only burns more quota
135+
// while delaying the real error.
131136
if (
132-
scanCResult.message === 'GitHub rate limit exceeded' ||
133-
scanCResult.message === 'GitHub GraphQL rate limit exceeded' ||
134-
scanCResult.message === 'GitHub abuse detection triggered' ||
135-
scanCResult.message === 'GitHub authentication failed'
137+
scanCResult.message === GITHUB_ERR_RATE_LIMIT ||
138+
scanCResult.message === GITHUB_ERR_GRAPHQL_RATE_LIMIT ||
139+
scanCResult.message === GITHUB_ERR_ABUSE_DETECTION ||
140+
scanCResult.message === GITHUB_ERR_AUTH_FAILED
136141
) {
137142
blockingError = {
138143
ok: false,

packages/cli/src/utils/git/github.mts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ import type { SpawnOptions } from '@socketsecurity/lib/spawn'
5454

5555
export type Pr = components['schemas']['pull-request']
5656

57+
// Canonical `message` values returned by `handleGitHubApiError` /
58+
// `handleGraphqlError`. Exported so callers can short-circuit on
59+
// blocking conditions without matching free-form strings.
60+
export const GITHUB_ERR_ABUSE_DETECTION = 'GitHub abuse detection triggered'
61+
export const GITHUB_ERR_AUTH_FAILED = 'GitHub authentication failed'
62+
export const GITHUB_ERR_GRAPHQL_RATE_LIMIT =
63+
'GitHub GraphQL rate limit exceeded'
64+
export const GITHUB_ERR_RATE_LIMIT = 'GitHub rate limit exceeded'
65+
5766
interface CacheEntry {
5867
timestamp: number
5968
data: JsonContent
@@ -398,7 +407,7 @@ export function handleGraphqlError(
398407
if (isGraphqlRateLimitError(e)) {
399408
return {
400409
ok: false,
401-
message: 'GitHub GraphQL rate limit exceeded',
410+
message: GITHUB_ERR_GRAPHQL_RATE_LIMIT,
402411
cause:
403412
`GitHub GraphQL rate limit exceeded while ${context}. ` +
404413
'Try again in a few minutes.\n\n' +
@@ -440,7 +449,7 @@ export function handleGitHubApiError(
440449
if (status === 403 && e.message.includes('secondary rate limit')) {
441450
return {
442451
ok: false,
443-
message: 'GitHub abuse detection triggered',
452+
message: GITHUB_ERR_ABUSE_DETECTION,
444453
cause:
445454
`GitHub abuse detection triggered while ${context}. ` +
446455
'This happens when making too many requests in a short period. ' +
@@ -474,7 +483,7 @@ export function handleGitHubApiError(
474483

475484
return {
476485
ok: false,
477-
message: 'GitHub rate limit exceeded',
486+
message: GITHUB_ERR_RATE_LIMIT,
478487
cause:
479488
`GitHub API rate limit exceeded while ${context}. ` +
480489
(waitTime
@@ -492,7 +501,7 @@ export function handleGitHubApiError(
492501
if (status === 401) {
493502
return {
494503
ok: false,
495-
message: 'GitHub authentication failed',
504+
message: GITHUB_ERR_AUTH_FAILED,
496505
cause:
497506
`GitHub authentication failed while ${context}. ` +
498507
'Your token may be invalid, expired, or missing required permissions.\n\n' +

packages/cli/test/unit/commands/scan/create-scan-from-github.test.mts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ const mockWithGitHubRetry = vi.hoisted(() =>
5151

5252
// Mock dependencies.
5353
vi.mock('../../../../src/utils/git/github.mts', () => ({
54+
GITHUB_ERR_ABUSE_DETECTION: 'GitHub abuse detection triggered',
55+
GITHUB_ERR_AUTH_FAILED: 'GitHub authentication failed',
56+
GITHUB_ERR_GRAPHQL_RATE_LIMIT: 'GitHub GraphQL rate limit exceeded',
57+
GITHUB_ERR_RATE_LIMIT: 'GitHub rate limit exceeded',
5458
getOctokit: vi.fn(() => mockOctokit),
5559
withGitHubRetry: mockWithGitHubRetry,
5660
}))

0 commit comments

Comments
 (0)