File indexing completed on 2025-07-02 07:54:41
0001 import shutil
0002 import subprocess
0003 from github import Auth, Github, GithubException
0004 from pathlib import Path
0005
0006 import click
0007 from urllib.parse import urlparse
0008
0009 from ..filesystem import hashdir
0010 from ..util import get_cache_dir
0011
0012
0013 @click.command()
0014 @click.option('--token', envvar="GITHUB_TOKEN", required=True, help="GitHub access token (defaults to GITHUB_TOKEN environment variable)")
0015 @click.option('--owner', help="Owner of the target repository (token owner by default)")
0016 @click.option('--repo', default="capybara-reports", help="Name of the target repository")
0017 @click.argument('report-dir', type=click.Path(exists=True, file_okay=False, dir_okay=True))
0018 @click.pass_context
0019 def cate(ctx: click.Context, owner: str, repo: str, report_dir: str, token: str):
0020 gh = Github(auth=Auth.Token(token))
0021
0022 if owner is not None:
0023 user = gh.get_user(owner)
0024 else:
0025 user = gh.get_user()
0026
0027 try:
0028 repo = user.get_repo(repo)
0029 except GithubException:
0030 click.secho(f"Repository {user.login}/{repo.name} is not available. Attempting to create...", fg="yellow", err=True)
0031 repo = user.create_repo(repo)
0032
0033 report_dir = Path(report_dir)
0034
0035 prefix = hashdir(report_dir)
0036
0037 clone_url = urlparse(repo.clone_url)
0038
0039 clone_url = clone_url._replace(
0040 netloc=f"{user.login}:{token}@{clone_url.netloc}",
0041 )
0042 local_repo = get_cache_dir() / user.login / repo.name
0043
0044 if not local_repo.exists():
0045 subprocess.check_output(["git", "clone", clone_url.geturl(), str(local_repo)])
0046 else:
0047 subprocess.check_output(["git", "-C", str(local_repo), "pull"])
0048 shutil.copytree(report_dir, local_repo / prefix)
0049 subprocess.check_output(["git", "-C", str(local_repo), "add", prefix])
0050 subprocess.check_output(["git", "-C", str(local_repo), "commit", "-m", f"Adding {prefix}/"])
0051 subprocess.check_output(["git", "-C", str(local_repo), "push"])
0052
0053 try:
0054 file = repo.get_contents(".nojekyll")
0055 except GithubException.UnknownObjectException:
0056
0057 repo.create_file(
0058 ".nojekyll",
0059 f"Adding .nojekyll",
0060 "",
0061 branch="gh-pages",
0062 )
0063
0064 click.echo(f"https://{user.login}.github.io/{repo.name}/{prefix}/")