Skip to content

Commit 88966b8

Browse files
committed
Allow adding & removing arbitrary members from gh teams
1 parent b2c32c0 commit 88966b8

3 files changed

Lines changed: 26 additions & 7 deletions

File tree

arthur/apis/github/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .common import GitHubError
22
from .orgs import remove_org_member
3-
from .teams import add_staff_member
3+
from .teams import add_member_to_team
44

5-
__all__ = ("GitHubError", "add_staff_member", "remove_org_member")
5+
__all__ = ("GitHubError", "add_member_to_team", "remove_org_member")

arthur/apis/github/teams.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
from arthur.config import CONFIG
55

66

7-
async def add_staff_member(username: str) -> None:
8-
"""Add a user to the default GitHub team."""
7+
async def add_member_to_team(username: str, github_team_slug: str) -> None:
8+
"""Add a user to a GitHub team."""
99
async with aiohttp.ClientSession() as session:
10-
endpoint = f"https://api.github.com/orgs/{CONFIG.github_org}/teams/{CONFIG.github_team}/memberships/{username}"
10+
endpoint = f"https://api.github.com/orgs/{CONFIG.github_org}/teams/{github_team_slug}/memberships/{username}"
1111
async with session.put(endpoint, headers=HEADERS) as response:
1212
try:
1313
response.raise_for_status()
@@ -25,3 +25,22 @@ async def add_staff_member(username: str) -> None:
2525

2626
msg = f"Unexpected error: {e.message}"
2727
raise GitHubError(msg)
28+
29+
30+
async def remove_member_from_team(username: str, github_team_slug: str) -> None:
31+
"""Remove a user from a GitHub team."""
32+
async with aiohttp.ClientSession() as session:
33+
endpoint = f"https://api.github.com/orgs/{CONFIG.github_org}/teams/{github_team_slug}/memberships/{username}"
34+
async with session.delete(endpoint, headers=HEADERS) as response:
35+
try:
36+
response.raise_for_status()
37+
except aiohttp.ClientResponseError as e:
38+
if e.status == HTTP_404:
39+
msg = f"Team or user not found: {e.message}"
40+
raise GitHubError(msg)
41+
if e.status == HTTP_403:
42+
msg = f"Forbidden: {e.message}"
43+
raise GitHubError(msg)
44+
45+
msg = f"Unexpected error: {e.message}"
46+
raise GitHubError(msg)

arthur/exts/github/management.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from discord.ext.commands import Cog, Context, group
66

7-
from arthur.apis.github import GitHubError, add_staff_member, remove_org_member
7+
from arthur.apis.github import GitHubError, add_member_to_team, remove_org_member
88
from arthur.config import CONFIG
99

1010
if TYPE_CHECKING:
@@ -35,7 +35,7 @@ async def github(self, ctx: Context) -> None:
3535
async def add_team_member(self, ctx: Context, username: str) -> None:
3636
"""Add a user to the default GitHub team."""
3737
try:
38-
await add_staff_member(username)
38+
await add_member_to_team(username, CONFIG.github_team)
3939
await ctx.send(f":white_check_mark: Successfully invited {username} to the staff team.")
4040
except GitHubError as e:
4141
await ctx.send(f":x: Failed to add {username} to the staff team: {e}")

0 commit comments

Comments
 (0)