From 1c3bd4ca8165c20078fb9adae0620babbb960879 Mon Sep 17 00:00:00 2001 From: Aaditya Srinivasan Date: Fri, 10 Jul 2026 16:38:27 +0530 Subject: [PATCH] GH-50379: Convert invalid PRs to draft automatically --- .github/workflows/dev_pr.yml | 3 ++- .github/workflows/dev_pr/title_check.js | 30 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/.github/workflows/dev_pr.yml b/.github/workflows/dev_pr.yml index b8667f4dd52f..ce7f08fb8c0f 100644 --- a/.github/workflows/dev_pr.yml +++ b/.github/workflows/dev_pr.yml @@ -34,7 +34,8 @@ concurrency: cancel-in-progress: true permissions: - contents: read + # Required for the GraphQL convertPullRequestToDraft mutation. + contents: write pull-requests: write issues: write diff --git a/.github/workflows/dev_pr/title_check.js b/.github/workflows/dev_pr/title_check.js index 709a71428584..a986c8bc5f03 100644 --- a/.github/workflows/dev_pr/title_check.js +++ b/.github/workflows/dev_pr/title_check.js @@ -38,11 +38,41 @@ async function commentOpenGitHubIssue(github, context, pullRequestNumber) { }); } +/** + * Converts the pull request to draft. + * + * @param {Object} github + * @param {Object} context + */ +async function convertToDraft(github, context) { + if (context.payload.pull_request.draft) { + return; + } + + await github.graphql( + ` + mutation ($pullRequestId: ID!) { + convertPullRequestToDraft( + input: {pullRequestId: $pullRequestId} + ) { + pullRequest { + id + } + } + } + `, + { + pullRequestId: context.payload.pull_request.node_id, + }, + ); +} + module.exports = async ({github, context}) => { const pullRequestNumber = context.payload.number; const title = context.payload.pull_request.title; const issue = helpers.detectIssue(title) if (!issue) { await commentOpenGitHubIssue(github, context, pullRequestNumber); + await convertToDraft(github, context); } };