|
| 1 | +name: Check PR Template |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: # zizmor: ignore[dangerous-triggers] |
| 5 | + types: [opened, edited] |
| 6 | + |
| 7 | +permissions: {} |
| 8 | + |
| 9 | +jobs: |
| 10 | + parse: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + if: ${{ github.event.pull_request.head.repo.fork == true }} |
| 13 | + permissions: |
| 14 | + contents: read |
| 15 | + outputs: |
| 16 | + uses_template: ${{ steps.check.outputs.uses_template }} |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 19 | + with: |
| 20 | + sparse-checkout: .github/pull_request_template.md |
| 21 | + sparse-checkout-cone-mode: false |
| 22 | + persist-credentials: false |
| 23 | + |
| 24 | + - name: Check required sections |
| 25 | + id: check |
| 26 | + env: |
| 27 | + BODY: ${{ github.event.pull_request.body }} |
| 28 | + run: | |
| 29 | + OK=true |
| 30 | + while IFS= read -r header; do |
| 31 | + printf '%s\n' "$BODY" | grep -qF "$header" || OK=false |
| 32 | + done < <(grep "^## " .github/pull_request_template.md) |
| 33 | + echo "uses_template=$OK" >> "$GITHUB_OUTPUT" |
| 34 | +
|
| 35 | + act: |
| 36 | + runs-on: ubuntu-latest |
| 37 | + needs: parse |
| 38 | + permissions: |
| 39 | + pull-requests: write |
| 40 | + steps: |
| 41 | + - name: Close PR |
| 42 | + if: ${{ needs.parse.outputs.uses_template == 'false' && github.event.pull_request.state != 'closed' }} |
| 43 | + env: |
| 44 | + GH_TOKEN: ${{ github.token }} |
| 45 | + NODE_ID: ${{ github.event.pull_request.node_id }} |
| 46 | + run: | |
| 47 | + gh api graphql \ |
| 48 | + -f prId="$NODE_ID" \ |
| 49 | + -f body="This PR has been automatically closed as the description doesn't follow our template. After you edit it to match the template, the PR will automatically be reopened." \ |
| 50 | + -f query=' |
| 51 | + mutation CommentAndClosePR($prId: ID!, $body: String!) { |
| 52 | + addComment(input: { |
| 53 | + subjectId: $prId, |
| 54 | + body: $body |
| 55 | + }) { |
| 56 | + __typename |
| 57 | + } |
| 58 | + closePullRequest(input: { |
| 59 | + pullRequestId: $prId |
| 60 | + }) { |
| 61 | + __typename |
| 62 | + } |
| 63 | + }' |
| 64 | +
|
| 65 | + - name: Reopen PR (sections now present, PR closed) |
| 66 | + if: ${{ needs.parse.outputs.uses_template == 'true' && github.event.pull_request.state == 'closed' }} |
| 67 | + env: |
| 68 | + GH_TOKEN: ${{ github.token }} |
| 69 | + NODE_ID: ${{ github.event.pull_request.node_id }} |
| 70 | + run: | |
| 71 | + gh api graphql \ |
| 72 | + -f prId="$NODE_ID" \ |
| 73 | + -f query=' |
| 74 | + mutation ReopenPR($prId: ID!) { |
| 75 | + reopenPullRequest(input: { |
| 76 | + pullRequestId: $prId |
| 77 | + }) { |
| 78 | + __typename |
| 79 | + } |
| 80 | + }' |
0 commit comments