Skip to content
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

feat: Support updating pull request draft status #3104

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions github/PullRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,56 @@ def update_branch(self, expected_head_sha: Opt[str] = NotSet) -> bool:
)
return status == 202

def convert_to_draft(
self,
client_mutation_id: Opt[str] = NotSet,
) -> dict[str, Any]:
"""
:calls: `POST /graphql <https://docs.github.com/en/graphql>`_ to convert pull request to draft
<https://docs.github.com/en/graphql/reference/mutations#convertpullrequesttodraft>
"""
assert is_optional(client_mutation_id, str), client_mutation_id

# Define the variables
variables = {
"pullRequestId": self.node_id,
"clientMutationId": client_mutation_id,
}

# Make the request
_, data = self._requester.graphql_named_mutation(
mutation_name="convertPullRequestToDraft",
mutation_input=NotSet.remove_unset_items(variables),
output_schema="clientMutationId pullRequest { isDraft }",
)
self._useAttributes({"draft": data["pullRequest"]["isDraft"]})
return data

def mark_ready_for_review(
self,
client_mutation_id: Opt[str] = NotSet,
) -> dict[str, Any]:
"""
:calls: `POST /graphql <https://docs.github.com/en/graphql>`_ to mark pull request ready for review
<https://docs.github.com/en/graphql/reference/mutations#markpullrequestreadyforreview>
"""
assert is_optional(client_mutation_id, str), client_mutation_id

# Define the variables
variables = {
"pullRequestId": self.node_id,
"clientMutationId": client_mutation_id,
}

# Make the request
_, data = self._requester.graphql_named_mutation(
mutation_name="markPullRequestReadyForReview",
mutation_input=NotSet.remove_unset_items(variables),
output_schema="clientMutationId pullRequest { isDraft }",
)
self._useAttributes({"draft": data["pullRequest"]["isDraft"]})
return data

def _useAttributes(self, attributes: dict[str, Any]) -> None:
if "additions" in attributes: # pragma no branch
self._additions = self._makeIntAttribute(attributes["additions"])
Expand Down
53 changes: 53 additions & 0 deletions tests/PullRequest2989.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
############################ Copyrights and license ############################
# #
# Copyright 2025 Bruno Didot <[email protected]> #
# #
# This file is part of PyGithub. #
# http://pygithub.readthedocs.io/ #
# #
# PyGithub is free software: you can redistribute it and/or modify it under #
# the terms of the GNU Lesser General Public License as published by the Free #
# Software Foundation, either version 3 of the License, or (at your option) #
# any later version. #
# #
# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY #
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
# details. #
# #
# You should have received a copy of the GNU Lesser General Public License #
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>. #
# #
################################################################################

from . import Framework


class PullRequest2989(Framework.TestCase):
def setUp(self):
super().setUp()
r = self.g.get_repo("didot/PyGithub", lazy=True)
self.ready_pr = r.get_pull(1)
self.draft_pr = r.get_pull(2)

def testConvertToDraft(self):
self.assertFalse(self.ready_pr.draft)
response = self.ready_pr.convert_to_draft()
self.assertTrue(self.ready_pr.draft)
assert response == {
"clientMutationId": None,
"pullRequest": {
"isDraft": True,
},
}

def testMarkReadyForReview(self):
self.assertTrue(self.draft_pr.draft)
response = self.draft_pr.mark_ready_for_review()
self.assertFalse(self.draft_pr.draft)
assert response == {
"clientMutationId": None,
"pullRequest": {
"isDraft": False,
},
}
Loading