|
| 1 | +name: Code size comment |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: [Check code size] |
| 6 | + types: [completed] |
| 7 | + |
| 8 | +concurrency: |
| 9 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 10 | + cancel-in-progress: true |
| 11 | + |
| 12 | +jobs: |
| 13 | + comment: |
| 14 | + runs-on: ubuntu-20.04 |
| 15 | + steps: |
| 16 | + - name: 'Download artifact' |
| 17 | + id: download-artifact |
| 18 | + uses: actions/github-script@v6 |
| 19 | + with: |
| 20 | + result-encoding: string |
| 21 | + script: | |
| 22 | + const fs = require('fs'); |
| 23 | +
|
| 24 | + const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 25 | + owner: context.repo.owner, |
| 26 | + repo: context.repo.repo, |
| 27 | + run_id: context.payload.workflow_run.id, |
| 28 | + }); |
| 29 | +
|
| 30 | + const matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { |
| 31 | + return artifact.name == "code-size-report" |
| 32 | + }); |
| 33 | +
|
| 34 | + if (matchArtifact.length === 0) { |
| 35 | + console.log('no matching artifact found'); |
| 36 | + console.log('result: "skip"'); |
| 37 | +
|
| 38 | + return 'skip'; |
| 39 | + } |
| 40 | +
|
| 41 | + const download = await github.rest.actions.downloadArtifact({ |
| 42 | + owner: context.repo.owner, |
| 43 | + repo: context.repo.repo, |
| 44 | + artifact_id: matchArtifact[0].id, |
| 45 | + archive_format: 'zip', |
| 46 | + }); |
| 47 | +
|
| 48 | + fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/code-size-report.zip`, Buffer.from(download.data)); |
| 49 | +
|
| 50 | + console.log('artifact downloaded to `code-size-report.zip`'); |
| 51 | + console.log('result: "ok"'); |
| 52 | +
|
| 53 | + return 'ok'; |
| 54 | + - name: 'Unzip artifact' |
| 55 | + if: steps.download-artifact.outputs.result == 'ok' |
| 56 | + run: unzip code-size-report.zip |
| 57 | + - name: Post comment to pull request |
| 58 | + if: steps.download-artifact.outputs.result == 'ok' |
| 59 | + uses: actions/github-script@v6 |
| 60 | + with: |
| 61 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 62 | + script: | |
| 63 | + const fs = require('fs'); |
| 64 | +
|
| 65 | + const prNumber = Number(fs.readFileSync('pr_number')); |
| 66 | + const codeSizeReport = `Code size report: |
| 67 | +
|
| 68 | + \`\`\` |
| 69 | + ${fs.readFileSync('diff')} |
| 70 | + \`\`\` |
| 71 | + `; |
| 72 | +
|
| 73 | + const comments = await github.paginate( |
| 74 | + github.rest.issues.listComments, |
| 75 | + { |
| 76 | + owner: context.repo.owner, |
| 77 | + repo: context.repo.repo, |
| 78 | + issue_number: prNumber, |
| 79 | + } |
| 80 | + ); |
| 81 | +
|
| 82 | + comments.reverse(); |
| 83 | +
|
| 84 | + const previousComment = comments.find(comment => |
| 85 | + comment.user.login === 'github-actions[bot]' |
| 86 | + ) |
| 87 | +
|
| 88 | + // if github-actions[bot] already made a comment, update it, |
| 89 | + // otherwise create a new comment. |
| 90 | +
|
| 91 | + if (previousComment) { |
| 92 | + await github.rest.issues.updateComment({ |
| 93 | + owner: context.repo.owner, |
| 94 | + repo: context.repo.repo, |
| 95 | + comment_id: previousComment.id, |
| 96 | + body: codeSizeReport, |
| 97 | + }); |
| 98 | + } else { |
| 99 | + await github.rest.issues.createComment({ |
| 100 | + owner: context.repo.owner, |
| 101 | + repo: context.repo.repo, |
| 102 | + issue_number: prNumber, |
| 103 | + body: codeSizeReport, |
| 104 | + }); |
| 105 | + } |
0 commit comments