name: SizeComment # spell-checker:ignore zizmor backquote on: workflow_run: workflows: ["make"] types: - completed # zizmor: ignore[dangerous-triggers] permissions: {} jobs: post-comment: permissions: actions: read # to list workflow runs artifacts pull-requests: write # to comment on pr runs-on: ubuntu-latest if: > github.event.workflow_run.event == 'pull_request' steps: - name: 'Download artifact' uses: actions/github-script@v9 with: script: | // List all artifacts from the make workflow run var artifacts = await github.rest.actions.listWorkflowRunArtifacts({ owner: context.repo.owner, repo: context.repo.repo, run_id: ${{ github.event.workflow_run.id }}, }); // Download the "size-comment" artifact, which contains a PR // number (NR) and result.txt produced by compare_size_results.py. var matchArtifact = artifacts.data.artifacts.filter((artifact) => { return artifact.name == "size-comment" })[0]; if (!matchArtifact) { core.info("No size-comment artifact found; nothing to post."); return; } var download = await github.rest.actions.downloadArtifact({ owner: context.repo.owner, repo: context.repo.repo, artifact_id: matchArtifact.id, archive_format: 'zip', }); var fs = require('fs'); fs.writeFileSync('${{ github.workspace }}/size-comment.zip', Buffer.from(download.data)); - name: 'Unzip artifact' run: | if [ -f size-comment.zip ]; then unzip size-comment.zip fi - name: 'Comment on PR' uses: actions/github-script@v9 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | const fs = require('fs'); if (!fs.existsSync('./NR') || !fs.existsSync('./result.txt')) { core.info("No size comment payload to post."); return; } const issue_number = Number(fs.readFileSync('./NR')); if (!issue_number) { core.info("No PR number; skipping."); return; } const content = fs.readFileSync('./result.txt').toString(); const marker = ''; const body = `${marker}\nBinary size comparison:\n\`\`\`\n${content}\`\`\``; const { data: comments } = await github.rest.issues.listComments({ ...context.repo, issue_number }); const existing = comments.find(c => c.user.login === 'github-actions[bot]' && c.body.includes(marker) ); if (existing) { await github.rest.issues.updateComment({ ...context.repo, comment_id: existing.id, body }); } else { // Only comment when there is something meaningful to say. // compare_size_results.py only writes the file when there is a // significant change, so an empty/short body is treated as // "nothing to report". if (content.trim().length == 0) { return; } await github.rest.issues.createComment({ ...context.repo, issue_number, body }); }