Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:18:57

0001 import click
0002 from github import Auth, Github, GithubException
0003 
0004 from ..github import download_artifact
0005 
0006 
0007 @click.command()
0008 @click.option('--artifact-name', default="rec_dis_18x275_minQ2=1000_craterlake_18x275.edm4eic.root")
0009 @click.option('--token', envvar="GITHUB_TOKEN", required=True, help="GitHub access token (defaults to GITHUB_TOKEN environment variable)")
0010 @click.option('--owner', default="eic", help="Owner of the target repository")
0011 @click.option('--repo', default="EICrecon", help="Name of the target repository")
0012 @click.argument('pr_number', type=int)
0013 @click.pass_context
0014 def pr(ctx: click.Context, artifact_name: str, owner: str, pr_number: int, repo: str, token: str):
0015     gh = Github(auth=Auth.Token(token))
0016     repo = gh.get_user(owner).get_repo(repo)
0017 
0018     click.secho(f'Fetching metadata for #{pr_number}...', fg='green', err=True)
0019     try:
0020         pr = repo.get_pull(pr_number)
0021     except GithubException as e:
0022         click.secho("Pull Request is not accessible", fg="red", err=True)
0023         click.secho(e)
0024         ctx.exit(1)
0025     click.echo(f"Title: {pr.title}", err=True)
0026     other_repo_message = ""
0027     if pr.head.repo != repo:
0028        other_repo_message = click.style(f"{pr.head.repo.owner.login}/{pr.head.repo.name}", italic=True) + "/"
0029     click.echo(f"PR head: {other_repo_message}{click.style(pr.head.ref, bold=True)}@{pr.head.sha}, targets {click.style(pr.base.ref, bold=True)}@{pr.base.sha}", err=True)
0030 
0031     workflow_head = None
0032     workflow_base = None
0033     messages = []
0034 
0035     def item_show_func(workflow):
0036        workflow_id = str(workflow.id) if hasattr(workflow, "id") else "?"
0037        head_found = "no" if workflow_head is None else "yes"
0038        base_found = "no" if workflow_base is None else "yes"
0039        return f"{workflow_id} head:{head_found} base:{base_found}"
0040 
0041     with click.progressbar(
0042             repo.get_workflow_runs(),
0043             label="Loading workflows",
0044             item_show_func=item_show_func,
0045     ) as workflows_bar:
0046         for workflow in workflows_bar:
0047             # workflow.head_commit.sha is not available, so we take the latest
0048             # TODO check if PR is the latest by parsing log files?
0049             if workflow.head_repository == pr.head.repo and workflow.head_branch == pr.head.ref and workflow_head is None:
0050                 if workflow.get_artifacts().totalCount == 0:
0051                     messages.append(dict(message=f"Skipping workflow {workflow.html_url} on {workflow.head_branch} with no artifacts", fg="red", err=True))
0052                     continue
0053                 workflow_head = workflow
0054             if workflow.head_repository == repo and workflow.head_branch == pr.base.ref and workflow_base is None:
0055                 if workflow.get_artifacts().totalCount == 0:
0056                     messages.append(dict(message=f"Skipping workflow {workflow.html_url} on {workflow.head_branch} with no artifacts", fg="red", err=True))
0057                     continue
0058                 workflow_base = workflow
0059             if workflow_head is not None and workflow_base is not None:
0060                 break
0061 
0062     if workflow_head is None:
0063         click.secho("No completed workflow found for head branch", fg="red")
0064         ctx.exit(1)
0065     if workflow_base is None:
0066         click.secho("No completed workflow found for base branch", fg="red")
0067         ctx.exit(1)
0068 
0069     for message in messages:
0070         click.secho(**message)
0071 
0072     click.echo(f"PR base workflow: {workflow_base.html_url}")
0073     click.echo(f"PR head workflow: {workflow_head.html_url}")
0074 
0075     click.echo(download_artifact(workflow_base, artifact_name, token=token, click=click))
0076     click.echo(download_artifact(workflow_head, artifact_name, token=token, click=click))
0077 
0078 
0079 @click.command()
0080 @click.option('--artifact-name', default="rec_dis_18x275_minQ2=1000_craterlake_18x275.edm4eic.root")
0081 @click.option('--token', envvar="GITHUB_TOKEN", required=True, help="GitHub access token (defaults to GITHUB_TOKEN environment variable)")
0082 @click.option('--owner', default="eic", help="Owner of the target repository")
0083 @click.option('--repo', default="EICrecon", help="Name of the target repository")
0084 @click.argument('ref', type=str)
0085 @click.pass_context
0086 def rev(ctx: click.Context, artifact_name: str, owner: str, ref: str, repo: str, token: str):
0087     gh = Github(auth=Auth.Token(token))
0088     repo = gh.get_user(owner).get_repo(repo)
0089 
0090     rev = repo.get_commit(ref)
0091 
0092     def item_show_func(workflow):
0093        workflow_id = str(workflow.id) if hasattr(workflow, "id") else "?"
0094        head_found = "no" if workflow_head is None else "yes"
0095        return f"{workflow_id} head:{head_found}"
0096 
0097     workflow_head = None
0098     messages = []
0099 
0100     with click.progressbar(
0101             repo.get_workflow_runs(),
0102             label="Loading workflows",
0103             item_show_func=item_show_func,
0104     ) as workflows_bar:
0105         for workflow in workflows_bar:
0106             # workflow.head_commit.sha is not available, so we take the latest
0107             # TODO check if PR is the latest by parsing log files?
0108             if workflow.head_repository == repo and workflow.head_sha == rev.sha and workflow_head is None:
0109                 if workflow.get_artifacts().totalCount == 0:
0110                     messages.append(dict(message=f"Skipping workflow {workflow.html_url} on {workflow.head_branch} with no artifacts", fg="red", err=True))
0111                     continue
0112                 workflow_head = workflow
0113             if workflow_head is not None:
0114                 break
0115 
0116     if workflow_head is None:
0117         click.secho("No completed workflow found for head branch", fg="red")
0118         ctx.exit(1)
0119 
0120     for message in messages:
0121         click.secho(**message)
0122 
0123     click.echo(f"PR head workflow: {workflow_head.html_url}")
0124 
0125     click.echo(download_artifact(workflow_head, artifact_name, token=token, click=click))
0126 
0127 
0128 class ForwardGroup(click.Group):
0129     def resolve_command(self, ctx, args):
0130         try:
0131             cmd_name, cmd, args = super().resolve_command(ctx, args)
0132         except click.exceptions.UsageError as e:
0133             click.secho(f"Invoking `capy' without subcommand is deprecated. Use `capy pr'.", fg="yellow", err=True)
0134             args = ["pr"] + args
0135             cmd_name, cmd, args = super().resolve_command(ctx, args)
0136         return cmd_name, cmd, args
0137 
0138 @click.group(cls=ForwardGroup, context_settings={'help_option_names': ['-h', '--help']})
0139 @click.option('--artifact-name', default="rec_dis_18x275_minQ2=1000_craterlake_18x275.edm4eic.root")
0140 @click.option('--token', envvar="GITHUB_TOKEN", required=True, help="GitHub access token (defaults to GITHUB_TOKEN environment variable)")
0141 @click.option('--owner', default="eic", help="Owner of the target repository")
0142 @click.option('--repo', default="EICrecon", help="Name of the target repository")
0143 def capy(**kwargs):
0144     pass
0145 
0146 
0147 capy.add_command(pr)
0148 capy.add_command(rev)