diff options
-rw-r--r-- | arthur/apis/github/teams.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/arthur/apis/github/teams.py b/arthur/apis/github/teams.py index a8775a3..489da90 100644 --- a/arthur/apis/github/teams.py +++ b/arthur/apis/github/teams.py @@ -1,3 +1,5 @@ +import aiohttp + from arthur.config import CONFIG HEADERS = { @@ -5,3 +7,37 @@ HEADERS = { "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.""" + async with aiohttp.ClientSession() as session: + endpoint = f"https://api.github.com/orgs/{CONFIG.github_org}/teams/{CONFIG.github_team}/memberships/{username}" + async with session.put(endpoint, headers=HEADERS) as response: + try: + response.raise_for_status() + return await response.json() + except aiohttp.ClientResponseError as e: + if e.status == HTTP_404: + msg = f"Team or user not found: {e.message}" + raise GitHubError(msg) + if e.status == HTTP_403: + msg = f"Forbidden: {e.message}" + raise GitHubError(msg) + if e.status == HTTP_422: + msg = "Cannot add organisation as a team member" + raise GitHubError(msg) + + msg = f"Unexpected error: {e.message}" + raise GitHubError(msg) |