-
Notifications
You must be signed in to change notification settings - Fork 624
ci+docs: Add draft PR enforcement #5867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| name: Enforce Draft PR | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| types: [opened, reopened] | ||
|
|
||
| permissions: | ||
| pull-requests: write | ||
|
|
||
| jobs: | ||
| enforce-draft: | ||
| name: Enforce Draft PR | ||
| runs-on: ubuntu-24.04 | ||
| if: github.event.pull_request.draft == false | ||
| steps: | ||
| - name: Convert PR to draft | ||
| uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | ||
| with: | ||
| script: | | ||
| const pullRequest = context.payload.pull_request; | ||
| const repo = context.repo; | ||
|
|
||
| // Convert to draft via GraphQL (REST API doesn't support this) | ||
| try { | ||
| await github.graphql(` | ||
| mutation($pullRequestId: ID!) { | ||
| convertPullRequestToDraft(input: { pullRequestId: $pullRequestId }) { | ||
| pullRequest { | ||
| isDraft | ||
| } | ||
| } | ||
| } | ||
| `, { | ||
| pullRequestId: pullRequest.node_id | ||
| }); | ||
| } catch (error) { | ||
| core.warning(`Failed to convert PR to draft: ${error.message}`); | ||
| return; | ||
| } | ||
|
|
||
| // Label the PR so maintainers can filter/track violations | ||
| await github.rest.issues.addLabels({ | ||
| ...repo, | ||
| issue_number: pullRequest.number, | ||
| labels: ['converted-to-draft'], | ||
| }); | ||
|
|
||
| // Check for existing bot comment to avoid duplicates on reopen | ||
| const comments = await github.rest.issues.listComments({ | ||
| ...repo, | ||
| issue_number: pullRequest.number, | ||
| }); | ||
| const botComment = comments.data.find(c => | ||
| c.user.type === 'Bot' && | ||
| c.body.includes('automatically converted to draft') | ||
| ); | ||
| if (botComment) { | ||
| core.info('Bot comment already exists, skipping.'); | ||
| return; | ||
| } | ||
|
|
||
| const contributingUrl = `https://github.com/${repo.owner}/${repo.repo}/blob/master/CONTRIBUTING.md`; | ||
|
|
||
| await github.rest.issues.createComment({ | ||
| ...repo, | ||
| issue_number: pullRequest.number, | ||
| body: [ | ||
| `This PR has been automatically converted to draft. All PRs must start as drafts per our [contributing guidelines](${contributingUrl}).`, | ||
| '', | ||
| '**Next steps:**', | ||
| '1. Ensure CI passes', | ||
| '2. Fill in the PR description completely', | ||
| '3. Mark as "Ready for review" when you\'re done' | ||
| ].join('\n') | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The
addLabelscall lacks error handling. If it fails, the workflow will terminate silently without posting its explanatory comment.Severity: MEDIUM
Suggested Fix
Wrap the
addLabelscall in atry/catchblock to handle potential errors gracefully. This ensures that the script can continue execution or log a meaningful error, preventing a silent failure. Consider also programmatically ensuring the label exists or documenting it as a prerequisite.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.