Skip to content

PR Benchmark Report #110

PR Benchmark Report

PR Benchmark Report #110

---
name: PR Benchmark Report
on:
# zizmor: ignore[dangerous-triggers] -- this workflow never checks out or executes PR code;
# it only reads artifacts produced by the benchmark run and posts a PR comment.
workflow_run:
workflows:
- PR Benchmarks
types:
- completed
permissions: {}
jobs:
comment:
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion != 'cancelled'
runs-on: ubuntu-24.04
permissions:
actions: read
contents: read
pull-requests: write
steps:
- name: Comment on PR with benchmark results
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
RUN_ID: ${{ github.event.workflow_run.id }}
RUN_URL: ${{ github.event.workflow_run.html_url }}
CONCLUSION: ${{ github.event.workflow_run.conclusion }}
run: |
HEAD_SHA=$(gh api "repos/${REPO}/actions/runs/${RUN_ID}" --jq '.head_sha')
PR_NUMBER=$(gh api \
"repos/${REPO}/actions/runs/${RUN_ID}" \
--jq '.pull_requests[0].number // ""')
# workflow_run.pull_requests can be empty for forked PRs. Prefer the
# artifact name because the benchmark workflow had the PR number when it
# uploaded the artifacts, then fall back to the run head repository/branch.
if [[ -z "${PR_NUMBER}" ]]; then
PR_NUMBER=$(gh api "repos/${REPO}/actions/runs/${RUN_ID}/artifacts" \
--jq '.artifacts[].name
| select(startswith("pr-benchmark-") and endswith("-'"${HEAD_SHA}"'"))
| split("-")[4]' \
| sort -u | head -1)
fi
if [[ -z "${PR_NUMBER}" ]]; then
HEAD_OWNER=$(gh api \
"repos/${REPO}/actions/runs/${RUN_ID}" \
--jq '.head_repository.owner.login // ""')
HEAD_BRANCH=$(gh api \
"repos/${REPO}/actions/runs/${RUN_ID}" \
--jq '.head_branch // ""')
if [[ -n "${HEAD_OWNER}" && -n "${HEAD_BRANCH}" ]]; then
PR_NUMBER=$(gh api --method GET "repos/${REPO}/pulls" \
-f state=open \
-f head="${HEAD_OWNER}:${HEAD_BRANCH}" \
--jq '.[].number' \
| head -1)
fi
fi
if [[ -z "${PR_NUMBER}" ]]; then
PR_NUMBER=$(gh api "repos/${REPO}/commits/${HEAD_SHA}/pulls" \
--jq '.[] | select(.state == "open") | .number' | head -1)
fi
if [[ -z "${PR_NUMBER}" ]]; then
echo "Could not determine PR number for run ${RUN_ID} (head ${HEAD_SHA})." >&2
exit 1
fi
COMMENT_ARTIFACT_NAME="pr-benchmark-comment-pr-${PR_NUMBER}-${HEAD_SHA}"
RESULTS_ARTIFACT_NAME="pr-benchmark-results-pr-${PR_NUMBER}-${HEAD_SHA}"
COMMENT_ARTIFACT_ID=$(gh api "repos/${REPO}/actions/runs/${RUN_ID}/artifacts" \
--jq '.artifacts[]
| select(.name == "'"${COMMENT_ARTIFACT_NAME}"'")
| .id' \
| head -1)
MARKER="<!-- pr-benchmark-report -->"
if [[ -n "${COMMENT_ARTIFACT_ID}" ]]; then
gh api \
"repos/${REPO}/actions/artifacts/${COMMENT_ARTIFACT_ID}/zip" \
> /tmp/pr-benchmark-comment.zip
unzip -p /tmp/pr-benchmark-comment.zip pr-benchmark-comment.md \
> /tmp/pr-benchmark-comment.md
SUMMARY_BODY=$(cat /tmp/pr-benchmark-comment.md)
else
SUMMARY_BODY="_Benchmark summary artifact was not found; "
SUMMARY_BODY+="see the workflow run for details._"
fi
if [[ "${CONCLUSION}" == "success" ]]; then
STATUS_LINE="Benchmark run succeeded for \`${HEAD_SHA}\`."
else
STATUS_LINE="Benchmark run finished with conclusion \`${CONCLUSION}\` "
STATUS_LINE+="for \`${HEAD_SHA}\`."
fi
body=$(cat <<EOF
${MARKER}
## Benchmark results
${STATUS_LINE}
- Workflow run: ${RUN_URL}
- Artifact: \`${RESULTS_ARTIFACT_NAME}\`
${SUMMARY_BODY}
EOF
)
comment_id=$(gh api "repos/${REPO}/issues/${PR_NUMBER}/comments" --paginate \
--jq ".[] | select(.body | startswith(\"${MARKER}\")) | .id" | head -1)
if [[ -n "${comment_id}" ]]; then
gh api --method PATCH "repos/${REPO}/issues/comments/${comment_id}" \
--field body="$body"
else
gh pr comment "${PR_NUMBER}" --repo "${REPO}" --body "$body"
fi