|
| 1 | +name: PR Checklist Validation |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, edited, reopened, ready_for_review] |
| 6 | + |
| 7 | +jobs: |
| 8 | + validate-checklist: |
| 9 | + if: github.event.pull_request.draft == false |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - name: Validate PR checklist and sections |
| 13 | + uses: actions/github-script@v7 |
| 14 | + with: |
| 15 | + script: | |
| 16 | + const body = context.payload.pull_request.body || ''; |
| 17 | + const errors = []; |
| 18 | +
|
| 19 | + // --- Check that all checklist items are checked --- |
| 20 | + const uncheckedPattern = /- \[ \]/g; |
| 21 | + const checkedPattern = /- \[x\]/gi; |
| 22 | + const uncheckedMatches = body.match(uncheckedPattern) || []; |
| 23 | + const checkedMatches = body.match(checkedPattern) || []; |
| 24 | + const totalCheckboxes = uncheckedMatches.length + checkedMatches.length; |
| 25 | +
|
| 26 | + if (totalCheckboxes > 0 && uncheckedMatches.length > 0) { |
| 27 | + errors.push( |
| 28 | + `${uncheckedMatches.length} checklist item(s) are unchecked. ` + |
| 29 | + 'All checklist items must be checked before merging — ' + |
| 30 | + 'including items that don\'t apply (check them and note why if needed).' |
| 31 | + ); |
| 32 | + } |
| 33 | +
|
| 34 | + // --- Warn if "Automated Tests" section is empty --- |
| 35 | + const automatedTestsMatch = body.match(/### Automated Tests\s*\n([\s\S]*?)(?=###|$)/); |
| 36 | + const automatedTestsContent = (automatedTestsMatch?.[1] || '') |
| 37 | + .replace(/<!-[\s\S]*?->/g, '') |
| 38 | + .trim(); |
| 39 | +
|
| 40 | + if (automatedTestsContent.length === 0) { |
| 41 | + core.warning( |
| 42 | + 'The "Automated Tests" section is empty. Please describe the automated tests you added, ' + |
| 43 | + 'or explain why automated tests are not needed for this change.' |
| 44 | + ); |
| 45 | + } |
| 46 | +
|
| 47 | + // --- Warn if "Manual Tests" section is empty --- |
| 48 | + const manualTestsMatch = body.match(/### Manual Tests\s*\n([\s\S]*?)(?=###|$)/); |
| 49 | + const manualTestsContent = (manualTestsMatch?.[1] || '') |
| 50 | + .replace(/<!-[\s\S]*?->/g, '') |
| 51 | + .trim(); |
| 52 | +
|
| 53 | + if (manualTestsContent.length === 0) { |
| 54 | + core.warning( |
| 55 | + 'The "Manual Tests" section is empty. Please describe how you manually tested this change.' |
| 56 | + ); |
| 57 | + } |
| 58 | +
|
| 59 | + // --- Warn if GH_LINK placeholder is still present --- |
| 60 | + const relatedIssuesMatch = body.match(/### Related Issues\s*\n([\s\S]*?)(?=###|$)/); |
| 61 | + const relatedIssuesContent = (relatedIssuesMatch?.[1] || '') |
| 62 | + .replace(/<!-[\s\S]*?->/g, '') |
| 63 | + .trim(); |
| 64 | +
|
| 65 | + if (relatedIssuesContent === 'GH_LINK' || relatedIssuesContent.length === 0) { |
| 66 | + core.warning( |
| 67 | + 'The "Related Issues" section still contains the GH_LINK placeholder or is empty. ' + |
| 68 | + 'Please replace it with the actual GitHub issue link.' |
| 69 | + ); |
| 70 | + } |
| 71 | +
|
| 72 | + // --- Fail if there are errors --- |
| 73 | + if (errors.length > 0) { |
| 74 | + const summary = '## PR Checklist Validation Failed\n\n' + |
| 75 | + errors.map((e) => `- ${e}`).join('\n') + |
| 76 | + '\n\nPlease complete the checklist and update the PR description.'; |
| 77 | +
|
| 78 | + core.summary.addRaw(summary); |
| 79 | + await core.summary.write(); |
| 80 | + core.setFailed(errors.join('\n')); |
| 81 | + } |
0 commit comments