Skip to content

Commit 79a5990

Browse files
authored
ci: resolve PR number from head SHA in benchmark report (#2278)
## Summary The `PR Benchmark Report` workflow failed to comment on [#2252](#2252) after a successful benchmark run ([run 28865892835](https://github.com/prometheus/client_java/actions/runs/28865892835)): ``` gh: Not Found (HTTP 404) parse "https://api.github.com/repos/prometheus/client_java/issues/comments/{\r": net/url: invalid control character in URL ``` Root cause: `workflow_run.pull_requests` is empty when the triggering run's head branch is not on the default branch (or the PR is from a fork). With `PR_NUMBER` empty, the subsequent `repos/${REPO}/issues//comments` API call 404s and the paginator emits a malformed URL, so no comment gets posted. ## Changes - Fall back to `repos/{owner}/{repo}/commits/{sha}/pulls` to resolve the PR from the head SHA when `workflow_run.pull_requests` is empty. - Fail fast with an explicit error if the PR still can't be resolved, so future regressions don't cascade into cryptic URL-parse errors. ## Test plan - [ ] Re-run the report workflow against run 28861406560 (or a fresh push to #2252) and confirm a benchmark comment lands on the PR. Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
1 parent 4dc54da commit 79a5990

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,19 @@ jobs:
3131
RUN_URL: ${{ github.event.workflow_run.html_url }}
3232
CONCLUSION: ${{ github.event.workflow_run.conclusion }}
3333
run: |
34-
PR_NUMBER=$(gh api "repos/${REPO}/actions/runs/${RUN_ID}" --jq '.pull_requests[0].number')
3534
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
40+
PR_NUMBER=$(gh api "repos/${REPO}/commits/${HEAD_SHA}/pulls" \
41+
--jq '.[] | select(.state == "open") | .number' | head -1)
42+
fi
43+
if [[ -z "${PR_NUMBER}" ]]; then
44+
echo "Could not determine PR number for run ${RUN_ID} (head ${HEAD_SHA})." >&2
45+
exit 1
46+
fi
3647
3748
COMMENT_ARTIFACT_ID=$(gh api "repos/${REPO}/actions/runs/${RUN_ID}/artifacts" \
3849
--jq '.artifacts[] | select(.name == "pr-benchmark-comment-pr-'"${PR_NUMBER}"'-'"${HEAD_SHA}"'") | .id' \

0 commit comments

Comments
 (0)