-
Notifications
You must be signed in to change notification settings - Fork 2.9k
ci: Use gh pr list to get PR number #8123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -68,6 +68,17 @@ jobs: | |||||||||||||||||||||||||||||||||||||||||||||
| RUN_ID="${{ github.event.workflow_run.id }}" | ||||||||||||||||||||||||||||||||||||||||||||||
| PR_NUMBER="${{ github.event.workflow_run.pull_requests[0].number }}" | ||||||||||||||||||||||||||||||||||||||||||||||
| HEAD_SHA="${{ github.event.workflow_run.pull_requests[0].head.sha || github.event.workflow_run.head_sha }}" | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| # workflow_run.pull_requests is empty for fork PRs (renovate-bot, dependabot, etc.). | ||||||||||||||||||||||||||||||||||||||||||||||
| # 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)" | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+78
to
+80
|
||||||||||||||||||||||||||||||||||||||||||||||
| --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 |
Copilot
AI
Mar 2, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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_requestsis empty for cross-repo fork PRs) to avoid misleading future readers.