Repository navigation
83 lines (72 loc) · 3.18 KB
/
Copy pathfirst-commit.yml
File metadata and controls
83 lines (72 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
name: Validate first commit
# pull_request_target runs this workflow from the base branch with a write
# token, so a fork's PR can be commented on and, when it passes, merged.
# To stay safe, the PR's
# code is never checked out or executed: the validator comes from the base
# branch and only reads the learner's file with git show + ast.parse.
# PRs that don't touch first-commits/<name>.py (status 3) get no comment.
#
# A PR that passes is merged here, so a learner's first pull request never
# waits for a maintainer. It is safe to merge unread because the validator
# allows exactly one ordinary file, first-commits/<login>.py, holding three
# short strings with no links or mentions. The merge is a merge commit (a
# squash would leave the learner's main out of step with ours), and only of
# the commit that was checked. This is the only bot that merges.
on:
pull_request_target:
types: [opened, synchronize, reopened]
branches: [main]
paths: ["first-commits/**"]
permissions:
contents: write # to merge a pull request that passed
pull-requests: write
concurrency:
group: first-commit-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Check out base branch (trusted code only)
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
persist-credentials: false
fetch-depth: 0
- name: Fetch PR commits without checking them out
env:
PR: ${{ github.event.pull_request.number }}
run: git fetch --no-tags origin "pull/${PR}/head"
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
with:
python-version: "3.12"
- name: Validate
id: check
env:
LOGIN: ${{ github.event.pull_request.user.login }}
BASE: ${{ github.event.pull_request.base.sha }}
HEAD: ${{ github.event.pull_request.head.sha }}
run: |
set +e
python tools/check_first_commit.py --login "$LOGIN" --base "$BASE" --head "$HEAD" > comment.md
echo "status=$?" >> "$GITHUB_OUTPUT"
cat comment.md
- name: Comment on the PR
if: steps.check.outputs.status != '3'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR: ${{ github.event.pull_request.number }}
run: gh pr comment "$PR" --repo "$GITHUB_REPOSITORY" --body-file comment.md
- name: Merge the pull request that passed
if: steps.check.outputs.status == '0'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR: ${{ github.event.pull_request.number }}
HEAD: ${{ github.event.pull_request.head.sha }}
run: |
if ! gh pr merge "$PR" --repo "$GITHUB_REPOSITORY" --merge --match-head-commit "$HEAD"; then
gh pr comment "$PR" --repo "$GITHUB_REPOSITORY" --body \
"I couldn't merge this by myself: the branch may have changed while I was checking, or it has a conflict. A maintainer will take a look."
fi
- name: Fail the check when the file needs fixes
if: steps.check.outputs.status != '0' && steps.check.outputs.status != '3'
run: exit 1