Report paired PR benchmark #111
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Report paired PR benchmark | |
| on: | |
| workflow_run: | |
| workflows: | |
| - Pull Request Validation | |
| types: | |
| - completed | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| concurrency: | |
| group: paired-benchmark-report-${{ github.event.workflow_run.pull_requests[0].number }} | |
| cancel-in-progress: true | |
| jobs: | |
| report: | |
| name: Report paired benchmark | |
| if: >- | |
| github.event.workflow_run.event == 'pull_request' && | |
| github.event.workflow_run.pull_requests[0].number != null && | |
| github.event.workflow_run.head_repository.full_name == github.repository | |
| runs-on: ubuntu-24.04-arm | |
| steps: | |
| - name: Checkout trusted reporter | |
| uses: actions/checkout@v7.0.1 | |
| with: | |
| ref: ${{ github.event.repository.default_branch }} | |
| fetch-depth: 1 | |
| sparse-checkout: | | |
| .github/scripts/* | |
| crates/stylex-rs-compiler/benchmark/fixtures.v1.json | |
| sparse-checkout-cone-mode: false | |
| - name: Download report from source run | |
| id: download | |
| continue-on-error: true | |
| uses: actions/download-artifact@v8 | |
| with: | |
| name: paired-benchmark-report | |
| path: ${{ runner.temp }}/paired-benchmark-report | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| repository: ${{ github.repository }} | |
| run-id: ${{ github.event.workflow_run.id }} | |
| - name: Resolve trusted source identities | |
| id: identities | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }} | |
| REPOSITORY: ${{ github.repository }} | |
| SOURCE_HEAD_SHA: ${{ github.event.workflow_run.head_sha }} | |
| run: | | |
| pr_json=$(gh api "repos/$REPOSITORY/pulls/$PR_NUMBER") | |
| resolution=$( | |
| node .github/scripts/resolve-benchmark-source.mjs \ | |
| --source-head-sha "$SOURCE_HEAD_SHA" \ | |
| --current-head-sha "$(jq -r '.head.sha' <<< "$pr_json")" | |
| ) | |
| if [[ "$(jq -r '.stale' <<< "$resolution")" == "true" ]]; then | |
| echo "The PR head changed after the source run; skipping stale report." | |
| echo "stale=true" >> "$GITHUB_OUTPUT" | |
| exit 0 | |
| fi | |
| echo "stale=false" >> "$GITHUB_OUTPUT" | |
| echo "head-sha=$(jq -r '.headSha' <<< "$resolution")" >> "$GITHUB_OUTPUT" | |
| - name: Validate and render report | |
| id: render | |
| if: steps.identities.outputs.stale == 'false' && steps.download.outcome == 'success' | |
| continue-on-error: true | |
| env: | |
| HEAD_SHA: ${{ steps.identities.outputs.head-sha }} | |
| PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }} | |
| REPORT_DIR: ${{ runner.temp }}/paired-benchmark-report | |
| RUN_ID: ${{ github.event.workflow_run.id }} | |
| RUN_URL: ${{ github.event.workflow_run.html_url }} | |
| RUN_CONCLUSION: ${{ github.event.workflow_run.conclusion }} | |
| run: | | |
| test "$(find "$REPORT_DIR" -type f | wc -l)" -eq 2 | |
| test "$(stat -c %s "$REPORT_DIR/compare-revisions.verdict.v1.json")" -le 1048576 | |
| test "$(stat -c %s "$REPORT_DIR/paired-benchmark-identity.v1.json")" -le 4096 | |
| node .github/scripts/render-benchmark-report.mjs \ | |
| --input "$REPORT_DIR/compare-revisions.verdict.v1.json" \ | |
| --identity "$REPORT_DIR/paired-benchmark-identity.v1.json" \ | |
| --output "$RUNNER_TEMP/paired-benchmark-comment.md" \ | |
| --run-url "$RUN_URL" \ | |
| --conclusion "$RUN_CONCLUSION" \ | |
| --run-id "$RUN_ID" \ | |
| --pr-number "$PR_NUMBER" \ | |
| --head-sha "$HEAD_SHA" | |
| # Same renderer, so the marker and the comment layout have one | |
| # definition. A divergent copy here would break the upsert below, which | |
| # finds the previous comment by that marker. | |
| - name: Render unavailable report | |
| if: >- | |
| steps.identities.outputs.stale == 'false' && (steps.download.outcome != 'success' || | |
| steps.render.outcome != 'success') | |
| env: | |
| RUN_URL: ${{ github.event.workflow_run.html_url }} | |
| RUN_CONCLUSION: ${{ github.event.workflow_run.conclusion }} | |
| run: | | |
| node .github/scripts/render-benchmark-report.mjs \ | |
| --unavailable true \ | |
| --output "$RUNNER_TEMP/paired-benchmark-comment.md" \ | |
| --run-url "$RUN_URL" \ | |
| --conclusion "$RUN_CONCLUSION" | |
| - name: Upsert authoritative PR comment | |
| if: steps.identities.outputs.stale == 'false' | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }} | |
| REPOSITORY: ${{ github.repository }} | |
| run: | | |
| # The renderer emits the marker as the first line of every comment it | |
| # writes, so read it back rather than repeating the literal here. | |
| export MARKER | |
| MARKER=$(head -n 1 "$RUNNER_TEMP/paired-benchmark-comment.md") | |
| mapfile -t comment_ids < <( | |
| gh api --paginate "repos/$REPOSITORY/issues/$PR_NUMBER/comments" \ | |
| --jq '.[] | select(.user.login == "github-actions[bot]") | select(.body | contains(env.MARKER)) | .id' | |
| ) | |
| jq -Rs '{body: .}' "$RUNNER_TEMP/paired-benchmark-comment.md" > "$RUNNER_TEMP/comment.json" | |
| if [[ ${#comment_ids[@]} -eq 0 ]]; then | |
| gh api "repos/$REPOSITORY/issues/$PR_NUMBER/comments" \ | |
| --method POST --input "$RUNNER_TEMP/comment.json" | |
| else | |
| gh api "repos/$REPOSITORY/issues/comments/${comment_ids[0]}" \ | |
| --method PATCH --input "$RUNNER_TEMP/comment.json" | |
| for duplicate_id in "${comment_ids[@]:1}"; do | |
| gh api "repos/$REPOSITORY/issues/comments/$duplicate_id" --method DELETE | |
| done | |
| fi |