aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Joe Banks <[email protected]>2025-07-16 10:00:10 +0100
committerGravatar Joe Banks <[email protected]>2025-07-16 10:00:10 +0100
commita6ac8a024cd0fc7ac5e17d7bb1b9ec4692e322d9 (patch)
treec01cf0b2990d520e0f1215c0e63897797eb99689
parentFix mistakes from my inferior brain (#327) (diff)
Add new endpoints for removing organisation members
-rw-r--r--arthur/apis/github/__init__.py24
-rw-r--r--arthur/apis/github/orgs.py25
-rw-r--r--arthur/apis/github/teams.py18
3 files changed, 48 insertions, 19 deletions
diff --git a/arthur/apis/github/__init__.py b/arthur/apis/github/__init__.py
index 11261da..3eb55a6 100644
--- a/arthur/apis/github/__init__.py
+++ b/arthur/apis/github/__init__.py
@@ -1,3 +1,23 @@
-from .teams import GitHubError, add_staff_member
+from arthur.config import CONFIG
-__all__ = ("GitHubError", "add_staff_member")
+from .orgs import remove_org_member
+from .teams import add_staff_member
+
+
+class GitHubError(Exception):
+ """Custom exception for GitHub API errors."""
+
+ def __init__(self, message: str):
+ super().__init__(message)
+
+
+__all__ = ("GitHubError", "add_staff_member", "remove_org_member")
+
+HEADERS = {
+ "Accept": "application/vnd.github+json",
+ "X-GitHub-Api-Version": "2022-11-28",
+ "Authorization": f"Bearer {CONFIG.github_token.get_secret_value()}",
+}
+HTTP_404 = 404
+HTTP_403 = 403
+HTTP_422 = 422
diff --git a/arthur/apis/github/orgs.py b/arthur/apis/github/orgs.py
new file mode 100644
index 0000000..55039c8
--- /dev/null
+++ b/arthur/apis/github/orgs.py
@@ -0,0 +1,25 @@
+import aiohttp
+
+from arthur.apis.github import GitHubError, HEADERS, HTTP_403, HTTP_404
+from arthur.config import CONFIG
+
+
+async def remove_org_member(username: str) -> None:
+ """Remove a user from the GitHub organisation."""
+ async with aiohttp.ClientSession() as session:
+ endpoint = f"https://api.github.com/orgs/{CONFIG.github_org}/members/{username}"
+
+ async with session.delete(endpoint, headers=HEADERS) as resp:
+ try:
+ resp.raise_for_status()
+ return await resp.json()
+ except aiohttp.ClientResponseError as e:
+ if e.status == HTTP_404:
+ msg = f"Team or user not found in the org: {e.message}"
+ raise GitHubError(msg)
+ if e.status == HTTP_403:
+ msg = f"Forbidden: {e.message}"
+ raise GitHubError(msg)
+
+ msg = f"Unexpected error: {e.message}"
+ raise GitHubError(msg)
diff --git a/arthur/apis/github/teams.py b/arthur/apis/github/teams.py
index 489da90..e919051 100644
--- a/arthur/apis/github/teams.py
+++ b/arthur/apis/github/teams.py
@@ -1,24 +1,8 @@
import aiohttp
+from arthur.apis.github import GitHubError, HEADERS, HTTP_403, HTTP_404, HTTP_422
from arthur.config import CONFIG
-HEADERS = {
- "Accept": "application/vnd.github+json",
- "X-GitHub-Api-Version": "2022-11-28",
- "Authorization": f"Bearer {CONFIG.github_token.get_secret_value()}",
-}
-
-HTTP_404 = 404
-HTTP_403 = 403
-HTTP_422 = 422
-
-
-class GitHubError(Exception):
- """Custom exception for GitHub API errors."""
-
- def __init__(self, message: str):
- super().__init__(message)
-
async def add_staff_member(username: str) -> None:
"""Add a user to the default GitHub team."""