Skip to content

Commit 6e9f03d

Browse files
authored
Merge pull request #1101 from kuepe-sl/add-collaborator-permission
add "permission" parameter to repo.add_collaborator
2 parents c4b1ee9 + a419969 commit 6e9f03d

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

AUTHORS.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,5 @@ Contributors
216216
- Philipp Heil (@zkdev)
217217

218218
- Petter Kvalvaag (@pettermk)
219+
220+
- Peter Küffner (@kuepe-sl)

src/github3/repos/repo.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,18 @@ def _create_pull(self, data):
118118
return self._instance_or_null(pulls.ShortPullRequest, json)
119119

120120
@decorators.requires_auth
121-
def add_collaborator(self, username):
121+
def add_collaborator(self, username, permission=None):
122122
"""Add ``username`` as a collaborator to a repository.
123123
124124
:param username:
125125
(required), username of the user
126126
:type username:
127127
str or :class:`~github3.users.User`
128+
:param str permission:
129+
(optional), permission to grant the collaborator, valid on
130+
organization repositories only.
131+
Can be 'pull', 'triage', 'push', 'maintain', 'admin' or an
132+
organization-defined custom role name.
128133
:returns:
129134
True if successful, False otherwise
130135
:rtype:
@@ -134,7 +139,12 @@ def add_collaborator(self, username):
134139
url = self._build_url(
135140
"collaborators", str(username), base_url=self._api
136141
)
137-
return self._boolean(self._put(url), 201, 404)
142+
if permission:
143+
data = {"permission": permission}
144+
resp = self._put(url, data=jsonlib.dumps(data))
145+
else:
146+
resp = self._put(url)
147+
return self._boolean(resp, 201, 404)
138148

139149
def archive(self, format, path="", ref="master"):
140150
"""Get the tarball or zipball archive for this repo at ref.

tests/unit/test_repos_repo.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ def test_add_collaborator(self):
8181
url_for("collaborators/sigmavirus24")
8282
)
8383

84+
def test_add_collaborator_with_permission(self):
85+
"""Verify the request to add a collaborator to a repository with
86+
`admin` permission."""
87+
self.instance.add_collaborator("sigmavirus24", "admin")
88+
89+
self.session.put.assert_called_once_with(
90+
url_for("collaborators/sigmavirus24"),
91+
data='{"permission": "admin"}',
92+
)
93+
8494
def test_add_null_collaborator(self):
8595
"""Verify no request is made when adding `None` as a collaborator."""
8696
self.instance.add_collaborator(None)

0 commit comments

Comments
 (0)