8000 Merge pull request #987 from dmitrykiselev27/associated_prs · domdfcoding/github3.py@197173b · GitHub
[go: up one dir, main page]

Skip to content

Commit 197173b

Browse files
authored
Merge pull request sigmavirus24#987 from dmitrykiselev27/associated_prs
add ability to get the list of associated PRs for commit
2 parents 16da0bf + 94d76e3 commit 197173b

File tree

6 files changed

+58
-4
lines changed

6 files changed

+58
-4
lines changed

AUTHORS.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,5 @@ Contributors
198198
- Kevin P. Fleming (@kpfleming)
199199

200200
- Andrew Hayworth (@ahayworth)
201+
202+
- Dmitry Kiselev (@dmitrykiselev27)

src/github3/repos/commit.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class _RepoCommit(models.GitHubCore):
2626
2727
"""
2828

29+
PREVIEW_HEADERS = {
30+
"Accept": "application/vnd.github.groot-preview+json,"
31+
"application/vnd.github.v3.full+json"
32+
}
33+
2934
class_name = "_RepoCommit"
3035

3136
def _update_attributes(self, commit):
@@ -139,6 +144,30 @@ def comments(self, number=-1, etag=None):
139144
url = self._build_url("comments", base_url=self._api)
140145
return self._iter(int(number), url, RepoComment, etag=etag)
141146

147+
def associated_pull_requests(self, number=-1, etag=None):
148+
"""Iterate pull requests associated with a commit.
149+
150+
:param int number:
151+
(optional), number of comments to return. Default: -1 returns all
152+
pull requests
153+
:param str etag:
154+
(optional), ETag from a previous request to the same endpoint
155+
:returns:
156+
generator of pull requests
157+
:rtype:
158+
:class:~github3.pulls.PullRequest`
159+
"""
160+
from .. import pulls
161+
162+
url = self._build_url("pulls", base_url=self._api)
163+
return self._iter(
164+
number,
165+
url,
166+
pulls.ShortPullRequest,
167+
etag=etag,
168+
headers=self.PREVIEW_HEADERS
169+
)
170+
142171

143172
class RepoCommit(_RepoCommit):
144173
"""Representation of a commit with repository and git data."""

tests/cassettes/RepoCommit_associated_pull_requests.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

tests/integration/test_repos_commit.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,14 @@ def test_author_is_not_committer(self):
5555
"6a0470c992dd97d97fa0fee503153b125141ca4c"
5656
)
5757
assert commit.author != commit.committer
58+
59+
def test_associated_pull_requests(self):
60+
"""Test the ability to retrieve associated pull requests of commit."""
61+
cassette_name = self.cassette_name("associated_pull_requests")
62+
with self.recorder.use_cassette(cassette_name):
63+
repository = self.gh.repository("sigmavirus24", "github3.py")
64+
commit = repository.commit(
65+
"6b12e37bdc1bea465f04a53262194ab332711ee8"
66+
)
67+
for pr in commit.associated_pull_requests():
68+
assert isinstance(pr, github3.pulls.ShortPullRequest)

tests/integration/test_repos_repo.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -976,10 +976,10 @@ def test_languages(self):
976976
with self.recorder.use_cassette(cassette_name):
977977
repository = self.gh.repository("sigmavirus24", "github3.py")
978978
assert repository is not None
979-
for l in repository.languages():
980-
assert "ETag" not in l
981-
assert "Last-Modified" not in l
982-
assert isinstance(l, tuple)
979+
for lang in repository.languages():
980+
assert "ETag" not in lang
981+
assert "Last-Modified" not in lang
982+
assert isinstance(lang, tuple)
983983

984984
def test_license(self):
985985
"""Test that a repository's license can be retrieved."""

tests/unit/test_repos_commit.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ def test_comments(self):
3434
url_for("comments"), params={"per_page": 100}, headers={}
3535
)
3636

37+
def test_associated_pull_requests(self):
38+
"""Verify the request to iterate over PRs associated with a commit."""
39+
i = self.instance.associated_pull_requests()
40+
self.get_next(i)
41+
42+
self.session.get.assert_called_once_with(
43+
url_for("comments").replace("comments", "pulls"),
44+
params={"per_page": 100},
45+
headers=github3.repos.commit._RepoCommit.PREVIEW_HEADERS
46+
)
47+
3748

3849
class TestRepoCommitIteratorAppInstAuth(helper.UnitIteratorAppInstHelper):
3950

0 commit comments

Comments
 (0)
0