Skip to content

Commit

Permalink
feat: Support updating pull request draft status
Browse files Browse the repository at this point in the history
  • Loading branch information
didot committed Jan 6, 2025
1 parent 3d84a47 commit 9ff3efe
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 0 deletions.
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
70 changes: 70 additions & 0 deletions tests/PullRequest2989.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
############################ Copyrights and license ############################
# #
# Copyright 2012 Vincent Jacques <[email protected]> #
# Copyright 2012 Zearin <[email protected]> #
# Copyright 2013 Vincent Jacques <[email protected]> #
# Copyright 2014 Vincent Jacques <[email protected]> #
# Copyright 2016 Matthew Neal <[email protected]> #
# Copyright 2016 Peter Buckley <[email protected]> #
# Copyright 2016 Sam Corbett <[email protected]> #
# Copyright 2018 sfdye <[email protected]> #
# Copyright 2019 Adam Baratz <[email protected]> #
# Copyright 2019 Olof-Joachim Frahm (欧雅福) <[email protected]> #
# Copyright 2019 Steve Kowalik <[email protected]> #
# Copyright 2019 TechnicalPirate <[email protected]>#
# Copyright 2019 Wan Liuyang <[email protected]> #
# Copyright 2020 Anuj Bansal <[email protected]> #
# Copyright 2020 Steve Kowalik <[email protected]> #
# Copyright 2023 Enrico Minack <[email protected]> #
# Copyright 2023 Jirka Borovec <[email protected]> #
# Copyright 2023 Mikhail f. Shiryaev <[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

0 comments on commit 9ff3efe

Please sign in to comment.