-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improved support for commenting when course timings change. #1797
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Based on https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ | ||
# | ||
# This runs with the full repo permissions, but checks out the upstream branch, not the PR. | ||
# Do not run arbitrary code from the PR here! | ||
name: Comment PR with Course Schedule | ||
on: | ||
workflow_run: | ||
workflows: ["Generate Course Schedule"] | ||
types: [completed] | ||
|
||
jobs: | ||
upload: | ||
runs-on: ubuntu-latest | ||
if: > | ||
github.event.workflow_run.event == 'pull_request' && | ||
github.event.workflow_run.conclusion == 'success' | ||
steps: | ||
- name: "Checkout" | ||
uses: actions/checkout@v4 | ||
|
||
- name: "Setup Rust cache" | ||
uses: ./.github/workflows/setup-rust-cache | ||
|
||
- name: "Generate Schedule on upstream branch" | ||
run: | | ||
cargo run -p mdbook-course --bin course-schedule > upstream-schedule | ||
|
||
- name: "Download artifact from PR workflow" | ||
# actions/download-artifact@v4 cannot do this without being given a PAT, although that | ||
# is not required for public forked repositories. | ||
uses: actions/github-script@v7.0.1 | ||
with: | ||
script: | | ||
var artifacts = await github.rest.actions.listWorkflowRunArtifacts({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
run_id: ${{github.event.workflow_run.id }}, | ||
}); | ||
var matchArtifact = artifacts.data.artifacts.filter((artifact) => { | ||
return artifact.name == "course-schedule" | ||
})[0]; | ||
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}}/course-schedule.zip', Buffer.from(download.data)); | ||
|
||
- name: "Unzip artifact" | ||
run: unzip course-schedule.zip | ||
|
||
- name: "Comment on PR if schedules differ" | ||
uses: actions/github-script@v7.0.1 | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
var fs = require('fs'); | ||
var pr_number = Number(fs.readFileSync('pr-number')); | ||
var upstream = fs.readFileSync('upstream-schedule').toString(); | ||
var schedule = fs.readFileSync('schedule').toString(); | ||
if (upstream != schedule) { | ||
await github.rest.issues.createComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: pr_number, | ||
body: schedule, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Based on https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ | ||
name: Generate Course Schedule | ||
|
||
on: | ||
pull_request: | ||
paths: | ||
- "src/**.md" | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup Rust cache | ||
uses: ./.github/workflows/setup-rust-cache | ||
|
||
- name: Generate Schedule | ||
run: | | ||
mkdir -p ./course-schedule | ||
cargo run -p mdbook-course --bin course-schedule > course-schedule/schedule | ||
|
||
# GitHub does not provide a reliable way to determine the PR number from which | ||
# a workflow_run was triggered (https://github.com/orgs/community/discussions/25220), | ||
# so we'll do the slightly awkward thing and put that in the artifact. This means | ||
# schedules could potentially be spammed to any PR in the repository, but that is | ||
# not an awful outcome (and clear, reportable evidence of abuse). | ||
echo ${{ github.event.number }} > ./course-schedule/pr-number | ||
|
||
- uses: actions/upload-artifact@v4 | ||
with: | ||
name: course-schedule | ||
path: course-schedule/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I think this can be simplified by using download-artifact action. When I was implementing my workflow, I wasn't aware of this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll give it a try! The linked article seemed to say that wouldn't work, but on a closer reading maybe that's not true. Also, it's quite an old article so maybe it works now :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't tried it, so it might indeed be not working :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, it requires a PAT with read access to the source repository, and won't accept the
repository
input parameter without one -- even if that repository might be public.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am ok with not implementing it as well if it is not easy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(It's possible that the script I've used also won't work with a different, even public, repository -- we'll see!)