|
| 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 | + const labRegex = /\[LAB(\d+)\]/; |
| 19 | + const titleRegex = /^\[LAB\d+\] [\da-zA-Z]+$/; |
| 20 | +
|
| 21 | + if (!titleRegex.test(title)) { |
| 22 | + core.setFailed('PR title does not match the required format. Please use the format [LAB#] student#.'); |
| 23 | + } |
| 24 | +
|
| 25 | + if (pr.data.head.ref !== pr.data.base.ref) { |
| 26 | + core.setFailed('The source branch and target branch must be the same.'); |
| 27 | + } |
| 28 | +
|
| 29 | + if (pr.data.base.ref === 'main') { |
| 30 | + core.setFailed('The target branch cannot be main.'); |
| 31 | + } |
| 32 | +
|
| 33 | + const match = title.match(labRegex); |
| 34 | + if (match) { |
| 35 | + const labelToAdd = 'lab' + match[1]; |
| 36 | + await github.rest.issues.addLabels({ owner, repo, issue_number, labels: [labelToAdd] }); |
| 37 | + } else { |
| 38 | + core.setFailed('No match found in PR title. Please add a label in the format [LAB#] to the PR title.'); |
| 39 | + } |
| 40 | + checklist-check: |
| 41 | + runs-on: ubuntu-latest |
| 42 | + steps: |
| 43 | + - name: Check PR description |
| 44 | + uses: actions/github-script@v5 |
| 45 | + with: |
| 46 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 47 | + script: | |
| 48 | + const { owner, repo, number: issue_number } = context.issue; |
| 49 | + const pr = await github.rest.pulls.get({ owner, repo, pull_number: issue_number }); |
| 50 | + const body = pr.data.body; |
| 51 | +
|
| 52 | + const checkboxes = body.match(/\- \[[x ]\]/g); |
| 53 | + if (!checkboxes || checkboxes.length !== 5) { |
| 54 | + core.setFailed('The PR description must contain exactly 5 checkboxes.'); |
| 55 | + } |
| 56 | +
|
| 57 | + const unchecked = body.match(/\- \[ \]/g); |
| 58 | + if (unchecked && unchecked.length > 0) { |
| 59 | + core.setFailed(`There are ${unchecked.length} unchecked items in the PR description.`); |
| 60 | + } |
0 commit comments