def clone_repo()

in devai-api/app/github_utils.py [0:0]


def clone_repo(github_account: str, repo_name: str):
    """Clones the specified GitHub repository using the provided credentials.

    Args:
        github_account (str): The GitHub account/organization name.
        repo_name (str): The name of the repository.

    Returns:
        Repo: The cloned repository object. Returns None if cloning fails.
    """
    try:
        github_app_id = os.environ["GITHUB_APP_ID"]
        github_installation_id = os.environ["GITHUB_APP_INSTALLATION_ID"]
        github_app_private_key = os.environ["GITHUB_APP_PRIVATE_KEY"]

        try:
            with open(github_app_private_key, "r") as f:
                private_key = f.read()
        except Exception:
            private_key = github_app_private_key

        auth = Auth.AppAuth(
            github_app_id,
            private_key,
        )
        
        jwt_token = auth.create_jwt()
        
        response = requests.post(
            f"https://api.github.com/app/installations/{github_installation_id}/access_tokens",
            headers={
                "Authorization": f"Bearer {jwt_token}",
                "Accept": "application/vnd.github+json",
            },
        )

        installation_token = response.json()["token"]

        repo = Repo.clone_from(
            f"https://x-access-token:{installation_token}@github.com/{github_account}/{repo_name}.git",
            repo_name,
        )

        return repo

    except Exception as e:
        print(f"Error cloning repository: {e}")
        return None