|
| 1 | +name: Enforce Draft PR |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, reopened] |
| 6 | + |
| 7 | +jobs: |
| 8 | + enforce-draft: |
| 9 | + name: Enforce Draft PR |
| 10 | + runs-on: ubuntu-24.04 |
| 11 | + if: github.event.pull_request.draft == false |
| 12 | + steps: |
| 13 | + - name: Generate GitHub App token |
| 14 | + id: app-token |
| 15 | + uses: actions/create-github-app-token@f8d387b68d61c58ab83c6c016672934102569859 # v2 |
| 16 | + with: |
| 17 | + app-id: ${{ vars.SDK_MAINTAINER_BOT_APP_ID }} |
| 18 | + private-key: ${{ secrets.SDK_MAINTAINER_BOT_PRIVATE_KEY }} |
| 19 | + |
| 20 | + - name: Convert PR to draft |
| 21 | + env: |
| 22 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 23 | + PR_URL: ${{ github.event.pull_request.html_url }} |
| 24 | + run: | |
| 25 | + gh pr ready "$PR_URL" --undo |
| 26 | +
|
| 27 | + - name: Label and comment |
| 28 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 29 | + with: |
| 30 | + github-token: ${{ steps.app-token.outputs.token }} |
| 31 | + script: | |
| 32 | + const pullRequest = context.payload.pull_request; |
| 33 | + const repo = context.repo; |
| 34 | +
|
| 35 | + // Label the PR so maintainers can filter/track violations |
| 36 | + await github.rest.issues.addLabels({ |
| 37 | + ...repo, |
| 38 | + issue_number: pullRequest.number, |
| 39 | + labels: ['converted-to-draft'], |
| 40 | + }); |
| 41 | +
|
| 42 | + // Check for existing bot comment to avoid duplicates on reopen |
| 43 | + const comments = await github.rest.issues.listComments({ |
| 44 | + ...repo, |
| 45 | + issue_number: pullRequest.number, |
| 46 | + }); |
| 47 | + const botComment = comments.data.find(c => |
| 48 | + c.user.type === 'Bot' && |
| 49 | + c.body.includes('automatically converted to draft') |
| 50 | + ); |
| 51 | + if (botComment) { |
| 52 | + core.info('Bot comment already exists, skipping.'); |
| 53 | + return; |
| 54 | + } |
| 55 | +
|
| 56 | + const contributingUrl = `https://github.com/${repo.owner}/${repo.repo}/blob/master/CONTRIBUTING.md`; |
| 57 | +
|
| 58 | + await github.rest.issues.createComment({ |
| 59 | + ...repo, |
| 60 | + issue_number: pullRequest.number, |
| 61 | + body: [ |
| 62 | + `This PR has been automatically converted to draft. All PRs must start as drafts per our [contributing guidelines](${contributingUrl}).`, |
| 63 | + '', |
| 64 | + '**Next steps:**', |
| 65 | + '1. Ensure CI passes', |
| 66 | + '2. Fill in the PR description completely', |
| 67 | + '3. Mark as "Ready for review" when you\'re done' |
| 68 | + ].join('\n') |
| 69 | + }); |
0 commit comments