|
| 1 | +name: Benchmark comment |
| 2 | + |
| 3 | +# runs in this repository with its own token once a Benchmark run of a pull request is done, |
| 4 | +# so the comment can be written also for a pull request from a fork. Nothing from the pull |
| 5 | +# request is checked out or run here, only the summary it produced is read |
| 6 | +on: |
| 7 | + workflow_run: |
| 8 | + workflows: [Benchmark] |
| 9 | + types: [completed] |
| 10 | + |
| 11 | +permissions: {} |
| 12 | + |
| 13 | +jobs: |
| 14 | + comment: |
| 15 | + if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' |
| 16 | + timeout-minutes: 5 |
| 17 | + runs-on: ubuntu-latest |
| 18 | + permissions: |
| 19 | + actions: read |
| 20 | + pull-requests: write |
| 21 | + steps: |
| 22 | + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 |
| 23 | + with: |
| 24 | + name: benchmark-summary |
| 25 | + run-id: ${{ github.event.workflow_run.id }} |
| 26 | + github-token: ${{ github.token }} |
| 27 | + - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 28 | + with: |
| 29 | + script: | |
| 30 | + const fs = require('node:fs') |
| 31 | + const marker = '<!-- benchmark-comment -->' |
| 32 | + // the summary comes from the pull request, so it is cut to what a comment can hold |
| 33 | + const body = fs.readFileSync('benchmark_summary.md', 'utf8').slice(0, 60000) |
| 34 | + const run = context.payload.workflow_run |
| 35 | + // the pull request comes from the event, not from the artifact, so a branch cannot |
| 36 | + // pick where its table is posted |
| 37 | + const { data: pulls } = await github.rest.pulls.list({ |
| 38 | + owner: context.repo.owner, |
| 39 | + repo: context.repo.repo, |
| 40 | + state: 'open', |
| 41 | + head: `${run.head_repository.owner.login}:${run.head_branch}` |
| 42 | + }) |
| 43 | + const pull = pulls.find((p) => p.head.sha === run.head_sha) |
| 44 | + if (!pull) { |
| 45 | + core.info(`no open pull request at ${run.head_sha}, nothing to comment`) |
| 46 | + return |
| 47 | + } |
| 48 | + const issue_number = pull.number |
| 49 | + const comments = await github.paginate(github.rest.issues.listComments, { |
| 50 | + owner: context.repo.owner, |
| 51 | + repo: context.repo.repo, |
| 52 | + issue_number |
| 53 | + }) |
| 54 | + const existing = comments.find((c) => c.user?.type === 'Bot' && c.body?.includes(marker)) |
| 55 | + if (existing) { |
| 56 | + await github.rest.issues.updateComment({ owner: context.repo.owner, repo: context.repo.repo, comment_id: existing.id, body }) |
| 57 | + } else { |
| 58 | + await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, issue_number, body }) |
| 59 | + } |
0 commit comments