diff --git a/.github/workflows/backport-auto-merge.yaml b/.github/workflows/backport-auto-merge.yaml index c6f3b2f1fb0..c8d9273f442 100644 --- a/.github/workflows/backport-auto-merge.yaml +++ b/.github/workflows/backport-auto-merge.yaml @@ -106,8 +106,11 @@ jobs: run: | set -euo pipefail + # `state` rather than `merged`: gh removed the `merged` JSON field, and + # the 2>/dev/null || echo false here would swallow that error and pin + # this to false forever — see scripts/cicd/gh-pr-view-fields.test.ts. is_merged() { - [ "$(GH_TOKEN="$READ_TOKEN" gh pr view "$1" --repo "$GH_REPO" --json merged --jq '.merged' 2>/dev/null || echo false)" = "true" ] + [ "$(GH_TOKEN="$READ_TOKEN" gh pr view "$1" --repo "$GH_REPO" --json state --jq '.state == "MERGED"' 2>/dev/null || echo false)" = "true" ] } for pr in $CANDIDATES; do diff --git a/.github/workflows/pr-backport.yaml b/.github/workflows/pr-backport.yaml index 14080345886..fbd1e0a020b 100644 --- a/.github/workflows/pr-backport.yaml +++ b/.github/workflows/pr-backport.yaml @@ -52,12 +52,12 @@ jobs: # Validate PR exists and is merged. The gh error is surfaced rather # than discarded: an auth failure and a genuinely missing PR are # indistinguishable once it is swallowed. - if ! PR_VIEW_ERR=$(gh pr view "${{ inputs.pr_number }}" --json merged 2>&1 >/dev/null); then + if ! PR_VIEW_ERR=$(gh pr view "${{ inputs.pr_number }}" --json state 2>&1 >/dev/null); then echo "::error::PR #${{ inputs.pr_number }} not found or inaccessible: ${PR_VIEW_ERR}" exit 1 fi - MERGED=$(gh pr view "${{ inputs.pr_number }}" --json merged --jq '.merged') + MERGED=$(gh pr view "${{ inputs.pr_number }}" --json state --jq '.state == "MERGED"') if [ "$MERGED" != "true" ]; then echo "::error::PR #${{ inputs.pr_number }} is not merged. Only merged PRs can be backported." exit 1 diff --git a/scripts/cicd/gh-pr-view-fields.test.ts b/scripts/cicd/gh-pr-view-fields.test.ts new file mode 100644 index 00000000000..d8711bb185a --- /dev/null +++ b/scripts/cicd/gh-pr-view-fields.test.ts @@ -0,0 +1,124 @@ +import { readFileSync, readdirSync } from 'node:fs' +import { join } from 'node:path' + +import { describe, expect, it } from 'vitest' + +const WORKFLOW_DIR = '.github/workflows' + +/** + * JSON fields that `gh pr view --json` no longer accepts. Requesting one makes + * gh exit non-zero with `Unknown JSON field: ""` before it prints + * anything, so the call site does not merely read a stale value — it fails. + * + * `merged` was dropped upstream and took PR Backport's manual dispatch red on + * `main` (run 33892240975). The same call in backport-auto-merge.yaml is the + * reason this is a repo-wide scan rather than a one-line fix: there the error + * is swallowed by `2>/dev/null || echo false`, so the merge-race reconciliation + * silently answers "not merged" for every PR and comments a merge failure on + * backports that did merge. + */ +const REMOVED_FIELDS: Record = { + merged: 'use `state` and compare against "MERGED", or check `mergedAt`' +} + +/** Join `\`-continued shell lines so a wrapped `--json` is still seen. */ +const joinContinuations = (source: string) => source.replace(/\\\r?\n\s*/g, ' ') + +/** + * Literal `VAR=a,b,c` shell assignments, so a call site that passes its field + * list indirectly (`--json "$FIELDS"`) is still checked rather than silently + * skipped — pr-label-backport.yaml does exactly that. + */ +const collectVariables = (source: string) => { + const variables = new Map() + for (const [, name, value] of source.matchAll( + /^\s*([A-Za-z_][A-Za-z0-9_]*)=([A-Za-z0-9_,]+)\s*$/gm + )) { + variables.set(name, value) + } + return variables +} + +interface Invocation { + file: string + fields: string[] + line: string + /** Set when the field list could not be resolved statically. */ + unresolved?: string +} + +const collectInvocations = (): Invocation[] => { + const files = readdirSync(WORKFLOW_DIR).filter((name) => + /\.ya?ml$/.test(name) + ) + + return files.flatMap((file) => { + const source = joinContinuations( + readFileSync(join(WORKFLOW_DIR, file), 'utf8') + ) + const variables = collectVariables(source) + + return source + .split('\n') + .filter((line) => line.includes('gh pr view') && line.includes('--json')) + .map((line): Invocation => { + const base = { file, line: line.trim() } + + // `--json a,b,c` — a literal, comma-separated field list. + const literal = /--json\s+([A-Za-z0-9_,]+)/.exec(line) + if (literal) { + return { ...base, fields: literal[1].split(',').filter(Boolean) } + } + + // `--json "$FIELDS"` / `--json "${FIELDS}"` — resolve from the file. + const indirect = /--json\s+"?\$\{?([A-Za-z_][A-Za-z0-9_]*)\}?"?/.exec( + line + ) + const resolved = indirect && variables.get(indirect[1]) + if (resolved) { + return { ...base, fields: resolved.split(',').filter(Boolean) } + } + + return { + ...base, + fields: [], + unresolved: indirect + ? `$${indirect[1]} is not a literal assignment in this file` + : 'field list is not a literal or a resolvable variable' + } + }) + }) +} + +describe('gh pr view --json fields', () => { + const invocations = collectInvocations() + + // Without this the suite would pass vacuously if the scan ever stopped + // matching — the failure mode the guard itself is meant to remove. + it('finds the gh pr view call sites it is meant to guard', () => { + expect(invocations.length).toBeGreaterThanOrEqual(3) + }) + + // A call site whose fields cannot be read statically is a hole in the scan, + // not a pass. Keep the list buildable from a literal so this stays checkable. + it('can resolve every requested field list', () => { + const unresolved = invocations + .filter((call) => call.unresolved) + .map((call) => `${call.file}: ${call.unresolved}\n ${call.line}`) + + expect(unresolved, unresolved.join('\n ')).toEqual([]) + }) + + it('requests no field that gh has removed', () => { + const offenders = invocations.flatMap((call) => + call.fields + .filter((field) => field in REMOVED_FIELDS) + .map( + (field) => + `${call.file}: --json ${field} is not a gh pr view field — ${REMOVED_FIELDS[field]}\n ${call.line}` + ) + ) + + expect(offenders, offenders.join('\n ')).toEqual([]) + }) +})