|
| 1 | +# Ref: https://securitylab.github.com/research/github-actions-preventing-pwn-requests |
| 2 | +name: Comment on the pull request |
| 3 | + |
| 4 | +# read-write repo token |
| 5 | +# access to secrets |
| 6 | +on: |
| 7 | + workflow_run: |
| 8 | + workflows: ["Breakage"] |
| 9 | + types: |
| 10 | + - completed |
| 11 | + |
| 12 | +jobs: |
| 13 | + upload: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + if: > |
| 16 | + github.event.workflow_run.event == 'pull_request' && |
| 17 | + github.event.workflow_run.conclusion == 'success' |
| 18 | + steps: |
| 19 | + - name: 'Download artifact' |
| 20 | + |
| 21 | + with: |
| 22 | + script: | |
| 23 | + var artifacts = await github.actions.listWorkflowRunArtifacts({ |
| 24 | + owner: context.repo.owner, |
| 25 | + repo: context.repo.repo, |
| 26 | + run_id: ${{github.event.workflow_run.id }}, |
| 27 | + }); |
| 28 | + var matchArtifact = artifacts.data.artifacts.filter((artifact) => { |
| 29 | + return artifact.name == "pr" |
| 30 | + })[0]; |
| 31 | + var download = await github.actions.downloadArtifact({ |
| 32 | + owner: context.repo.owner, |
| 33 | + repo: context.repo.repo, |
| 34 | + artifact_id: matchArtifact.id, |
| 35 | + archive_format: 'zip', |
| 36 | + }); |
| 37 | + var fs = require('fs'); |
| 38 | + fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data)); |
| 39 | + - run: unzip pr.zip |
| 40 | + |
| 41 | + - name: 'Comment on PR' |
| 42 | + uses: actions/github-script@v6 |
| 43 | + with: |
| 44 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 45 | + script: | |
| 46 | + var fs = require('fs') |
| 47 | + var issue_number = Number(fs.readFileSync('./NR')) |
| 48 | + var msg = fs.readFileSync('./MSG', 'utf8') |
| 49 | +
|
| 50 | + // Get the existing comments. |
| 51 | + const {data: comments} = await github.rest.issues.listComments({ |
| 52 | + owner: context.repo.owner, |
| 53 | + repo: context.repo.repo, |
| 54 | + issue_number: issue_number |
| 55 | + }) |
| 56 | +
|
| 57 | + // Find any comment already made by the bot. |
| 58 | + const botComment = comments.find(comment => comment.user.id === 41898282) |
| 59 | +
|
| 60 | + if (botComment) { |
| 61 | + await github.rest.issues.updateComment({ |
| 62 | + owner: context.repo.owner, |
| 63 | + repo: context.repo.repo, |
| 64 | + comment_id: botComment.id, |
| 65 | + body: msg |
| 66 | + }) |
| 67 | + } else { |
| 68 | + await github.rest.issues.createComment({ |
| 69 | + owner: context.repo.owner, |
| 70 | + repo: context.repo.repo, |
| 71 | + issue_number: issue_number, |
| 72 | + body: msg |
| 73 | + }) |
| 74 | + } |
0 commit comments