|
| 1 | +name: PR checklist checker |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, edited, reopened, synchronize] |
| 6 | + |
| 7 | +concurrency: |
| 8 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} |
| 9 | + cancel-in-progress: true |
| 10 | + |
| 11 | +jobs: |
| 12 | + check-checklist: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + |
| 15 | + steps: |
| 16 | + - name: Checkout repository |
| 17 | + uses: actions/checkout@v4 |
| 18 | + |
| 19 | + # Wait 15 seconds so that the job doesn't run immediately when checking off multiple items in the checklist. |
| 20 | + # - name: Wait for 15 seconds |
| 21 | + # run: | |
| 22 | + # sleep 15 |
| 23 | + |
| 24 | + - name: Check checklist in PR description |
| 25 | + id: check_checklist |
| 26 | + run: | |
| 27 | + DESCRIPTION=$(jq -r .pull_request.body < $GITHUB_EVENT_PATH) |
| 28 | + UNCHECKED_ITEMS=$(echo "$DESCRIPTION" | grep -o '.*\[ \].*' || true) |
| 29 | + if [ -z "$UNCHECKED_ITEMS" ]; then |
| 30 | + echo "all_checked=true" >> $GITHUB_ENV |
| 31 | + else |
| 32 | + echo "all_checked=false" >> $GITHUB_ENV |
| 33 | + echo "unchecked_items<<EOF" >> $GITHUB_ENV |
| 34 | + echo "$UNCHECKED_ITEMS" >> $GITHUB_ENV |
| 35 | + echo "EOF" >> $GITHUB_ENV |
| 36 | + fi |
| 37 | +
|
| 38 | + - name: Debug PR body |
| 39 | + run: | |
| 40 | + curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ |
| 41 | + https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }} | jq -r '.body' |
| 42 | +
|
| 43 | + - name: Leave a comment |
| 44 | + if: env.all_checked == 'true' |
| 45 | + uses: actions/github-script@v6 |
| 46 | + with: |
| 47 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 48 | + script: | |
| 49 | + github.rest.issues.createComment({ |
| 50 | + issue_number: context.issue.number, |
| 51 | + owner: context.repo.owner, |
| 52 | + repo: context.repo.repo, |
| 53 | + body: "All items in the checklist have been checked🎉" |
| 54 | + }) |
| 55 | +
|
| 56 | + - name: Leave a comment with unchecked items |
| 57 | + if: env.all_checked == 'false' |
| 58 | + uses: actions/github-script@v6 |
| 59 | + with: |
| 60 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 61 | + script: | |
| 62 | + const { data: pullRequest } = await github.rest.pulls.get({ |
| 63 | + owner: context.repo.owner, |
| 64 | + repo: context.repo.repo, |
| 65 | + pull_number: context.issue.number |
| 66 | + }); |
| 67 | + const assignee = pullRequest.assignee ? `@${pullRequest.assignee.login}` : 'No assignee'; |
| 68 | + const uncheckedItems = process.env.unchecked_items.split('\n').map(item => `- ${item}`).join('\n'); |
| 69 | + const prLink = `https://github.com/${context.repo.owner}/${context.repo.repo}/pull/${context.issue.number}`; |
| 70 | + github.rest.issues.createComment({ |
| 71 | + issue_number: context.issue.number, |
| 72 | + owner: context.repo.owner, |
| 73 | + repo: context.repo.repo, |
| 74 | + body: `${assignee} Not all items in the checklist have been checked👀\n\nPlease review the unchecked items in the [PR description](${prLink}#checklist).` |
| 75 | + }) |
| 76 | +
|
| 77 | + - name: Fail the job if not all items are checked |
| 78 | + if: env.all_checked == 'false' |
| 79 | + run: | |
| 80 | + echo "Failing the job because not all items in the checklist have been checked." |
| 81 | + exit 1 |
0 commit comments