Skip to content

Commit 1353ca0

Browse files
chambridgeclaude
andcommitted
fix(ci): use gh pr view for fork PR number lookup in coverage comment
The workflow_run event's pull_requests array is empty for fork PRs due to a known GitHub limitation. This prevented coverage comments from being posted on fork PRs. Replace the JavaScript-based pull_requests array lookup with gh pr view command, which reliably finds PRs from both forks and the same repo. Add conditional logic to use the correct branch query format: - Fork PRs: "owner:branch" format - Same-repo PRs: "branch" format See: https://github.com/orgs/community/discussions/25220 Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Chris Hambridge <chambrid@redhat.com>
1 parent 538e703 commit 1353ca0

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

.github/workflows/coverage-comment.yml

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,42 @@ jobs:
2424
steps:
2525
- name: Get PR number from workflow run
2626
id: pr_info
27-
uses: actions/github-script@v7
28-
with:
29-
script: |
30-
// Get the PR associated with the workflow run (trusted source)
31-
const workflowRun = await github.rest.actions.getWorkflowRun({
32-
owner: context.repo.owner,
33-
repo: context.repo.repo,
34-
run_id: context.payload.workflow_run.id
35-
});
27+
env:
28+
GH_TOKEN: ${{ github.token }}
29+
run: |
30+
# For fork PRs, pull_requests array is empty in workflow_run event
31+
# Use gh pr view to find the PR number
32+
# See: https://github.com/orgs/community/discussions/25220
3633
37-
// Extract PR number from the workflow run's pull_requests array
38-
const pullRequests = workflowRun.data.pull_requests;
39-
if (!pullRequests || pullRequests.length === 0) {
40-
core.setFailed('No pull request associated with this workflow run');
41-
return;
42-
}
34+
HEAD_REPO="${{ github.event.workflow_run.head_repository.full_name }}"
35+
HEAD_BRANCH="${{ github.event.workflow_run.head_branch }}"
36+
TARGET_REPO="${{ github.repository }}"
37+
38+
echo "Looking for PR from ${HEAD_REPO} branch ${HEAD_BRANCH}"
39+
40+
# Determine branch query format:
41+
# - Fork PRs: use "owner:branch" format
42+
# - Same-repo PRs: use just "branch" format
43+
if [ "$HEAD_REPO" = "$TARGET_REPO" ]; then
44+
BRANCH_QUERY="${HEAD_BRANCH}"
45+
else
46+
BRANCH_QUERY="${HEAD_REPO%%/*}:${HEAD_BRANCH}"
47+
fi
48+
49+
echo "Using branch query: ${BRANCH_QUERY}"
50+
51+
PR_NUMBER=$(gh pr view \
52+
--repo "$TARGET_REPO" \
53+
"$BRANCH_QUERY" \
54+
--json number --jq .number 2>/dev/null || echo "")
55+
56+
if [ -z "$PR_NUMBER" ]; then
57+
echo "::error::Could not find PR for ${BRANCH_QUERY} in ${TARGET_REPO}"
58+
exit 1
59+
fi
4360
44-
const prNumber = pullRequests[0].number;
45-
core.setOutput('pr_number', prNumber);
46-
console.log(`PR number from workflow run: ${prNumber}`);
61+
echo "Found PR #${PR_NUMBER}"
62+
echo "pr_number=${PR_NUMBER}" >> "$GITHUB_OUTPUT"
4763
4864
- name: Download coverage data
4965
uses: actions/download-artifact@v4

0 commit comments

Comments
 (0)