Add Repository.rename_branch method (#2089)

The GitHub API exposes an endpoint to rename a branch, so we should
support calling it. Sadly, there is not enough information to add
that method to the Branch class, so expose it in the Repository object.

Fixes #1901
This commit is contained in:
Steve Kowalik
2021-10-24 15:21:31 +11:00
committed by GitHub
parent c8a945bb42
commit 6452ddfeb4
5 changed files with 64 additions and 0 deletions
+21
View File
@@ -1644,6 +1644,27 @@ class Repository(github.GithubObject.CompletableGithubObject):
)
return github.Branch.Branch(self._requester, headers, data, completed=True)
def rename_branch(self, branch, new_name):
"""
:calls: `POST /repos/{owner}/{repo}/branches/{branch}/rename <https://docs.github.com/en/rest/reference/repos#branches>`
:param branch: :class:`github.Branch.Branch` or string
:param new_name: string
:rtype: bool
NOTE: This method does not return the branch since it may take some
time to fully complete server-side.
"""
is_branch = isinstance(branch, github.Branch.Branch)
assert isinstance(branch, str) or is_branch, branch
assert isinstance(new_name, str), new_name
if is_branch:
branch = branch.name
parameters = {"new_name": new_name}
status, _, _ = self._requester.requestJson(
"POST", f"{self.url}/branches/{branch}/rename", input=parameters
)
return status == 201
def get_branches(self):
"""
:calls: `GET /repos/{owner}/{repo}/branches <https://docs.github.com/en/rest/reference/repos>`_
+3
View File
@@ -307,6 +307,9 @@ class Repository(CompletableGithubObject):
) -> str: ...
def get_assignees(self) -> PaginatedList[NamedUser]: ...
def get_branch(self, branch: str) -> Branch: ...
def rename_branch(
self, branch: Union[str, Branch], new_name: str
) -> bool: ...
def get_branches(self) -> PaginatedList[Branch]: ...
def get_check_run(self, check_run_id: int) -> CheckRun: ...
def get_check_suite(self, check_suite_id: int) -> CheckSuite: ...