Post benchmark comment #36
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: Post benchmark comment | |
| on: | |
| workflow_run: | |
| workflows: ["build"] | |
| types: [completed] | |
| permissions: | |
| issues: write | |
| pull-requests: write | |
| jobs: | |
| post-comment: | |
| if: github.event.workflow_run.event == 'pull_request' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Download comparison artifact | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| run_id: context.payload.workflow_run.id, | |
| }); | |
| const match = artifacts.data.artifacts.find(a => a.name === 'benchmark-comparison'); | |
| if (!match) { | |
| core.info('No benchmark-comparison artifact; nothing to post.'); | |
| return; | |
| } | |
| const download = await github.rest.actions.downloadArtifact({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| artifact_id: match.id, | |
| archive_format: 'zip', | |
| }); | |
| fs.writeFileSync('artifact.zip', Buffer.from(download.data)); | |
| core.exportVariable('HAS_ARTIFACT', '1'); | |
| - name: Unzip | |
| if: env.HAS_ARTIFACT == '1' | |
| run: unzip -o artifact.zip | |
| - name: Post comment | |
| if: env.HAS_ARTIFACT == '1' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const output = fs.readFileSync('comparison.txt', 'utf8'); | |
| const pr_number = parseInt(fs.readFileSync('pr_number.txt', 'utf8').trim(), 10); | |
| if (!Number.isFinite(pr_number)) { | |
| core.setFailed('Could not parse PR number from artifact.'); | |
| return; | |
| } | |
| const marker = '<!-- benchmark-bot -->'; | |
| const body = `${marker}\n## Benchmark Results\n\`\`\`\n${output.trimEnd()}\n\`\`\``; | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, repo: context.repo.repo, | |
| issue_number: pr_number, | |
| }); | |
| const existing = comments.find(c => c.body.startsWith(marker)); | |
| if (existing) { | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, repo: context.repo.repo, | |
| comment_id: existing.id, body, | |
| }); | |
| } else { | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, repo: context.repo.repo, | |
| issue_number: pr_number, body, | |
| }); | |
| } |