Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/utils/__tests__/git-review-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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: '',
Expand Down
11 changes: 8 additions & 3 deletions src/utils/git-review-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}
Expand All @@ -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');
}
Expand Down
Loading