File indexing completed on 2025-01-30 09:18:57
0001 from pathlib import Path
0002
0003 import click
0004 from github import Auth, Github, GithubException
0005
0006 from ..filesystem import hashdir
0007
0008
0009 @click.command()
0010 @click.option('--token', envvar="GITHUB_TOKEN", required=True, help="GitHub access token (defaults to GITHUB_TOKEN environment variable)")
0011 @click.option('--owner', help="Owner of the target repository (token owner by default)")
0012 @click.option('--repo', default="capybara-reports", help="Name of the target repository")
0013 @click.argument('report-dir', type=click.Path(exists=True, file_okay=False, dir_okay=True))
0014 @click.pass_context
0015 def cate(ctx: click.Context, owner: str, repo: str, report_dir: str, token: str):
0016 gh = Github(auth=Auth.Token(token))
0017 if owner is not None:
0018 user = gh.get_user(owner)
0019 try:
0020 repo = user.get_repo(repo)
0021 except GithubException:
0022 click.secho(f"Repository {owner}/{repo} is not available.", fg="red", err=True)
0023 repo = user.create_repo(repo)
0024 else:
0025 user = gh.get_user()
0026 try:
0027 repo = user.get_repo(repo)
0028 except GithubException:
0029 click.secho(f"Repository {user.login}/{repo} is not available. Attempting to create...", fg="yellow", err=True)
0030 repo = user.create_repo(repo)
0031
0032 report_dir = Path(report_dir)
0033
0034 prefix = hashdir(report_dir)
0035
0036 def recurse_upload(cur_path):
0037 paths = cur_path.iterdir()
0038 for file_ in sorted([p for p in paths if p.is_file()]):
0039 relpath = file_.relative_to(report_dir)
0040 with open(file_, "rb") as fp:
0041 contents = fp.read()
0042 target_path = f"{prefix}/{relpath}"
0043 click.secho(f"Uploading {target_path}", fg="green", err=True)
0044 repo.create_file(
0045 target_path,
0046 f"Adding {target_path}",
0047 contents.decode(),
0048 branch="gh-pages",
0049 )
0050
0051 paths = cur_path.iterdir()
0052 for dir_ in sorted([p for p in paths if p.is_dir()]):
0053 recurse_upload(dir_)
0054
0055 recurse_upload(report_dir)
0056
0057 try:
0058 file = repo.get_contents(".nojekyll")
0059 except GithubException.UnknownObjectException:
0060
0061 repo.create_file(
0062 ".nojekyll",
0063 f"Adding .nojekyll",
0064 "",
0065 branch="gh-pages",
0066 )
0067
0068 click.echo(f"https://{user.login}.github.io/{repo.name}/{prefix}/")