Skip to content

Commit e68aa38

Browse files
authored
Merge pull request #1072 from sigmavirus24/improve-teams-api-support
Fixes for changes to the Teams API
2 parents fc41784 + 31e616a commit e68aa38

File tree

12 files changed

+71
-270
lines changed

12 files changed

+71
-270
lines changed

docs/source/release-notes/3.1.0.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
3.1.0: 2022-02-xx
2+
-----------------
3+
4+
Features Added
5+
``````````````
6+
7+
- Add :class:`~github3.orgs.ShortRepositoryWithPermissions` to include the
8+
permissions dictionary returned by the GitHub API when listing repositories
9+
a team has access to.
10+
11+
- Add :meth:`github3.orgs.Team.permissions_for("<org>/<repo>")` to retrieve a
12+
full permissions listing for a given repository in an organization.

docs/source/release-notes/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ here with the newest releases first.
99
==================
1010

1111
.. toctree::
12+
3.1.0
1213
3.0.0
1314

1415
2.x Release Series

src/github3/orgs.py

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@
1212
from .repos import Repository
1313
from .repos import ShortRepository
1414

15+
if t.TYPE_CHECKING:
16+
from . import users as _users
17+
18+
19+
class ShortRepositoryWithPermissions(ShortRepository):
20+
class_name = "ShortRepositoryWithPermissions"
21+
22+
def _update_attributes(self, repo) -> None:
23+
super()._update_attributes(repo)
24+
self.permissions = repo["permissions"]
25+
1526

1627
class _Team(models.GitHubCore):
1728
"""Base class for Team representations."""
@@ -32,6 +43,10 @@ def _update_attributes(self, team):
3243
) # TODO: Re-record cassettes to ensure this exists
3344
self.repositories_url = team["repositories_url"]
3445
self.slug = team["slug"]
46+
self.parent = None
47+
parent = team.get("parent")
48+
if parent:
49+
self.parent = ShortTeam(parent, self)
3550

3651
def _repr(self):
3752
return "<{s.class_name} [{s.name}]>".format(s=self)
@@ -170,6 +185,15 @@ def members(self, role=None, number=-1, etag=None):
170185
headers=headers,
171186
)
172187

188+
@requires_auth
189+
def permissions_for(
190+
self, repository: str
191+
) -> ShortRepositoryWithPermissions:
192+
headers = {"Accept": "application/vnd.github.v3.repository+json"}
193+
url = self._build_url("repos", repository, base_url=self._api)
194+
json = self._json(self._get(url, headers=headers), 200)
195+
return ShortRepositoryWithPermissions(json, self)
196+
173197
@requires_auth
174198
def repositories(self, number=-1, etag=None):
175199
"""Iterate over the repositories this team has access to.
@@ -182,12 +206,14 @@ def repositories(self, number=-1, etag=None):
182206
:returns:
183207
generator of repositories this team has access to
184208
:rtype:
185-
:class:`~github3.repos.ShortRepository`
209+
:class:`~github3.orgs.ShortRepositoryWithPermissions`
186210
"""
187-
headers = {"Accept": "application/vnd.github.ironman-preview+json"}
188211
url = self._build_url("repos", base_url=self._api)
189212
return self._iter(
190-
int(number), url, ShortRepository, etag=etag, headers=headers
213+
int(number),
214+
url,
215+
ShortRepositoryWithPermissions,
216+
etag=etag,
191217
)
192218

193219
@requires_auth
@@ -343,10 +369,6 @@ class _Organization(models.GitHubCore):
343369
http://developer.github.com/v3/orgs/
344370
"""
345371

346-
PREVIEW_HEADERS = {
347-
"Accept": "application/vnd.github.hellcat-preview+json"
348-
}
349-
350372
class_name = "_Organization"
351373

352374
# Filters available when listing members. Note: ``"2fa_disabled"``
@@ -654,11 +676,14 @@ def conceal_member(self, username):
654676
@requires_auth
655677
def create_team(
656678
self,
657-
name,
658-
repo_names=[],
659-
permission="pull",
660-
parent_team_id=None,
661-
privacy="secret",
679+
name: str,
680+
repo_names: t.Optional[t.Sequence[str]] = [],
681+
maintainers: t.Optional[
682+
t.Union[t.Sequence[str], t.Sequence["_users._User"]]
683+
] = [],
684+
permission: str = "pull",
685+
parent_team_id: t.Optional[int] = None,
686+
privacy: str = "secret",
662687
):
663688
"""Create a new team and return it.
664689
@@ -668,6 +693,8 @@ def create_team(
668693
(required), name to be given to the team
669694
:param list repo_names:
670695
(optional) repositories, e.g. ['github/dotfiles']
696+
:param list maintainers:
697+
(optional) list of usernames who will be maintainers
671698
:param str permission:
672699
(optional), options:
673700
@@ -692,20 +719,16 @@ def create_team(
692719
"""
693720
data = {
694721
"name": name,
695-
"repo_names": repo_names,
722+
"repo_names": [getattr(r, "full_name", r) for r in repo_names],
723+
"maintainers": [getattr(m, "login", m) for m in maintainers],
696724
"permission": permission,
697725
"privacy": privacy,
698726
}
699-
headers = (
700-
self.PREVIEW_HEADERS
701-
if parent_team_id or privacy == "closed"
702-
else None
703-
)
704727
if parent_team_id:
705728
data.update({"parent_team_id": parent_team_id})
706729

707730
url = self._build_url("teams", base_url=self._api)
708-
json = self._json(self._post(url, data, headers=headers), 201)
731+
json = self._json(self._post(url, data), 201)
709732
return self._instance_or_null(Team, json)
710733

711734
@requires_auth

tests/cassettes/Organization_create_team.json

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

tests/cassettes/Organization_create_team_child.json

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

tests/cassettes/Organization_create_team_parent.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/cassettes/Team_delete.json

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

0 commit comments

Comments
 (0)