Resolve PR by head SHA in coverage-comment workflow#847
Merged
Conversation
Two issues with the previous "Resolve PR number" step:
1. Inline `${{ toJson(github.event) }}` interpolation inside `'...'`
shell quoting could be broken by any single quote in the payload
(e.g. an apostrophe in a commit message), producing the
notorious `syntax error near unexpected token "else"`.
2. `workflow_run.pull_requests[]` is *always empty* for PRs whose
head ref lives in a fork. By GitHub design, the default-branch
`workflow_run` handler (which holds the write token) does not
receive cross-repo PR linkage. The first PR through the new
coverage workflow happened to come from a fork, so the comment
was posted to the tracking issue instead of the PR.
Replace the bash step with a github-script step:
- reads `github.event` via an environment variable, immune to
payload-quoting issues;
- first tries `workflow_run.pull_requests[0]` for the same-repo
branch case;
- falls back to
`GET /repos/{owner}/{repo}/commits/{sha}/pulls` (the
"pull requests associated with commit" API), which is the only
way to recover a PR number from a fork `workflow_run` payload;
- prefers an open PR but tolerates a closed one if there's a
squash-merge race between the build and this comment job;
- falls through to the tracking-issue path only when neither
source produces a PR.
The downstream "Comment on PR" step is unchanged — it consumes the
same `steps.pr.outputs.pr` it always did.
Coverage report (cross-platform merged)Lines covered ( Merged line coverage is the per-line union across all platforms. Region coverage is reported per-platform only; no cross-platform region total is computed. Per-directory breakdown
Per-platform contributions (advisory)
|
mjp41
added a commit
that referenced
this pull request
May 10, 2026
The SHA-based fallback added in #847 (resolve PR by head SHA when `workflow_run.pull_requests[]` is empty) handles fork PRs correctly, but it also fires for `schedule` and `push` runs on `main`. The nightly's head SHA is the merge commit of whichever PR last landed, and `listPullRequestsAssociatedWithCommit` returns that PR — so the nightly coverage report ended up posted as a comment on the just- merged (now closed) PR instead of on the tracking issue. Two changes, both narrowing the fallback: 1. Only run the SHA fallback when `workflow_run.event === 'pull_request'`. `schedule`/`push` runs on the default branch always belong on the tracking issue and have no business looking up a PR by SHA. 2. Require the matched PR to be in the `open` state. The previous "fall back to any PR if only closed ones match (e.g. squash-merge race)" is undesirable: if the PR has already merged, posting a fresh coverage comment on it is noise, not information. If a contributor really wanted the comment on a merged PR they can re-run the workflow. Together these mean the SHA fallback only ever fires for an open fork PR, which is the only case it was ever meant to handle.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
workflow_run.pull_requests[]is always empty for PRs whose head ref lives in a fork. By GitHub design, the default-branchworkflow_runhandler (which holds the write token) does not receive cross-repo PR linkage. The first PR through the new coverage workflow happened to come from a fork, so the comment was posted to the tracking issue instead of the PR.Replace the bash step with a github-script step:
github.eventvia an environment variable, immune to payload-quoting issues;workflow_run.pull_requests[0]for the same-repo branch case;GET /repos/{owner}/{repo}/commits/{sha}/pulls(the "pull requests associated with commit" API), which is the only way to recover a PR number from a forkworkflow_runpayload;The downstream "Comment on PR" step is unchanged — it consumes the same
steps.pr.outputs.prit always did.