diff --git a/src/utils/__tests__/git-review-cache.test.ts b/src/utils/__tests__/git-review-cache.test.ts index 876d6278a..1d903b4a1 100644 --- a/src/utils/__tests__/git-review-cache.test.ts +++ b/src/utils/__tests__/git-review-cache.test.ts @@ -232,6 +232,16 @@ function gitConfigFromEnv(env: NodeJS.ProcessEnv | undefined): [string, string][ ]); } +// The error execFileSync throws when `gh pr view` with CI checks exits non-zero. +// Its message starts with the command line, which names statusCheckRollup +// whatever went wrong; gh's own output is its stderr. +function ghCiLookupFailure(stderr: string): Error { + return Object.assign( + new Error(`Command failed: gh pr view --json url,number,title,state,reviewDecision,statusCheckRollup\n${stderr}`), + { stderr } + ); +} + // The fields a gh call asks for: the value after --json function jsonFields(args: string[] | undefined): string | undefined { const index = args?.indexOf('--json') ?? -1; @@ -374,8 +384,8 @@ describe('git-review-cache', () => { it('does not retry metadata-only for ordinary CI lookup failures', () => { const harness = createHarness(); harness.setOriginRemoteUrl('https://github.com/example-owner/example-repo.git'); - harness.ghResponses.push(new Error('no pull request found')); - harness.ghResponses.push(new Error('no pull request found')); + harness.ghResponses.push(ghCiLookupFailure('no pull requests found for branch "feature/cache-a"\n')); + harness.ghResponses.push(ghCiLookupFailure('no pull requests found for branch "feature/cache-a"\n')); expect(fetchGitReviewData('/tmp/repo', harness.deps, { includeChecks: true })).toBeNull(); @@ -667,7 +677,7 @@ describe('git-review-cache', () => { it('retries metadata-only when gh cannot query CI checks', () => { const harness = createHarness(); harness.setOriginRemoteUrl('https://github.com/example-owner/example-repo.git'); - harness.ghResponses.push(new Error('statusCheckRollup is unavailable')); + harness.ghResponses.push(ghCiLookupFailure('Unknown JSON field: "statusCheckRollup"\nAvailable fields:\n additions\n assignees\n')); harness.ghResponses.push(JSON.stringify({ number: 42, reviewDecision: 'APPROVED', @@ -821,7 +831,7 @@ describe('git-review-cache', () => { const harness = createHarness(); harness.setOriginRemoteUrl('https://github.com/fork-owner/example-repo.git'); harness.ghResponses.push(''); - harness.ghResponses.push(new Error('statusCheckRollup is unavailable')); + harness.ghResponses.push(ghCiLookupFailure('GraphQL: Resource not accessible by integration (repository.pullRequest.statusCheckRollup.nodes.0.commit.statusCheckRollup)\n')); harness.ghResponses.push(JSON.stringify({ number: 1, reviewDecision: '', diff --git a/src/utils/git-review-cache.ts b/src/utils/git-review-cache.ts index 430c54f9c..c8816cee5 100644 --- a/src/utils/git-review-cache.ts +++ b/src/utils/git-review-cache.ts @@ -480,7 +480,9 @@ function mapGlabState(state: string): string { return state.toUpperCase(); } -function errorText(error: unknown): string { +// What gh printed. Not the error's message: execFileSync starts that with the +// command line, which names every --json field the lookup asked for. +function ghStderr(error: unknown): string { if (!(error instanceof Error)) { return ''; } @@ -489,11 +491,14 @@ function errorText(error: unknown): string { ? (error as Error & { stderr?: Buffer | string }).stderr : undefined; const stderrText = Buffer.isBuffer(stderr) ? stderr.toString('utf8') : (stderr ?? ''); - return `${error.message}\n${stderrText}`.toLowerCase(); + return stderrText.toLowerCase(); } +// gh names the field it can't give: its flag check prints +// `Unknown JSON field: "statusCheckRollup"`, and a GraphQL error on the field +// gives its path function isCiFieldUnavailableError(error: unknown): boolean { - const text = errorText(error); + const text = ghStderr(error); return text.includes('statuscheckrollup') || text.includes('resource not accessible by integration'); }