From abbe60c655d17ebcaadf8083bf4686f4b2555b57 Mon Sep 17 00:00:00 2001 From: Fleet City Date: Fri, 4 Sep 2026 16:05:23 +0000 Subject: [PATCH 1/2] fix(ci): use valid --json state field in PR Backport manual-dispatch validation gh pr view --json merged has never been a valid query (mergedAt/state are); every workflow_dispatch of the PR Backport workflow dies in 'Validate inputs for manual triggers' with: Unknown JSON field: "merged". Run 33892240975 (PR #16967) is the latest example. Replace the existence probe with --json state and derive the merged check as .state == "MERGED". Automatic (pull_request_target) path is untouched. --- .github/workflows/pr-backport.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From fddfcd0831064b4d60144036322aa9e5882f7bd8 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Fri, 4 Sep 2026 09:27:50 -0700 Subject: [PATCH 2/2] fix(ci): drop the last --json merged call site and guard the field repo-wide The same removed gh field is used in backport-auto-merge.yaml, where it is worse than the loud dispatch failure this PR already fixes: the call is `2>/dev/null || echo false`, so `is_merged` swallows the error and answers false for every PR. That kills both of its uses. The pre-merge check stops recognising a PR a concurrent run or a human already merged, so the loop burns three merge attempts on it; and the final reconciliation, whose whole purpose is "don't post a false failure if the PR is in fact merged", can no longer rescue the race. The workflow then comments "automatic merge failed after 3 attempts, please merge manually" on backport PRs that did merge. Nothing goes red, so the sweep has been degrading silently every 15 minutes. Adds a static guard over every workflow rather than a third one-line edit, because the field is the defect, not the call site. It resolves literal `FIELDS=` assignments too, so pr-label-backport.yaml's `--json "$FIELDS"` is checked rather than skipped, and it fails on any call site whose fields it cannot read so the scan cannot go quietly blind. Verified: red before the workflow fix naming backport-auto-merge.yaml, and red again naming pr-backport.yaml when that file is reverted to its pre-fix main state, so the guard covers the defect this PR opened for. 108/108 scripts/cicd tests pass; oxfmt, check-yaml.sh, check-shell.sh, tsc clean. --- .github/workflows/backport-auto-merge.yaml | 5 +- scripts/cicd/gh-pr-view-fields.test.ts | 124 +++++++++++++++++++++ 2 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 scripts/cicd/gh-pr-view-fields.test.ts 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/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([]) + }) +})