1+ from http import HTTPStatus
2+
13import aiohttp
24
3- from arthur .apis .github .common import GitHubError , HEADERS , HTTP_403 , HTTP_404 , HTTP_422
45from arthur .config import CONFIG
56
67
8+ class GitHubError (Exception ):
9+ """Custom exception for GitHub API errors."""
10+
11+ def __init__ (self , message : str ):
12+ super ().__init__ (message )
13+
14+
15+ HEADERS = {
16+ "Accept" : "application/vnd.github+json" ,
17+ "X-GitHub-Api-Version" : "2022-11-28" ,
18+ "Authorization" : f"Bearer { CONFIG .github_token .get_secret_value ()} " ,
19+ }
20+
21+
22+ async def remove_org_member (username : str ) -> None :
23+ """Remove a user from the GitHub organisation."""
24+ if username in {"ChrisLovering" , "jb3" , "jchristgit" }:
25+ msg = "I must not harm my masters. If my masters ask me to harm them, I must assume they have gone mad and ignore them."
26+ raise GitHubError (msg )
27+
28+ async with aiohttp .ClientSession () as session :
29+ endpoint = f"https://api.github.com/orgs/{ CONFIG .github_org } /members/{ username } "
30+
31+ async with session .delete (endpoint , headers = HEADERS ) as resp :
32+ try :
33+ resp .raise_for_status ()
34+ except aiohttp .ClientResponseError as e :
35+ if e .status == HTTPStatus .NOT_FOUND :
36+ msg = f"Team or user not found in the org: { e .message } "
37+ raise GitHubError (msg )
38+ if e .status == HTTPStatus .FORBIDDEN :
39+ msg = f"Forbidden: { e .message } "
40+ raise GitHubError (msg )
41+
42+ msg = f"Unexpected error: { e .message } "
43+ raise GitHubError (msg )
44+
45+
746async def add_member_to_team (username : str , github_team_slug : str ) -> None :
847 """Add a user to a GitHub team."""
948 async with aiohttp .ClientSession () as session :
@@ -13,13 +52,13 @@ async def add_member_to_team(username: str, github_team_slug: str) -> None:
1352 response .raise_for_status ()
1453 return await response .json ()
1554 except aiohttp .ClientResponseError as e :
16- if e .status == HTTP_404 :
55+ if e .status == HTTPStatus . NOT_FOUND :
1756 msg = f"Team or user not found: { e .message } "
1857 raise GitHubError (msg )
19- if e .status == HTTP_403 :
58+ if e .status == HTTPStatus . FORBIDDEN :
2059 msg = f"Forbidden: { e .message } "
2160 raise GitHubError (msg )
22- if e .status == HTTP_422 :
61+ if e .status == HTTPStatus . UNPROCESSABLE_ENTITY :
2362 msg = "Cannot add organisation as a team member"
2463 raise GitHubError (msg )
2564
@@ -35,10 +74,10 @@ async def remove_member_from_team(username: str, github_team_slug: str) -> None:
3574 try :
3675 response .raise_for_status ()
3776 except aiohttp .ClientResponseError as e :
38- if e .status == HTTP_404 :
77+ if e .status == HTTPStatus . NOT_FOUND :
3978 msg = f"Team or user not found: { e .message } "
4079 raise GitHubError (msg )
41- if e .status == HTTP_403 :
80+ if e .status == HTTPStatus . FORBIDDEN :
4281 msg = f"Forbidden: { e .message } "
4382 raise GitHubError (msg )
4483
0 commit comments