8000 Added Collaborator, Contributor and Repository.collaborators(affiliat… · AndreasBackx/github3.py@15af1b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 15af1b9

Browse files
committed
Added Collaborator, Contributor and Repository.collaborators(affiliation).
See sigmavirus24#730 for some background information. This still fails 1 test because the cassette Repository_collaborators.json needs to be updated with the permissions as it somehow does not contain that information. The GitHub API does say that it should be there, but the recording is from 2014 so perhaps it wasn't returned back then. Note that I have currently put the named parameter `affiliation` at the front of the arguments in `Repository.collaborators`. I did this to be in line with the other similar iterators. This can be moved to the back, but I assume some backwards-incompatible changes are fine for 1.0.0? This uses separate classes for the specific User object uses as I understood the idea was from sigmavirus24#670.
1 parent c82e90e commit 15af1b9

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

github3/repos/repo.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from uritemplate import URITemplate
1616

1717
from .. import users
18-
1918
from ..decorators import requires_auth
2019
from ..events import Event
2120
from ..git import Blob, Commit, Reference, Tag, Tree
@@ -41,6 +40,7 @@
4140
from .stats import ContributorStats
4241
from .status import Status
4342
from .tag import RepoTag
43+
from .users import Collaborator, Contributor
4444

4545

4646
class Repository(GitHubCore):
@@ -524,17 +524,24 @@ def code_frequency(self, number=-1, etag=None):
524524
url = self._build_url('stats', 'code_frequency', base_url=self._api)
525525
return self._iter(int(number), url, list, etag=etag)
526526

527-
def collaborators(self, number=-1, etag=None):
527+
def collaborators(self, affiliation='all', number=-1, etag=None):
528528
r"""Iterate over the collaborators of this repository.
529529
530530
:param int number: (optional), number of collaborators to return.
531531
Default: -1 returns all comments
532532
:param str etag: (optional), ETag from a previous request to the same
533533
endpoint
534-
:returns: generator of :class:`~github3.users.ShortUser`\ s
534+
:returns: generator of :class:`~github3.repos.users.Collaborator`\ s
535535
"""
536536
url = self._build_url('collaborators', base_url=self._api)
537-
return self._iter(int(number), url, users.ShortUser, etag=etag)
537+
affiliations = {'outside', 'direct', 'all'}
538+
if affiliation not in affiliations:
539+
raise ValueError(
540+
"Invalid affiliation parameter passed, must be 'outside', "
541+
"'direct', or 'all' (default 'all')."
542+
)
543+
params = {'affiliation': affiliation}
544+
return self._iter(int(number), url, Collaborator, params, etag=etag)
538545

539546
def comments(self, number=-1, etag=None):
540547
r"""Iterate over comments on all commits in the repository.
@@ -674,13 +681,13 @@ def contributors(self, anon=False, number=-1, etag=None):
674681
Default: -1 returns all contributors
675682
:param str etag: (optional), ETag from a previous request to the same
676683
endpoint
677-
:returns: generator of :class:`~github3.users.ShortUser`\ s
684+
:returns: generator of :class:`~github3.repos.users.Contributor`\ s
678685
"""
679686
url = self._build_url('contributors', base_url=self._api)
680687
params = {}
681688
if anon:
682689
params = {'anon': 'true'}
683-
return self._iter(int(number), url, users.ShortUser, params, etag)
690+
return self._iter(int(number), url, Contributor, params, etag)
684691

685692
@requires_auth
686693
def create_blob(self, content, encoding):
@@ -1038,7 +1045,7 @@ def create_release(self, tag_name, target_commitish=None, name=None,
10381045
url = self._build_url('releases', base_url=self._api)
10391046
json = self._json(self._post(
10401047
url, data=data, headers=Release.CUSTOM_HEADERS
1041-
), 201)
1048+
), 201)
10421049
return self._instance_or_null(Release, json)
10431050

10441051
@requires_auth

github3/repos/users.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from ..users import _User
2+
3+
4+
class Collaborator(_User):
5+
"""Object for the representation of a collaborator.
6+
7+
This represents a user as a collaborator of a :class:`Repository`.
8+
This adds permission information of the user to the repository.
9+
10+
.. versionadded:: 1.0.0
11+
"""
12+
13+
def _update_attributes(self, user):
14+
super(Collaborator, self)._update_attributes(user)
15+
16+
#: Admin, push, and pull permissions of a user
17+
self.permissions = user['permissions']
18+
19+
20+
class Contributor(_User):
21+
"""Object for the representation of a contributor.
22+
23+
This represents a user as a contributor of a :class:`Repository`.
24+
This adds contribution information of the user to the repository.
25+
26+
.. versionadded:: 1.0.0
27+
"""
28+
29+
def _update_attributes(self, user):
30+
super(Contributor, self)._update_attributes(user)
31+
32+
#: A contributor's amount of contributions
33+
self.contributions = user['contributions']

tests/integration/test_repos_repo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_collaborators(self):
8383
repository = self.gh.repository('sigmavirus24', 'github3.py')
8484
assert repository is not None
8585
for collaborator in repository.collaborators():
86-
assert isinstance(collaborator, github3.users.ShortUser)
86+
assert isinstance(collaborator, github3.repos.users.Collaborator)
8787

8888
def test_comments(self):
8989
"""Test the ability to retrieve comments on a repository."""
@@ -146,7 +146,7 @@ def test_contributors(self):
146146
repository = self.gh.repository('sigmavirus24', 'github3.py')
147147
assert repository is not None
148148
for contributor in repository.contributors():
149-
assert isinstance(contributor, github3.users.ShortUser)
149+
assert isinstance(contributor, github3.repos.users.Contributor)
150150
assert isinstance(contributor.contributions, int)
151151

152152
def test_create_blob(self):

tests/unit/test_repos_repo.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,10 +1042,29 @@ def test_collaborators(self):
10421042

10431043
self.session.get.assert_called_once_with(
10441044
url_for('collaborators'),
1045-
params={'per_page': 100},
1045+
params={'affiliation': 'all', 'per_page': 100},
1046+
headers={}
1047+
)
1048+
1049+
def test_collaborators_valid_affiliation(self):
1050+
"""
1051+
Test the ability to iterate over the collaborators on a repo with a
1052+
specific affiliation.
1053+
"""
1054+
i = self.instance.collaborators(affiliation='direct')
1055+
self.get_next(i)
1056+
1057+
self.session.get.assert_called_once_with(
1058+
url_for('collaborators'),
1059+
params={'affiliation': 'direct', 'per_page': 100},
10461060
headers={}
10471061
)
10481062

1063+
def test_collaborators_invalid_affiliation(self):
1064+
"""Test invalid affiliation requests raise ValueError."""
1065+
with pytest.raises(ValueError):
1066+
self.instance.collaborators(affiliation='invalid')
1067+
10491068
def test_comments(self):
10501069
"""Test the ability to iterate over the comments on a repository."""
10511070
i = self.instance.comments()

0 commit comments

Comments
 (0)
0