aboutsummaryrefslogtreecommitdiffstats
path: root/arthur/apis/github/teams.py
blob: 4cf18d499403176b1d50f5644670dc4baf001245 (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
import aiohttp

from arthur.config import CONFIG
from arthur.log import logger

HEADERS = {
    "Accept": "application/vnd.github+json",
    "X-GitHub-Api-Version": "2022-11-28",
    "Authorization": f"Bearer {CONFIG.github_token.get_secret_value()}",
}
BASE_URL = "https://api.github.com"
MEMBERS_PER_PAGE = 100


class GithubTeamNotFoundError(aiohttp.ClientResponseError):
    """Raised when a github team could not be found."""


async def list_team_members(team_slug: str, session: aiohttp.ClientSession) -> list[dict[str, str]]:
    """List all Github teams."""
    endpoint = f"{BASE_URL}/orgs/{CONFIG.github_org}/teams/{team_slug}/members"
    params = {"per_page": MEMBERS_PER_PAGE}
    async with session.get(endpoint, headers=HEADERS, params=params) as response:
        response.raise_for_status()
        teams_resp = await response.json()
        if len(teams_resp) == MEMBERS_PER_PAGE:
            logger.warning(
                "Max number (%d) of members returned when fetching members of %s. Some members may have been missed.",
                MEMBERS_PER_PAGE,
                team_slug,
            )
        return teams_resp