ci: Use gh pr list to get PR number#8123
Conversation
Signed-off-by: Yuri Shkuro <github@ysh.us>
There was a problem hiding this comment.
Pull request overview
Updates the CI Summary Report workflow to better resolve the PR number for workflow_run-triggered executions (notably when the workflow_run.pull_requests array is empty), so the workflow can still post PR comments and checks.
Changes:
- Add a fallback path that uses
gh pr listto locate the PR number by head branch whenworkflow_run.pull_requests[0]is missing. - Log the resolved PR number for easier debugging of fork PR scenarios.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| --json number,headRefOid \ | ||
| --jq ".[] | select(.headRefOid == \"$HEAD_SHA\") | .number") | ||
| echo "Resolved PR #$PR_NUMBER from head branch (fork PR fallback)" |
There was a problem hiding this comment.
gh pr list can return multiple PRs for the same head: search (e.g., same branch name across forks), and the current --jq expression would then output multiple numbers separated by newlines. That can corrupt subsequent $GITHUB_OUTPUT writes (pr_number=$PR_NUMBER) and make the workflow flaky. Consider constraining the query (--limit 1, --state open) and/or making the jq expression return a single scalar (e.g., pick the first match) and only logging success when PR_NUMBER is non-empty/non-null.
| --json number,headRefOid \ | |
| --jq ".[] | select(.headRefOid == \"$HEAD_SHA\") | .number") | |
| echo "Resolved PR #$PR_NUMBER from head branch (fork PR fallback)" | |
| --state open \ | |
| --limit 20 \ | |
| --json number,headRefOid \ | |
| --jq "map(select(.headRefOid == \"$HEAD_SHA\")) | .[0].number") | |
| if [ -n "$PR_NUMBER" ] && [ "$PR_NUMBER" != "null" ]; then | |
| echo "Resolved PR #$PR_NUMBER from head branch (fork PR fallback)" | |
| fi |
| # workflow_run.pull_requests is empty for fork PRs (renovate-bot, dependabot, etc.). | ||
| # Fall back to searching for a PR matching the head branch. |
There was a problem hiding this comment.
The comment says fork PRs are "(renovate-bot, dependabot, etc.)", but those actors don't necessarily imply a cross-repo fork (e.g., dependabot PRs are typically branches on the same repo). Consider rewording to describe the actual condition (workflow_run.pull_requests is empty for cross-repo fork PRs) to avoid misleading future readers.
| # workflow_run.pull_requests is empty for fork PRs (renovate-bot, dependabot, etc.). | |
| # Fall back to searching for a PR matching the head branch. | |
| # For cross-repo fork PRs, workflow_run.pull_requests is empty in the event payload. | |
| # In that case, fall back to searching for a PR matching the head branch and commit. |
| # Fall back to searching for a PR matching the head branch. | ||
| if [ -z "$PR_NUMBER" ] && [ "${{ github.event.workflow_run.event }}" == "pull_request" ]; then | ||
| PR_NUMBER=$(gh pr list \ | ||
| --repo "${{ github.repository }}" \ | ||
| --search "head:${{ github.event.workflow_run.head_branch }}" \ | ||
| --json number,headRefOid \ | ||
| --jq ".[] | select(.headRefOid == \"$HEAD_SHA\") | .number") | ||
| echo "Resolved PR #$PR_NUMBER from head branch (fork PR fallback)" |
There was a problem hiding this comment.
The fork-PR fallback is likely never going to match because HEAD_SHA falls back to github.event.workflow_run.head_sha, which (for workflows triggered by pull_request) is typically the PR merge commit SHA, while gh pr list --json headRefOid returns the PR head commit OID. This means the select(.headRefOid == "$HEAD_SHA") filter will often produce no results and PR_NUMBER stays empty. Prefer resolving the PR number directly from the run SHA via the REST endpoint repos/{owner}/{repo}/commits/{sha}/pulls (or request/compare against the PR's merge commit OID instead of headRefOid).
| # Fall back to searching for a PR matching the head branch. | |
| if [ -z "$PR_NUMBER" ] && [ "${{ github.event.workflow_run.event }}" == "pull_request" ]; then | |
| PR_NUMBER=$(gh pr list \ | |
| --repo "${{ github.repository }}" \ | |
| --search "head:${{ github.event.workflow_run.head_branch }}" \ | |
| --json number,headRefOid \ | |
| --jq ".[] | select(.headRefOid == \"$HEAD_SHA\") | .number") | |
| echo "Resolved PR #$PR_NUMBER from head branch (fork PR fallback)" | |
| # Fall back to resolving the PR directly from the run's commit SHA. | |
| if [ -z "$PR_NUMBER" ] && [ "${{ github.event.workflow_run.event }}" == "pull_request" ]; then | |
| PR_NUMBER=$(gh api "repos/${{ github.repository }}/commits/$HEAD_SHA/pulls" --jq '.[0].number') | |
| echo "Resolved PR #$PR_NUMBER from commit SHA $HEAD_SHA (fork PR fallback)" |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8123 +/- ##
=======================================
Coverage 95.67% 95.67%
=======================================
Files 317 317
Lines 16734 16734
=======================================
Hits 16010 16010
Misses 571 571
Partials 153 153
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
No description provided.