|
| 1 | +name: PR automation |
| 2 | +on: |
| 3 | + pull_request_target: |
| 4 | + types: [opened, reopened, edited, ready_for_review] |
| 5 | + |
| 6 | +jobs: |
| 7 | + labeler: |
| 8 | + runs-on: ubuntu-latest |
| 9 | + steps: |
| 10 | + - name: Label PR |
| 11 | + uses: actions/github-script@v5 |
| 12 | + with: |
| 13 | + github-token: ${{ secrets.PAT }} |
| 14 | + script: | |
| 15 | + const { owner, repo, number: issue_number } = context.issue; |
| 16 | + const pr = await github.rest.pulls.get({ owner, repo, pull_number: issue_number }); |
| 17 | + const title = pr.data.title; |
| 18 | +
|
| 19 | + const titleRegex = /^\[LAB(\d+)\] [a-zA-Z]?\d+$/; |
| 20 | + const match = title.match(titleRegex); |
| 21 | +
|
| 22 | + let labNumberStr = undefined; |
| 23 | + if (match) { |
| 24 | + labNumberStr = match[1]; |
| 25 | + } else { |
| 26 | + core.setFailed('PR title does not match the required format. Please use the format: [LAB#] <studentId>.'); |
| 27 | + } |
| 28 | +
|
| 29 | + const labelToAdd = `lab${labNumberStr}`; |
| 30 | + if (labNumberStr) { |
| 31 | + await github.rest.issues.addLabels({ owner, repo, issue_number, labels: [labelToAdd] }); |
| 32 | + } |
| 33 | +
|
| 34 | + if (pr.data.base.ref === 'main') { |
| 35 | + core.setFailed('The target branch cannot be main.'); |
| 36 | + } |
| 37 | +
|
| 38 | + if (labNumberStr < 3 && pr.data.head.ref !== pr.data.base.ref) { |
| 39 | + core.setFailed('The source branch and target branch must be the same.'); |
| 40 | + } |
| 41 | + if (labNumberStr >= 3 && pr.data.head.ref !== labelToAdd) { |
| 42 | + core.setFailed(`The source branch must be '${labelToAdd}'`); |
| 43 | + } |
| 44 | + checklist-check: |
| 45 | + runs-on: ubuntu-latest |
| 46 | + steps: |
| 47 | + - name: Check PR description |
| 48 | + uses: actions/github-script@v5 |
| 49 | + with: |
| 50 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 51 | + script: | |
| 52 | + const { owner, repo, number: issue_number } = context.issue; |
| 53 | + const pr = await github.rest.pulls.get({ owner, repo, pull_number: issue_number }); |
| 54 | + const body = pr.data.body; |
| 55 | +
|
| 56 | + const checkboxes = body.match(/^ ?(-|\*) \[[x ]\]/gmi); |
| 57 | + if (!checkboxes || checkboxes.length !== 5) { |
| 58 | + core.setFailed('The PR description must contain exactly 5 checkboxes.'); |
| 59 | + } |
| 60 | +
|
| 61 | + const unchecked = body.match(/^ ?(-|\*) \[ \]/gm); |
| 62 | + if (unchecked && unchecked.length > 0) { |
| 63 | + core.setFailed(`There are ${unchecked.length} unchecked item(s) in the PR description.`); |
| 64 | + } |
0 commit comments