Skip to content

Commit

Permalink
Get branches where commit is head (PyGithub#3083)
Browse files Browse the repository at this point in the history
Implements `GET
/repos/{owner}/{repo}/commits/{commit_sha}/branches-where-head`

https://docs.github.com/rest/commits/commits#list-branches-for-head-commit
  • Loading branch information
EnricoMi authored Dec 18, 2024
1 parent a50ae51 commit 3d84a47
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
9 changes: 9 additions & 0 deletions github/Commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

from typing import TYPE_CHECKING, Any

import github.Branch
import github.CheckRun
import github.CheckSuite
import github.CommitCombinedStatus
Expand All @@ -61,6 +62,7 @@
from github.PaginatedList import PaginatedList

if TYPE_CHECKING:
from github.Branch import Branch
from github.CheckRun import CheckRun
from github.CheckSuite import CheckSuite
from github.CommitCombinedStatus import CommitCombinedStatus
Expand Down Expand Up @@ -206,6 +208,13 @@ def create_status(
)
return github.CommitStatus.CommitStatus(self._requester, headers, data)

def get_branches_where_head(self) -> list[Branch]:
"""
:calls: `GET /repos/{owner}/{repo}/commits/{commit_sha}/branches-where-head <https://docs.github.com/rest/commits/commits#list-branches-for-head-commit>`_
"""
headers, data = self._requester.requestJsonAndCheck("GET", f"{self.url}/branches-where-head")
return [github.Branch.Branch(self._requester, headers, item) for item in data]

def get_comments(self) -> PaginatedList[CommitComment]:
"""
:calls: `GET /repos/{owner}/{repo}/commits/{sha}/comments <https://docs.github.com/en/rest/reference/repos#comments>`_
Expand Down
9 changes: 9 additions & 0 deletions tests/Commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ def testAttributes(self):
'Commit(sha="1292bf0e22c796e91cc3d6e24b544aece8c21f2a")',
)

def testGetBranchesWhereHead(self):
repo = self.g.get_repo("PyGithub/PyGithub")
commit = repo.get_commit("0791cc7b1a706ab5d7c607ddff35de4d486ba3e9")
self.assertListKeyEqual(
commit.get_branches_where_head(),
lambda b: b.name,
["release-v2-0"],
)

def testGetComments(self):
self.assertListKeyEqual(
self.commit.get_comments(),
Expand Down
Loading

0 comments on commit 3d84a47

Please sign in to comment.