|
| 1 | +name: PR Checklist Validation |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, edited, synchronize, reopened] |
| 6 | + branches: |
| 7 | + - main |
| 8 | + - dev |
| 9 | + |
| 10 | +permissions: |
| 11 | + pull-requests: write |
| 12 | + issues: write |
| 13 | + contents: read |
| 14 | + |
| 15 | +jobs: |
| 16 | + validate-checklist: |
| 17 | + name: PR Checklist |
| 18 | + runs-on: ubuntu-latest |
| 19 | + |
| 20 | + steps: |
| 21 | + # ----------------------------------------------------------------- |
| 22 | + # Parse the PR body, validate required checkboxes, manage labels, |
| 23 | + # and post/update a single sticky comment with the result. |
| 24 | + # ----------------------------------------------------------------- |
| 25 | + - name: Validate checklist and apply labels |
| 26 | + uses: actions/github-script@v7 |
| 27 | + with: |
| 28 | + script: | |
| 29 | + const body = context.payload.pull_request.body || ''; |
| 30 | + const title = context.payload.pull_request.title || ''; |
| 31 | + const prNumber = context.payload.pull_request.number; |
| 32 | + const author = context.payload.pull_request.user.login; |
| 33 | + const existingLabels = context.payload.pull_request.labels.map(l => l.name); |
| 34 | +
|
| 35 | + // ── Helpers ───────────────────────────────────────────────────────── |
| 36 | +
|
| 37 | + /** |
| 38 | + * Extract the content of a Markdown section identified by its heading |
| 39 | + * prefix (e.g. "## Testing" or "### Code quality"). Stops as soon as |
| 40 | + * it encounters another heading at the same level or higher. |
| 41 | + */ |
| 42 | + function extractSection(text, headingPrefix) { |
| 43 | + const lines = text.split('\n'); |
| 44 | + const headingLevel = (headingPrefix.match(/^(#+)/) || ['', '#'])[1].length; |
| 45 | + let capturing = false; |
| 46 | + const result = []; |
| 47 | +
|
| 48 | + for (const line of lines) { |
| 49 | + if (!capturing && line.startsWith(headingPrefix)) { |
| 50 | + capturing = true; |
| 51 | + result.push(line); |
| 52 | + } else if (capturing) { |
| 53 | + const m = line.match(/^(#+)\s/); |
| 54 | + if (m && m[1].length <= headingLevel) break; |
| 55 | + result.push(line); |
| 56 | + } |
| 57 | + } |
| 58 | + return result.join('\n'); |
| 59 | + } |
| 60 | +
|
| 61 | + /** True if the section contains at least one `- [x]` line. */ |
| 62 | + function atLeastOneChecked(section) { |
| 63 | + return /^- \[x\]/im.test(section); |
| 64 | + } |
| 65 | +
|
| 66 | + /** True if every checkbox in the section is `- [x]` (none are `- [ ]`). */ |
| 67 | + function allChecked(section) { |
| 68 | + return atLeastOneChecked(section) && !/^- \[ \]/im.test(section); |
| 69 | + } |
| 70 | +
|
| 71 | + // ── Extract relevant sections ──────────────────────────────────────── |
| 72 | +
|
| 73 | + const typeSection = extractSection(body, '## Type of change'); |
| 74 | + const packagesSection = extractSection(body, '### Packages'); |
| 75 | + const codeQuality = extractSection(body, '### Code quality'); |
| 76 | + const testingSection = extractSection(body, '## Testing'); |
| 77 | + const breakingSection = extractSection(body, '## Breaking changes'); |
| 78 | + const reviewerSection = extractSection(body, '## Reviewer notes'); |
| 79 | +
|
| 80 | + // ── Validation rules ───────────────────────────────────────────────── |
| 81 | +
|
| 82 | + const errors = []; |
| 83 | +
|
| 84 | + // PR title must follow [Type] Brief description |
| 85 | + if (!/^\[(Feature|Fix|Docs|Refactor|Chore|Test)\] .+/.test(title)) { |
| 86 | + errors.push( |
| 87 | + '**PR title** must follow `[Type] Brief description` ' + |
| 88 | + '— valid types: `Feature` · `Fix` · `Docs` · `Refactor` · `Chore` · `Test`' |
| 89 | + ); |
| 90 | + } |
| 91 | +
|
| 92 | + // Type of change — at least one |
| 93 | + if (!atLeastOneChecked(typeSection)) { |
| 94 | + errors.push('**Type of change** — select at least one option'); |
| 95 | + } |
| 96 | +
|
| 97 | + // Packages — at least one (including "Not applicable") |
| 98 | + if (!atLeastOneChecked(packagesSection)) { |
| 99 | + errors.push( |
| 100 | + '**Packages** — select at least one option ' + |
| 101 | + '(tick "Not applicable" if no package changes were made)' |
| 102 | + ); |
| 103 | + } |
| 104 | +
|
| 105 | + // Code quality — all boxes must be ticked |
| 106 | + if (!allChecked(codeQuality)) { |
| 107 | + errors.push('**Code quality** — all checkboxes must be ticked before merging'); |
| 108 | + } |
| 109 | +
|
| 110 | + // Testing — all boxes must be ticked |
| 111 | + if (!allChecked(testingSection)) { |
| 112 | + errors.push('**Testing** — all checkboxes must be ticked before merging'); |
| 113 | + } |
| 114 | +
|
| 115 | + // Breaking changes — at least one option selected |
| 116 | + if (!atLeastOneChecked(breakingSection)) { |
| 117 | + errors.push('**Breaking changes** — select at least one option'); |
| 118 | + } |
| 119 | +
|
| 120 | + // Reviewer notes — all boxes must be ticked |
| 121 | + if (!allChecked(reviewerSection)) { |
| 122 | + errors.push('**Reviewer notes** — all checkboxes must be ticked before merging'); |
| 123 | + } |
| 124 | +
|
| 125 | + // ── Label management ───────────────────────────────────────────────── |
| 126 | +
|
| 127 | + // breaking-change: the "Yes — describe impact" line is checked |
| 128 | + const isBreakingChange = /^- \[x\].*Yes/im.test(breakingSection); |
| 129 | +
|
| 130 | + // package-changed: any @pptb line is checked (i.e. not just "Not applicable") |
| 131 | + const isPackageChanged = /^- \[x\].*@pptb/im.test(packagesSection); |
| 132 | +
|
| 133 | + async function syncLabel(name, shouldHave) { |
| 134 | + const has = existingLabels.includes(name); |
| 135 | + if (shouldHave && !has) { |
| 136 | + await github.rest.issues.addLabels({ |
| 137 | + owner: context.repo.owner, |
| 138 | + repo: context.repo.repo, |
| 139 | + issue_number: prNumber, |
| 140 | + labels: [name], |
| 141 | + }); |
| 142 | + } else if (!shouldHave && has) { |
| 143 | + try { |
| 144 | + await github.rest.issues.removeLabel({ |
| 145 | + owner: context.repo.owner, |
| 146 | + repo: context.repo.repo, |
| 147 | + issue_number: prNumber, |
| 148 | + name, |
| 149 | + }); |
| 150 | + } catch { |
| 151 | + // Label may have already been removed — ignore |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +
|
| 156 | + await syncLabel('breaking-change', isBreakingChange); |
| 157 | + await syncLabel('package-changed', isPackageChanged); |
| 158 | +
|
| 159 | + // ── Sticky comment ─────────────────────────────────────────────────── |
| 160 | + // We post one comment and update it in place on subsequent runs so the |
| 161 | + // PR timeline stays clean. |
| 162 | +
|
| 163 | + const MARKER = '<!-- pr-checklist-bot -->'; |
| 164 | +
|
| 165 | + const { data: comments } = await github.rest.issues.listComments({ |
| 166 | + owner: context.repo.owner, |
| 167 | + repo: context.repo.repo, |
| 168 | + issue_number: prNumber, |
| 169 | + }); |
| 170 | + const existing = comments.find(c => c.body && c.body.includes(MARKER)); |
| 171 | +
|
| 172 | + const labelNote = [ |
| 173 | + isBreakingChange ? '🔴 `breaking-change` label applied' : null, |
| 174 | + isPackageChanged ? '🔵 `package-changed` label applied' : null, |
| 175 | + ].filter(Boolean).join('\n'); |
| 176 | +
|
| 177 | + let commentBody; |
| 178 | +
|
| 179 | + const errorList = errors.map(e => `- ${e}`).join('\n'); |
| 180 | +
|
| 181 | + if (errors.length === 0) { |
| 182 | + commentBody = |
| 183 | + `${MARKER}\n` + |
| 184 | + `### ✅ PR Checklist\n\n` + |
| 185 | + `All required checklist items are complete. This PR is ready for review.\n` + |
| 186 | + (labelNote ? `\n${labelNote}` : ''); |
| 187 | + } else { |
| 188 | + // On updates we omit the @mention to avoid re-notifying on every push. |
| 189 | + const mention = existing ? '' : `@${author} — please address the items below.\n\n`; |
| 190 | + commentBody = |
| 191 | + `${MARKER}\n` + |
| 192 | + `### ❌ PR Checklist — Action Required\n\n` + |
| 193 | + `${mention}` + |
| 194 | + `The following items must be completed before this PR can be merged:\n\n` + |
| 195 | + `${errorList}\n` + |
| 196 | + (labelNote ? `\n---\n${labelNote}` : ''); |
| 197 | + } |
| 198 | +
|
| 199 | + if (existing) { |
| 200 | + await github.rest.issues.updateComment({ |
| 201 | + owner: context.repo.owner, |
| 202 | + repo: context.repo.repo, |
| 203 | + comment_id: existing.id, |
| 204 | + body: commentBody, |
| 205 | + }); |
| 206 | + } else { |
| 207 | + await github.rest.issues.createComment({ |
| 208 | + owner: context.repo.owner, |
| 209 | + repo: context.repo.repo, |
| 210 | + issue_number: prNumber, |
| 211 | + body: commentBody, |
| 212 | + }); |
| 213 | + } |
| 214 | +
|
| 215 | + // ── Fail the status check ──────────────────────────────────────────── |
| 216 | +
|
| 217 | + if (errors.length > 0) { |
| 218 | + core.setFailed( |
| 219 | + `PR checklist incomplete — ${errors.length} item${errors.length > 1 ? 's' : ''} outstanding:\n` + |
| 220 | + errors.map(e => ` • ${e.replace(/\*\*/g, '')}`).join('\n') |
| 221 | + ); |
| 222 | + } |
0 commit comments