|
| 1 | +import subprocess |
| 2 | +from typing_extensions import Annotated |
| 3 | +import typer |
| 4 | +import github |
| 5 | + |
| 6 | +def get_installation_access_token(app_id: int, private_key: str, |
| 7 | + installation_id: int) -> str: |
| 8 | + """ |
| 9 | + Obtain an installation access token using JWT. |
| 10 | +
|
| 11 | + Args: |
| 12 | + - app_id (int): The application ID for GitHub App. |
| 13 | + - private_key (str): The private key associated with the GitHub App. |
| 14 | + - installation_id (int): The installation ID of the GitHub App for a particular account. |
| 15 | +
|
| 16 | + Returns |
| 17 | + - Optional[str]: The installation access token. Returns `None` if there's an error obtaining the token. |
| 18 | +
|
| 19 | + """ |
| 20 | + integration = github.GithubIntegration(app_id, private_key) |
| 21 | + auth = integration.get_access_token(installation_id) |
| 22 | + assert auth |
| 23 | + assert auth.token |
| 24 | + return auth.token |
| 25 | + |
| 26 | + |
| 27 | +def main(branch: Annotated[str, typer.Option(envvar="GITHUB_REF_NAME")], |
| 28 | + app_id: Annotated[int, typer.Option(envvar="APP_ID")], |
| 29 | + installation_id: Annotated[int, typer.Option(envvar="INSTALLATION_ID")], |
| 30 | + server_docs_private_key: Annotated[str, typer.Option(envvar="SERVER_DOCS_PRIVATE_KEY")]): |
| 31 | + |
| 32 | + access_token = get_installation_access_token(app_id, server_docs_private_key, installation_id) |
| 33 | + |
| 34 | + git_destination_url_with_token = f"https://x-access-token:{access_token}@github.com/mongodb/docs.git" |
| 35 | + # Use a local path for testing |
| 36 | + # git_destination_url_with_token = "path_to_local_git" |
| 37 | + |
| 38 | + # Taken from SO: https://stackoverflow.com/a/69979203 |
| 39 | + subprocess.run(["git", "config", "--unset-all", "http.https://github.com/.extraheader"], check=True) |
| 40 | + # Push the code upstream |
| 41 | + subprocess.run(["git", "push", git_destination_url_with_token, branch], check=True) |
| 42 | + |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + typer.run(main) |
0 commit comments