|
| 1 | +name: Check PR template completeness |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, reopened, edited] |
| 6 | + schedule: |
| 7 | + - cron: '0 8 * * *' # every day at 08:00 UTC |
| 8 | + |
| 9 | +permissions: |
| 10 | + pull-requests: write |
| 11 | + issues: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + # ── 1. On every PR open/edit: check that the template was filled in ────────── |
| 15 | + check-template: |
| 16 | + if: ${{ github.event_name == 'pull_request_target' }} |
| 17 | + runs-on: ubuntu-latest |
| 18 | + steps: |
| 19 | + - name: Ensure needs-information label exists |
| 20 | + uses: actions/github-script@v7 |
| 21 | + with: |
| 22 | + script: | |
| 23 | + try { |
| 24 | + await github.rest.issues.createLabel({ |
| 25 | + owner: context.repo.owner, |
| 26 | + repo: context.repo.repo, |
| 27 | + name: 'needs-information', |
| 28 | + color: 'e4e669', |
| 29 | + description: 'More information is needed before this can be reviewed', |
| 30 | + }); |
| 31 | + } catch (e) { |
| 32 | + if (e.status !== 422) throw e; // 422 = already exists, ignore |
| 33 | + } |
| 34 | +
|
| 35 | + - name: Detect incomplete template |
| 36 | + uses: actions/github-script@v7 |
| 37 | + with: |
| 38 | + script: | |
| 39 | + const { data: pr } = await github.rest.pulls.get({ |
| 40 | + owner: context.repo.owner, |
| 41 | + repo: context.repo.repo, |
| 42 | + pull_number: context.payload.pull_request.number, |
| 43 | + }); |
| 44 | +
|
| 45 | + const body = pr.body || ''; |
| 46 | +
|
| 47 | + function isIncomplete(body) { |
| 48 | + if (body.trim().length < 30) return true; |
| 49 | + // Template HTML comment still present → body was never edited |
| 50 | + if (body.includes('<!-- Please describe your change')) return true; |
| 51 | + // The placeholder issue reference was not replaced |
| 52 | + if (/Fixes\s*#\s*\(issue\)/.test(body)) return true; |
| 53 | + // All checklist items still unchecked |
| 54 | + const unchecked = (body.match(/- \[ \]/g) || []).length; |
| 55 | + const checked = (body.match(/- \[x\]/gi) || []).length; |
| 56 | + if (unchecked >= 3 && checked === 0) return true; |
| 57 | + return false; |
| 58 | + } |
| 59 | +
|
| 60 | + const labels = pr.labels.map(l => l.name); |
| 61 | + const alreadyFlagged = labels.includes('needs-information'); |
| 62 | +
|
| 63 | + if (isIncomplete(body)) { |
| 64 | + if (!alreadyFlagged) { |
| 65 | + await github.rest.issues.addLabels({ |
| 66 | + owner: context.repo.owner, |
| 67 | + repo: context.repo.repo, |
| 68 | + issue_number: pr.number, |
| 69 | + labels: ['needs-information'], |
| 70 | + }); |
| 71 | +
|
| 72 | + await github.rest.issues.createComment({ |
| 73 | + owner: context.repo.owner, |
| 74 | + repo: context.repo.repo, |
| 75 | + issue_number: pr.number, |
| 76 | + body: [ |
| 77 | + `## 📋 Please complete the PR template, @${pr.user.login}`, |
| 78 | + '', |
| 79 | + 'It looks like the Pull Request description is **empty or hasn\'t been filled in** yet.', |
| 80 | + '', |
| 81 | + 'To help maintainers review your contribution, please:', |
| 82 | + '', |
| 83 | + '1. Edit this PR and fill in the description template:', |
| 84 | + ' - Describe **what** your change does and **why**', |
| 85 | + ' - Link the related issue (e.g. `Fixes #123`)', |
| 86 | + ' - Tick the checklist items that apply', |
| 87 | + '', |
| 88 | + '2. Once the template is complete, the `needs-information` label will be removed automatically.', |
| 89 | + '', |
| 90 | + '> **Note:** If this PR is not updated within **7 days**, it will be closed automatically. You are welcome to re-open it once the description is complete.', |
| 91 | + '', |
| 92 | + '_This is an automated message._', |
| 93 | + ].join('\n'), |
| 94 | + }); |
| 95 | + console.log(`PR #${pr.number} flagged as needs-information.`); |
| 96 | + } else { |
| 97 | + console.log(`PR #${pr.number} still incomplete, already flagged.`); |
| 98 | + } |
| 99 | + } else { |
| 100 | + // Template is now complete — remove the label if it was set |
| 101 | + if (alreadyFlagged) { |
| 102 | + await github.rest.issues.removeLabel({ |
| 103 | + owner: context.repo.owner, |
| 104 | + repo: context.repo.repo, |
| 105 | + issue_number: pr.number, |
| 106 | + name: 'needs-information', |
| 107 | + }); |
| 108 | + await github.rest.issues.createComment({ |
| 109 | + owner: context.repo.owner, |
| 110 | + repo: context.repo.repo, |
| 111 | + issue_number: pr.number, |
| 112 | + body: `Thanks for completing the PR template, @${pr.user.login}! The \`needs-information\` label has been removed. A maintainer will review your PR shortly. 🙏`, |
| 113 | + }); |
| 114 | + console.log(`PR #${pr.number} template now complete, label removed.`); |
| 115 | + } else { |
| 116 | + console.log(`PR #${pr.number} template looks complete.`); |
| 117 | + } |
| 118 | + } |
| 119 | +
|
| 120 | + # ── 2. Daily: close PRs still labelled needs-information after 7 days ───────── |
| 121 | + close-stale-incomplete: |
| 122 | + if: ${{ github.event_name == 'schedule' }} |
| 123 | + runs-on: ubuntu-latest |
| 124 | + steps: |
| 125 | + - name: Close PRs with incomplete template after 7 days of inactivity |
| 126 | + uses: actions/github-script@v7 |
| 127 | + with: |
| 128 | + script: | |
| 129 | + const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000); |
| 130 | +
|
| 131 | + // Fetch all open PRs labelled needs-information (paginate if needed) |
| 132 | + const prs = await github.paginate(github.rest.pulls.list, { |
| 133 | + owner: context.repo.owner, |
| 134 | + repo: context.repo.repo, |
| 135 | + state: 'open', |
| 136 | + per_page: 100, |
| 137 | + }); |
| 138 | +
|
| 139 | + for (const pr of prs) { |
| 140 | + const hasLabel = pr.labels.some(l => l.name === 'needs-information'); |
| 141 | + if (!hasLabel) continue; |
| 142 | +
|
| 143 | + const lastUpdate = new Date(pr.updated_at); |
| 144 | + if (lastUpdate >= sevenDaysAgo) { |
| 145 | + console.log(`PR #${pr.number} flagged but still recent (${pr.updated_at}), skipping.`); |
| 146 | + continue; |
| 147 | + } |
| 148 | +
|
| 149 | + console.log(`Closing PR #${pr.number} — no activity for 7+ days with incomplete template.`); |
| 150 | +
|
| 151 | + await github.rest.issues.createComment({ |
| 152 | + owner: context.repo.owner, |
| 153 | + repo: context.repo.repo, |
| 154 | + issue_number: pr.number, |
| 155 | + body: [ |
| 156 | + '## PR closed — template not completed', |
| 157 | + '', |
| 158 | + `Hi @${pr.user.login},`, |
| 159 | + '', |
| 160 | + 'This Pull Request has been **automatically closed** because the description template was not completed within 7 days.', |
| 161 | + '', |
| 162 | + 'You are welcome to **re-open it** once the template is fully filled in. A complete description helps maintainers understand the context and intent of your change.', |
| 163 | + '', |
| 164 | + `See our [CONTRIBUTING guide](https://github.com/${context.repo.owner}/${context.repo.repo}/blob/${context.payload.repository.default_branch}/CONTRIBUTING.md) for details.`, |
| 165 | + '', |
| 166 | + '_This is an automated message._', |
| 167 | + ].join('\n'), |
| 168 | + }); |
| 169 | +
|
| 170 | + await github.rest.pulls.update({ |
| 171 | + owner: context.repo.owner, |
| 172 | + repo: context.repo.repo, |
| 173 | + pull_number: pr.number, |
| 174 | + state: 'closed', |
| 175 | + }); |
| 176 | + } |
0 commit comments