Skip to content

Commit 4a6958a

Browse files
authored
ci: handle fork PR benchmark reports (#2293)
## Summary Fix the `PR Benchmark Report` workflow for forked PR benchmark runs like #2252, where `workflow_run.pull_requests` and `commits/{sha}/pulls` can both be empty for the upstream repository. ## Changes - Resolve the PR number from the uploaded benchmark artifact names first. - Fall back to the workflow run head repository owner and branch via `repos/{owner}/{repo}/pulls`. - Keep the existing commit-SHA lookup as a final fallback. - Remove the artifact ZIP download `Accept: application/octet-stream` header because `gh api` rejected it during local replay. ## Validation - Replayed the resolution logic against benchmark run `28928458470` and confirmed it resolves PR #2252 and downloads artifact `8164318312`. - Ran `mise run lint:fix`. - Ran `git diff --check`.
1 parent 87f8b1e commit 4a6958a

1 file changed

Lines changed: 43 additions & 12 deletions

File tree

.github/workflows/pr-benchmark-report.yml

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,36 @@ jobs:
3232
CONCLUSION: ${{ github.event.workflow_run.conclusion }}
3333
run: |
3434
HEAD_SHA=$(gh api "repos/${REPO}/actions/runs/${RUN_ID}" --jq '.head_sha')
35-
PR_NUMBER=$(gh api "repos/${REPO}/actions/runs/${RUN_ID}" --jq '.pull_requests[0].number')
36-
# workflow_run.pull_requests is empty when the triggering run's head branch
37-
# is not on the default branch (or the PR is from a fork). Fall back to
38-
# resolving the PR from the head SHA.
39-
if [[ -z "${PR_NUMBER}" || "${PR_NUMBER}" == "null" ]]; then
35+
PR_NUMBER=$(gh api \
36+
"repos/${REPO}/actions/runs/${RUN_ID}" \
37+
--jq '.pull_requests[0].number // ""')
38+
39+
# workflow_run.pull_requests can be empty for forked PRs. Prefer the
40+
# artifact name because the benchmark workflow had the PR number when it
41+
# uploaded the artifacts, then fall back to the run head repository/branch.
42+
if [[ -z "${PR_NUMBER}" ]]; then
43+
PR_NUMBER=$(gh api "repos/${REPO}/actions/runs/${RUN_ID}/artifacts" \
44+
--jq '.artifacts[].name
45+
| select(startswith("pr-benchmark-") and endswith("-'"${HEAD_SHA}"'"))
46+
| split("-")[4]' \
47+
| sort -u | head -1)
48+
fi
49+
if [[ -z "${PR_NUMBER}" ]]; then
50+
HEAD_OWNER=$(gh api \
51+
"repos/${REPO}/actions/runs/${RUN_ID}" \
52+
--jq '.head_repository.owner.login // ""')
53+
HEAD_BRANCH=$(gh api \
54+
"repos/${REPO}/actions/runs/${RUN_ID}" \
55+
--jq '.head_branch // ""')
56+
if [[ -n "${HEAD_OWNER}" && -n "${HEAD_BRANCH}" ]]; then
57+
PR_NUMBER=$(gh api --method GET "repos/${REPO}/pulls" \
58+
-f state=open \
59+
-f head="${HEAD_OWNER}:${HEAD_BRANCH}" \
60+
--jq '.[].number' \
61+
| head -1)
62+
fi
63+
fi
64+
if [[ -z "${PR_NUMBER}" ]]; then
4065
PR_NUMBER=$(gh api "repos/${REPO}/commits/${HEAD_SHA}/pulls" \
4166
--jq '.[] | select(.state == "open") | .number' | head -1)
4267
fi
@@ -45,27 +70,33 @@ jobs:
4570
exit 1
4671
fi
4772
73+
COMMENT_ARTIFACT_NAME="pr-benchmark-comment-pr-${PR_NUMBER}-${HEAD_SHA}"
74+
RESULTS_ARTIFACT_NAME="pr-benchmark-results-pr-${PR_NUMBER}-${HEAD_SHA}"
4875
COMMENT_ARTIFACT_ID=$(gh api "repos/${REPO}/actions/runs/${RUN_ID}/artifacts" \
49-
--jq '.artifacts[] | select(.name == "pr-benchmark-comment-pr-'"${PR_NUMBER}"'-'"${HEAD_SHA}"'") | .id' \
76+
--jq '.artifacts[]
77+
| select(.name == "'"${COMMENT_ARTIFACT_NAME}"'")
78+
| .id' \
5079
| head -1)
5180
52-
RESULTS_ARTIFACT_NAME="pr-benchmark-results-pr-${PR_NUMBER}-${HEAD_SHA}"
5381
MARKER="<!-- pr-benchmark-report -->"
5482
5583
if [[ -n "${COMMENT_ARTIFACT_ID}" ]]; then
5684
gh api \
57-
-H "Accept: application/octet-stream" \
58-
"repos/${REPO}/actions/artifacts/${COMMENT_ARTIFACT_ID}/zip" > /tmp/pr-benchmark-comment.zip
59-
unzip -p /tmp/pr-benchmark-comment.zip pr-benchmark-comment.md > /tmp/pr-benchmark-comment.md
85+
"repos/${REPO}/actions/artifacts/${COMMENT_ARTIFACT_ID}/zip" \
86+
> /tmp/pr-benchmark-comment.zip
87+
unzip -p /tmp/pr-benchmark-comment.zip pr-benchmark-comment.md \
88+
> /tmp/pr-benchmark-comment.md
6089
SUMMARY_BODY=$(cat /tmp/pr-benchmark-comment.md)
6190
else
62-
SUMMARY_BODY="_Benchmark summary artifact was not found; see the workflow run for details._"
91+
SUMMARY_BODY="_Benchmark summary artifact was not found; "
92+
SUMMARY_BODY+="see the workflow run for details._"
6393
fi
6494
6595
if [[ "${CONCLUSION}" == "success" ]]; then
6696
STATUS_LINE="Benchmark run succeeded for \`${HEAD_SHA}\`."
6797
else
68-
STATUS_LINE="Benchmark run finished with conclusion \`${CONCLUSION}\` for \`${HEAD_SHA}\`."
98+
STATUS_LINE="Benchmark run finished with conclusion \`${CONCLUSION}\` "
99+
STATUS_LINE+="for \`${HEAD_SHA}\`."
69100
fi
70101
71102
body=$(cat <<EOF

0 commit comments

Comments
 (0)