Skip to content

Commit a6ac8a0

Browse files
committed
Add new endpoints for removing organisation members
1 parent 85e5951 commit a6ac8a0

3 files changed

Lines changed: 48 additions & 19 deletions

File tree

arthur/apis/github/__init__.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1-
from .teams import GitHubError, add_staff_member
1+
from arthur.config import CONFIG
22

3-
__all__ = ("GitHubError", "add_staff_member")
3+
from .orgs import remove_org_member
4+
from .teams import add_staff_member
5+
6+
7+
class GitHubError(Exception):
8+
"""Custom exception for GitHub API errors."""
9+
10+
def __init__(self, message: str):
11+
super().__init__(message)
12+
13+
14+
__all__ = ("GitHubError", "add_staff_member", "remove_org_member")
15+
16+
HEADERS = {
17+
"Accept": "application/vnd.github+json",
18+
"X-GitHub-Api-Version": "2022-11-28",
19+
"Authorization": f"Bearer {CONFIG.github_token.get_secret_value()}",
20+
}
21+
HTTP_404 = 404
22+
HTTP_403 = 403
23+
HTTP_422 = 422

arthur/apis/github/orgs.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import aiohttp
2+
3+
from arthur.apis.github import GitHubError, HEADERS, HTTP_403, HTTP_404
4+
from arthur.config import CONFIG
5+
6+
7+
async def remove_org_member(username: str) -> None:
8+
"""Remove a user from the GitHub organisation."""
9+
async with aiohttp.ClientSession() as session:
10+
endpoint = f"https://api.github.com/orgs/{CONFIG.github_org}/members/{username}"
11+
12+
async with session.delete(endpoint, headers=HEADERS) as resp:
13+
try:
14+
resp.raise_for_status()
15+
return await resp.json()
16+
except aiohttp.ClientResponseError as e:
17+
if e.status == HTTP_404:
18+
msg = f"Team or user not found in the org: {e.message}"
19+
raise GitHubError(msg)
20+
if e.status == HTTP_403:
21+
msg = f"Forbidden: {e.message}"
22+
raise GitHubError(msg)
23+
24+
msg = f"Unexpected error: {e.message}"
25+
raise GitHubError(msg)

arthur/apis/github/teams.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
import aiohttp
22

3+
from arthur.apis.github import GitHubError, HEADERS, HTTP_403, HTTP_404, HTTP_422
34
from arthur.config import CONFIG
45

5-
HEADERS = {
6-
"Accept": "application/vnd.github+json",
7-
"X-GitHub-Api-Version": "2022-11-28",
8-
"Authorization": f"Bearer {CONFIG.github_token.get_secret_value()}",
9-
}
10-
11-
HTTP_404 = 404
12-
HTTP_403 = 403
13-
HTTP_422 = 422
14-
15-
16-
class GitHubError(Exception):
17-
"""Custom exception for GitHub API errors."""
18-
19-
def __init__(self, message: str):
20-
super().__init__(message)
21-
226

237
async def add_staff_member(username: str) -> None:
248
"""Add a user to the default GitHub team."""

0 commit comments

Comments
 (0)