|
| 1 | +name: Type performance comment |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: [Type performance] |
| 6 | + types: [completed] |
| 7 | + |
| 8 | +concurrency: |
| 9 | + group: type-performance-comment-${{ github.event.workflow_run.head_repository.id }}-${{ github.event.workflow_run.head_branch }} |
| 10 | + cancel-in-progress: true |
| 11 | + |
| 12 | +permissions: |
| 13 | + actions: read |
| 14 | + contents: read |
| 15 | + issues: write |
| 16 | + pull-requests: read |
| 17 | + |
| 18 | +jobs: |
| 19 | + comment: |
| 20 | + if: >- |
| 21 | + github.event.workflow_run.event == 'pull_request' && |
| 22 | + github.event.workflow_run.conclusion == 'success' |
| 23 | + runs-on: ubuntu-latest |
| 24 | + steps: |
| 25 | + - name: Check out trusted reporting code |
| 26 | + uses: actions/checkout@v7 |
| 27 | + |
| 28 | + - name: Download performance report |
| 29 | + uses: actions/download-artifact@v8 |
| 30 | + with: |
| 31 | + name: type-performance-report |
| 32 | + path: report |
| 33 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 34 | + run-id: ${{ github.event.workflow_run.id }} |
| 35 | + |
| 36 | + - name: Render report from validated benchmark data |
| 37 | + run: >- |
| 38 | + node scripts/compare-type-performance.mjs |
| 39 | + report/before.json |
| 40 | + report/after.json |
| 41 | + > "$RUNNER_TEMP/report.md" |
| 42 | +
|
| 43 | + - name: Create or update pull request comment |
| 44 | + uses: actions/github-script@v9 |
| 45 | + env: |
| 46 | + REPORT_PATH: ${{ runner.temp }}/report.md |
| 47 | + with: |
| 48 | + script: | |
| 49 | + const fs = require("node:fs") |
| 50 | + const marker = "<!-- effect-machine-type-performance -->" |
| 51 | + const report = fs.readFileSync(process.env.REPORT_PATH, "utf8") |
| 52 | +
|
| 53 | + if (Buffer.byteLength(report, "utf8") > 60_000) { |
| 54 | + core.setFailed("Type-performance report exceeds the safe comment size") |
| 55 | + return |
| 56 | + } |
| 57 | +
|
| 58 | + const run = context.payload.workflow_run |
| 59 | + let pull = run.pull_requests?.[0] |
| 60 | +
|
| 61 | + if (pull === undefined) { |
| 62 | + try { |
| 63 | + const associated = await github.rest.repos.listPullRequestsAssociatedWithCommit({ |
| 64 | + ...context.repo, |
| 65 | + commit_sha: run.head_sha |
| 66 | + }) |
| 67 | + pull = associated.data.find((candidate) => |
| 68 | + candidate.base.repo.full_name === `${context.repo.owner}/${context.repo.repo}` |
| 69 | + ) |
| 70 | + } catch (error) { |
| 71 | + core.warning(`Unable to look up a pull request for ${run.head_sha}: ${error.message}`) |
| 72 | + } |
| 73 | + } |
| 74 | +
|
| 75 | + if (pull === undefined) { |
| 76 | + core.notice("No pull request is associated with this workflow run") |
| 77 | + return |
| 78 | + } |
| 79 | +
|
| 80 | + const body = `${marker}\n${report}` |
| 81 | + const comments = await github.paginate(github.rest.issues.listComments, { |
| 82 | + ...context.repo, |
| 83 | + issue_number: pull.number, |
| 84 | + per_page: 100 |
| 85 | + }) |
| 86 | + const previous = comments.find((comment) => |
| 87 | + comment.user?.type === "Bot" && comment.body?.startsWith(marker) |
| 88 | + ) |
| 89 | +
|
| 90 | + if (previous === undefined) { |
| 91 | + await github.rest.issues.createComment({ |
| 92 | + ...context.repo, |
| 93 | + issue_number: pull.number, |
| 94 | + body |
| 95 | + }) |
| 96 | + } else { |
| 97 | + await github.rest.issues.updateComment({ |
| 98 | + ...context.repo, |
| 99 | + comment_id: previous.id, |
| 100 | + body |
| 101 | + }) |
| 102 | + } |
0 commit comments