|
| 1 | +name: PR Template Enforcer |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, edited, reopened] |
| 6 | + workflow_dispatch: # Manually trigger to scan ALL open PRs |
| 7 | + |
| 8 | +permissions: |
| 9 | + issues: write |
| 10 | + pull-requests: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + enforce-template: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Check PR body against template structure |
| 17 | + uses: actions/github-script@v7 |
| 18 | + with: |
| 19 | + script: | |
| 20 | + const label = 'needs-details'; |
| 21 | +
|
| 22 | + // Build the list of PRs to check: |
| 23 | + // - workflow_dispatch: scan all open PRs |
| 24 | + // - pull_request_target: check only the triggering PR |
| 25 | + let prsToCheck; |
| 26 | + if (context.eventName === 'workflow_dispatch') { |
| 27 | + const { data } = await github.rest.pulls.list({ |
| 28 | + owner: context.repo.owner, |
| 29 | + repo: context.repo.repo, |
| 30 | + state: 'open', |
| 31 | + per_page: 100, |
| 32 | + }); |
| 33 | + prsToCheck = data; |
| 34 | + } else { |
| 35 | + prsToCheck = [context.payload.pull_request]; |
| 36 | + } |
| 37 | +
|
| 38 | + const body_template = pr => pr.body ?? ''; |
| 39 | +
|
| 40 | + // Required section headings from .github/pull_request_template.md |
| 41 | + const requiredSections = [ |
| 42 | + '## Description', |
| 43 | + '## Pillar', |
| 44 | + '## Checklist', |
| 45 | + ]; |
| 46 | +
|
| 47 | + // Minimum character threshold — catches "fixed issue" type bodies |
| 48 | + const MIN_LENGTH = 80; |
| 49 | +
|
| 50 | + // Ensure the label exists once before the loop |
| 51 | + try { |
| 52 | + await github.rest.issues.getLabel({ |
| 53 | + owner: context.repo.owner, |
| 54 | + repo: context.repo.repo, |
| 55 | + name: label, |
| 56 | + }); |
| 57 | + } catch { |
| 58 | + await github.rest.issues.createLabel({ |
| 59 | + owner: context.repo.owner, |
| 60 | + repo: context.repo.repo, |
| 61 | + name: label, |
| 62 | + color: 'f97316', |
| 63 | + description: 'This PR is missing required description details.', |
| 64 | + }); |
| 65 | + } |
| 66 | +
|
| 67 | + for (const pr of prsToCheck) { |
| 68 | + const body = body_template(pr); |
| 69 | +
|
| 70 | + const missingSection = requiredSections.find( |
| 71 | + section => !body.toLowerCase().includes(section.toLowerCase()) |
| 72 | + ); |
| 73 | + const isTooShort = body.trim().length < MIN_LENGTH; |
| 74 | + const needsAction = missingSection || isTooShort; |
| 75 | +
|
| 76 | + const currentLabels = pr.labels.map(l => l.name); |
| 77 | + const alreadyLabeled = currentLabels.includes(label); |
| 78 | +
|
| 79 | + if (needsAction) { |
| 80 | + // Apply label |
| 81 | + if (!alreadyLabeled) { |
| 82 | + await github.rest.issues.addLabels({ |
| 83 | + owner: context.repo.owner, |
| 84 | + repo: context.repo.repo, |
| 85 | + issue_number: pr.number, |
| 86 | + labels: [label], |
| 87 | + }); |
| 88 | + } |
| 89 | +
|
| 90 | + // Check for an existing enforcement comment to avoid duplicates |
| 91 | + const { data: comments } = await github.rest.issues.listComments({ |
| 92 | + owner: context.repo.owner, |
| 93 | + repo: context.repo.repo, |
| 94 | + issue_number: pr.number, |
| 95 | + per_page: 100, |
| 96 | + }); |
| 97 | +
|
| 98 | + const alreadyCommented = comments.some(c => |
| 99 | + c.user?.login === 'github-actions[bot]' && |
| 100 | + c.body?.includes("didn't use our PR template") |
| 101 | + ); |
| 102 | +
|
| 103 | + if (!alreadyCommented) { |
| 104 | + const reason = missingSection |
| 105 | + ? `The section **\`${missingSection}\`** is missing from your PR description.` |
| 106 | + : `Your PR description is too short (${body.trim().length} characters — minimum is ${MIN_LENGTH}).`; |
| 107 | +
|
| 108 | + await github.rest.issues.createComment({ |
| 109 | + owner: context.repo.owner, |
| 110 | + repo: context.repo.repo, |
| 111 | + issue_number: pr.number, |
| 112 | + body: `👋 Hey @${pr.user.login}, it looks like you didn't use our PR template! |
| 113 | +
|
| 114 | + ${reason} |
| 115 | +
|
| 116 | + Please update your PR description to include all required sections so we can review this properly: |
| 117 | + - **\`## Description\`** — What does this PR do? Which issue does it fix? |
| 118 | + - **\`## Pillar\`** — Which contribution pillar does this fall under? |
| 119 | + - **\`## Checklist\`** — Have you ticked off the quality checklist? |
| 120 | +
|
| 121 | + You can find the full template in [CONTRIBUTING.md](https://github.com/${context.repo.owner}/${context.repo.repo}/blob/main/CONTRIBUTING.md#-opening-a-pull-request). Just edit your PR description and the \`needs-details\` label will be removed automatically. 🙌`, |
| 122 | + }); |
| 123 | + } |
| 124 | + } else if (!needsAction && alreadyLabeled) { |
| 125 | + // PR has been updated and now meets the requirements — remove the label |
| 126 | + await github.rest.issues.removeLabel({ |
| 127 | + owner: context.repo.owner, |
| 128 | + repo: context.repo.repo, |
| 129 | + issue_number: pr.number, |
| 130 | + name: label, |
| 131 | + }); |
| 132 | + } |
| 133 | + } |
0 commit comments