|
| 1 | +# Based on https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ |
| 2 | +# |
| 3 | +# This runs with the full repo permissions, but checks out the upstream branch, not the PR. |
| 4 | +# Do not run arbitrary code from the PR here! |
| 5 | +name: Comment PR with Course Schedule |
| 6 | +on: |
| 7 | + workflow_run: |
| 8 | + workflows: ["Generate Course Schedule"] |
| 9 | + types: [completed] |
| 10 | + |
| 11 | +jobs: |
| 12 | + upload: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + if: > |
| 15 | + github.event.workflow_run.event == 'pull_request' && |
| 16 | + github.event.workflow_run.conclusion == 'success' |
| 17 | + steps: |
| 18 | + - name: "Checkout" |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: "Setup Rust cache" |
| 22 | + uses: ./.github/workflows/setup-rust-cache |
| 23 | + |
| 24 | + - name: "Generate Schedule on upstream branch" |
| 25 | + run: | |
| 26 | + cargo run -p mdbook-course --bin course-schedule > upstream-schedule |
| 27 | +
|
| 28 | + - name: "Download artifact from PR workflow" |
| 29 | + # actions/download-artifact@v4 cannot do this without being given a PAT, although that |
| 30 | + # is not required for public forked repositories. |
| 31 | + |
| 32 | + with: |
| 33 | + script: | |
| 34 | + var artifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 35 | + owner: context.repo.owner, |
| 36 | + repo: context.repo.repo, |
| 37 | + run_id: ${{github.event.workflow_run.id }}, |
| 38 | + }); |
| 39 | + var matchArtifact = artifacts.data.artifacts.filter((artifact) => { |
| 40 | + return artifact.name == "course-schedule" |
| 41 | + })[0]; |
| 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}}/course-schedule.zip', Buffer.from(download.data)); |
| 50 | +
|
| 51 | + - name: "Unzip artifact" |
| 52 | + run: unzip course-schedule.zip |
| 53 | + |
| 54 | + - name: "Comment on PR if schedules differ" |
| 55 | + |
| 56 | + with: |
| 57 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 58 | + script: | |
| 59 | + var fs = require('fs'); |
| 60 | + var pr_number = Number(fs.readFileSync('pr-number')); |
| 61 | + var upstream = fs.readFileSync('upstream-schedule').toString(); |
| 62 | + var schedule = fs.readFileSync('schedule').toString(); |
| 63 | + if (upstream != schedule) { |
| 64 | + await github.rest.issues.createComment({ |
| 65 | + owner: context.repo.owner, |
| 66 | + repo: context.repo.repo, |
| 67 | + issue_number: pr_number, |
| 68 | + body: schedule, |
| 69 | + }); |
| 70 | + } |
0 commit comments