From 5d747feffd19145a14f8e099fe213889b4a8e18c Mon Sep 17 00:00:00 2001 From: Eric Engberg Date: Fri, 16 Oct 2026 21:14:58 -0500 Subject: [PATCH] fix(git): retry a PR lookup without CI fields only when gh names that field With a Git CI widget on the line, the PR lookup asks `gh pr view` for statusCheckRollup and runs again without it when gh can't give that field. The check searched the whole error text for "statuscheckrollup". execFileSync starts that error's message with the command line, which asks for statusCheckRollup, so every failure matched: no PR for the branch, an auth error, a network error. Each failing lookup ran gh twice inside the one 5s budget that the lookup with gh's own repo resolution and the one pinned to origin with `--repo` share. On a branch with no PR that was four gh runs instead of two. When gh takes about 1.7s or more, the pinned lookup, which is what finds a PR opened from a fork, ran out of time and the lookup was cached as no PR. Decide from gh's stderr alone. gh names the field when it can't give it: its --json check prints `Unknown JSON field: "statusCheckRollup"`, and a GraphQL error on the field gives the field's path. The tests' stand-in gh failures now carry the command line in their message and gh's output in stderr, as execFileSync's errors do. --- src/utils/__tests__/git-review-cache.test.ts | 18 ++++++++++++++---- src/utils/git-review-cache.ts | 11 ++++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/utils/__tests__/git-review-cache.test.ts b/src/utils/__tests__/git-review-cache.test.ts index fb2095367..78d743db3 100644 --- a/src/utils/__tests__/git-review-cache.test.ts +++ b/src/utils/__tests__/git-review-cache.test.ts @@ -222,6 +222,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; @@ -364,8 +374,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(); @@ -657,7 +667,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', @@ -782,7 +792,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 35dbe5270..cc7b7ab90 100644 --- a/src/utils/git-review-cache.ts +++ b/src/utils/git-review-cache.ts @@ -440,7 +440,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 ''; } @@ -449,11 +451,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'); }