pr-status: look up PR runs by head SHA and load job logs on gh 2.97+ (#101707)
## Summary
`node scripts/pr-status.js <pr>` could report a clean result for a PR
whose CI had failed. Both bugs below came up while triaging
[vercel/next.js#101628](https://github.com/vercel/next.js/pull/101628).
### Wrong run for fork PRs that share a branch name
The script listed runs with `actions/runs?branch=<name>` and took the
newest `build-and-test` run. Fork PRs
[vercel/next.js#101628](https://github.com/vercel/next.js/pull/101628)
and
[vercel/next.js#101643](https://github.com/vercel/next.js/pull/101643)
both come from a branch named
`fix/proxy-rewrite-status-cache-components`, and runs for fork PRs have
an empty `pull_requests` list. So `node scripts/pr-status.js 101628`
reported the other PR's unapproved run (`action_required`, "No failed
jobs found") rather than its own failed run.
When the PR is known (passed as an argument or found for the current
branch), the script now lists runs by the PR's head commit
(`headRefOid`, `actions/runs?head_sha=`). Without a PR it still queries
by branch.
### No job logs with gh 2.97 or later
Since gh 2.97
([GHSA-3m3g-3wcr-px46](https://github.com/cli/cli/security/advisories/GHSA-3m3g-3wcr-px46)),
`gh api` refuses to print a raw response that contains an ESC byte
unless `--allow-escape-sequences` is passed, and every Actions job log
contains them. The script caught the error, so every job file said "Logs
not available" and the flaky-test check found 0 flaky tests without
saying why.
- One `getJobLogs` now serves both the PR's failed jobs and the flaky
check. It passes `--allow-escape-sequences` when `gh api --help` lists
it; older gh rejects the flag as unknown, so it is left out there.
- With the flag, gh no longer filters escape sequences, so `stripAnsi`
now removes all of them (CSI, OSC, other escapes, and any stray ESC
byte) before the logs are parsed or written to the output files.
Previously it removed only CSI sequences that end in a letter.
- A failed download now shows up in the report. The failed-jobs table
says "Logs unavailable", `index.md` lists each failure with gh's error
under "Log Fetch Failures", the job file explains why it has no
sections, and the Known Flaky Tests section warns when it couldn't read
some logs or couldn't list runs.
To make this testable, the script now runs `main()` only when invoked
directly and exports its helpers. The runs URL (`workflowRunsPath`) and
the `--help` check (`rawOutputFlagsFromHelp`) are their own functions,
and `getJobLogs` accepts the gh runner and flags, so
`test/unit/pr-status-ci-logs.test.ts` runs without gh, on Windows too.
A finished run that didn't pass but has no failed jobs, such as one
waiting for approval, still reads as clean. A separate PR handles that.
The `pr-status-triage` skill docs don't describe how runs are picked or
logs are fetched, so they're unchanged.
## Verification
- `test/unit/pr-status-ci-logs.test.ts`: undoing any one fix (head-SHA
lookup, the flag, escape stripping, reported download errors) fails a
test.
- `node scripts/pr-status.js 101628` with gh 2.103.3 reports run
43221853884 (attempt 2) with 4 failed jobs. The three deploy jobs' test
files contain the `vercel link` failure ("No existing credentials
found"), no output file contains an ESC byte, and the flaky check read
all 21 logs from other branches.
- The same run with a stand-in pre-2.97 `gh` on `PATH` (no flag in `gh
api --help`, rejects it as unknown, prints raw logs) never passes the
flag and produces output identical to the gh 2.103.3 run.
- With a stand-in `gh` whose log downloads fail with HTTP 404,
`index.md` lists the 4 failures, each job file names the error, and
Known Flaky Tests warns that 21 of 21 logs couldn't be fetched.
- `node scripts/pr-status.js --skip-flaky-check` on a branch without a
PR still queries by branch, and `--comments-only` still works.
- `stripAnsi` checked against colors, private-mode CSI, OSC 8 hyperlinks
(ST and BEL terminators), charset escapes, malformed and unterminated
sequences, and JSON text containing an escaped `\u001b`.
- prettier, eslint and ast-grep on `scripts/pr-status.js`.
<!-- NEXT_JS_LLM -->
🤖 Generated with [Claude Code](https://claude.com/claude-code)