|
| 1 | +name: SizeComment |
| 2 | + |
| 3 | +# spell-checker:ignore zizmor backquote |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_run: |
| 7 | + workflows: ["make"] |
| 8 | + types: |
| 9 | + - completed # zizmor: ignore[dangerous-triggers] |
| 10 | + |
| 11 | +permissions: {} |
| 12 | +jobs: |
| 13 | + post-comment: |
| 14 | + permissions: |
| 15 | + actions: read # to list workflow runs artifacts |
| 16 | + pull-requests: write # to comment on pr |
| 17 | + |
| 18 | + runs-on: ubuntu-latest |
| 19 | + if: > |
| 20 | + github.event.workflow_run.event == 'pull_request' |
| 21 | + steps: |
| 22 | + - name: 'Download artifact' |
| 23 | + uses: actions/github-script@v9 |
| 24 | + with: |
| 25 | + script: | |
| 26 | + // List all artifacts from the make workflow run |
| 27 | + var artifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 28 | + owner: context.repo.owner, |
| 29 | + repo: context.repo.repo, |
| 30 | + run_id: ${{ github.event.workflow_run.id }}, |
| 31 | + }); |
| 32 | +
|
| 33 | + // Download the "size-comment" artifact, which contains a PR |
| 34 | + // number (NR) and result.txt produced by compare_size_results.py. |
| 35 | + var matchArtifact = artifacts.data.artifacts.filter((artifact) => { |
| 36 | + return artifact.name == "size-comment" |
| 37 | + })[0]; |
| 38 | + if (!matchArtifact) { |
| 39 | + core.info("No size-comment artifact found; nothing to post."); |
| 40 | + return; |
| 41 | + } |
| 42 | + var download = await github.rest.actions.downloadArtifact({ |
| 43 | + owner: context.repo.owner, |
| 44 | + repo: context.repo.repo, |
| 45 | + artifact_id: matchArtifact.id, |
| 46 | + archive_format: 'zip', |
| 47 | + }); |
| 48 | + var fs = require('fs'); |
| 49 | + fs.writeFileSync('${{ github.workspace }}/size-comment.zip', Buffer.from(download.data)); |
| 50 | + - name: 'Unzip artifact' |
| 51 | + run: | |
| 52 | + if [ -f size-comment.zip ]; then |
| 53 | + unzip size-comment.zip |
| 54 | + fi |
| 55 | +
|
| 56 | + - name: 'Comment on PR' |
| 57 | + uses: actions/github-script@v9 |
| 58 | + with: |
| 59 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 60 | + script: | |
| 61 | + var fs = require('fs'); |
| 62 | + if (!fs.existsSync('./NR') || !fs.existsSync('./result.txt')) { |
| 63 | + core.info("No size comment payload to post."); |
| 64 | + return; |
| 65 | + } |
| 66 | + var issue_number = Number(fs.readFileSync('./NR')); |
| 67 | + if (!issue_number) { |
| 68 | + core.info("No PR number; skipping."); |
| 69 | + return; |
| 70 | + } |
| 71 | + var content = fs.readFileSync('./result.txt'); |
| 72 | + // Only comment when there is something meaningful to say. |
| 73 | + // compare_size_results.py only writes the file when there is a |
| 74 | + // significant change, so an empty/short body is treated as |
| 75 | + // "nothing to report". |
| 76 | + if (content.toString().trim().length > 0) { |
| 77 | + await github.rest.issues.createComment({ |
| 78 | + owner: context.repo.owner, |
| 79 | + repo: context.repo.repo, |
| 80 | + issue_number: issue_number, |
| 81 | + body: 'Binary size comparison:\n```\n' + content + '```' |
| 82 | + }); |
| 83 | + } |
0 commit comments