blob: 489da904551f8c351230c0cac0f88f7908ae5f52 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
import aiohttp
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."""
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)
|