|
| 1 | +name: Auto-close empty PRs |
| 2 | + |
| 3 | +permissions: |
| 4 | + issues: write |
| 5 | + pull-requests: write |
| 6 | + |
| 7 | +on: |
| 8 | + pull_request_target: |
| 9 | + # 只在新建、重新打开、草稿转正式时检查 PR 说明 |
| 10 | + types: |
| 11 | + - opened |
| 12 | + - reopened |
| 13 | + - ready_for_review |
| 14 | + |
| 15 | +jobs: |
| 16 | + check-pr: |
| 17 | + name: Close PRs with empty or short descriptions |
| 18 | + runs-on: ubuntu-latest |
| 19 | + # 草稿 PR 先不处理,等作者准备好再检查 |
| 20 | + if: ${{ !github.event.pull_request.draft }} |
| 21 | + |
| 22 | + steps: |
| 23 | + - name: Close PR when description is empty or too short |
| 24 | + uses: actions/github-script@v7 |
| 25 | + with: |
| 26 | + script: | |
| 27 | + const pr = context.payload.pull_request; |
| 28 | +
|
| 29 | + // 去掉空白和 HTML 注释,避免“看起来写了,实际上没写内容” |
| 30 | + const description = (pr.body || '') |
| 31 | + .replace(/<!--[\s\S]*?-->/g, ' ') |
| 32 | + .replace(/\s+/g, ' ') |
| 33 | + .trim(); |
| 34 | +
|
| 35 | + const descriptionLength = Array.from(description).length; |
| 36 | + core.info(`Description length: ${descriptionLength}`); |
| 37 | +
|
| 38 | + // 说明超过 8 个字符,就视为已经写了基本内容 |
| 39 | + if (descriptionLength > 8) { |
| 40 | + core.info('Description is long enough. No action needed.'); |
| 41 | + return; |
| 42 | + } |
| 43 | +
|
| 44 | + // 说明为空或太短,先留言提醒,再自动关闭 PR |
| 45 | + await github.rest.issues.createComment({ |
| 46 | + owner: context.repo.owner, |
| 47 | + repo: context.repo.repo, |
| 48 | + issue_number: pr.number, |
| 49 | + body: [ |
| 50 | + '🤖 这个 PR 因为说明为空,或说明过短,已被自动关闭。', |
| 51 | + '', |
| 52 | + '请补充这次改了什么、为什么要改,再重新提交 PR。', |
| 53 | + ].join('\n'), |
| 54 | + }); |
| 55 | +
|
| 56 | + await github.rest.pulls.update({ |
| 57 | + owner: context.repo.owner, |
| 58 | + repo: context.repo.repo, |
| 59 | + pull_number: pr.number, |
| 60 | + state: 'closed', |
| 61 | + }); |
0 commit comments