|
| 1 | +"""GitHub client factory.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from githubkit import AppInstallationAuthStrategy, GitHub # type: ignore[reportMissingImports] |
| 6 | + |
| 7 | +__all__ = ("create_github_client",) |
| 8 | + |
| 9 | + |
| 10 | +def create_github_client( |
| 11 | + app_id: int, |
| 12 | + private_key: str, |
| 13 | + installation_id: int, |
| 14 | + client_id: str, |
| 15 | + client_secret: str, |
| 16 | +) -> GitHub: |
| 17 | + """Create a GitHub client for an app installation. |
| 18 | +
|
| 19 | + This factory function creates a GitHub client authenticated as a GitHub App installation. |
| 20 | + Unlike the hardcoded client in the monolith, this allows for dynamic installation IDs |
| 21 | + so different guilds can use different GitHub App installations. |
| 22 | +
|
| 23 | + Args: |
| 24 | + app_id: GitHub App ID. |
| 25 | + private_key: GitHub App private key (PEM format). |
| 26 | + installation_id: GitHub App installation ID. |
| 27 | + client_id: GitHub App client ID. |
| 28 | + client_secret: GitHub App client secret. |
| 29 | +
|
| 30 | + Returns: |
| 31 | + Configured GitHub client instance. |
| 32 | +
|
| 33 | + Example: |
| 34 | + ```python |
| 35 | + from byte_common.clients import create_github_client |
| 36 | +
|
| 37 | + client = create_github_client( |
| 38 | + app_id=480575, |
| 39 | + private_key=settings.github.APP_PRIVATE_KEY, |
| 40 | + installation_id=guild_installation_id, |
| 41 | + client_id=settings.github.APP_CLIENT_ID, |
| 42 | + client_secret=settings.github.APP_CLIENT_SECRET, |
| 43 | + ) |
| 44 | +
|
| 45 | + # Use the client |
| 46 | + repo = await client.rest.repos.async_get("owner", "repo") |
| 47 | + ``` |
| 48 | + """ |
| 49 | + return GitHub( |
| 50 | + AppInstallationAuthStrategy( |
| 51 | + app_id=app_id, |
| 52 | + private_key=private_key, |
| 53 | + installation_id=installation_id, |
| 54 | + client_id=client_id, |
| 55 | + client_secret=client_secret, |
| 56 | + ) |
| 57 | + ) |
0 commit comments