Skip to content

Commit 33b1984

Browse files
authored
Improved support for commenting when course timings change. (google#1797)
This * Works with the GitHub permissions model (at least in local testing on another temporary repo) * Only comments when the timings actually change
1 parent 7636276 commit 33b1984

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
uses: actions/[email protected]
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+
uses: actions/[email protected]
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Based on https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
2+
name: Generate Course Schedule
3+
4+
on:
5+
pull_request:
6+
paths:
7+
- "src/**.md"
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
17+
- name: Setup Rust cache
18+
uses: ./.github/workflows/setup-rust-cache
19+
20+
- name: Generate Schedule
21+
run: |
22+
mkdir -p ./course-schedule
23+
cargo run -p mdbook-course --bin course-schedule > course-schedule/schedule
24+
25+
# GitHub does not provide a reliable way to determine the PR number from which
26+
# a workflow_run was triggered (https://github.com/orgs/community/discussions/25220),
27+
# so we'll do the slightly awkward thing and put that in the artifact. This means
28+
# schedules could potentially be spammed to any PR in the repository, but that is
29+
# not an awful outcome (and clear, reportable evidence of abuse).
30+
echo ${{ github.event.number }} > ./course-schedule/pr-number
31+
32+
- uses: actions/upload-artifact@v4
33+
with:
34+
name: course-schedule
35+
path: course-schedule/

0 commit comments

Comments
 (0)