From e1659f36fc0a4ebc529bc4bbed568ba93feebe85 Mon Sep 17 00:00:00 2001 From: Konrad Kleine Date: Fri, 16 Feb 2024 17:00:09 +0100 Subject: [PATCH] Add script to check if a workflow artifact exists (#263) This adds a script to check if a workflow artifact exists. This can be used in places where we want to prevent a costly workflow to run multiple times. --- scripts/workflow-artifact-exists.py | 110 ++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100755 scripts/workflow-artifact-exists.py diff --git a/scripts/workflow-artifact-exists.py b/scripts/workflow-artifact-exists.py new file mode 100755 index 00000000..5ed07767 --- /dev/null +++ b/scripts/workflow-artifact-exists.py @@ -0,0 +1,110 @@ +#!/bin/env python3 + +import argparse +from github import Github +from datetime import date +import re +import sys +from pprint import pprint + + +def workflow_artifact_exists( + token: str, + project: str, + artifact_name: str, + workflow_name: str, + run_date: str = None, +) -> bool: + """Checks if the given workflow artifact exists + + Args: + token (str): The GitHub token to use + project (str): The GitHub project to query + artifact_name (str): The artifact to look for + workflow_name (str): The workflow name to look for + run_date (str, optional): Date when the workflow run happened. Defaults to None. + + Returns: + bool: Whether the artifact exists or not. + """ + extra_args = {} + if run_date is not None: + extra_args["created"] = str(run_date) + + g = Github(login_or_token=token) + repo = g.get_repo(project) + + for wf in repo.get_workflows(): + print(f"Workflow: {wf.name}", file=sys.stderr) + if wf.name != workflow_name: + continue + print( + " Found matching workflow. Now listing runs and their artifacts.", + file=sys.stderr, + ) + for run in wf.get_runs(exclude_pull_requests=True, **extra_args): + print(f" Run started at: {run.run_started_at}", file=sys.stderr) + for artifact in run.get_artifacts(): + print(f" Artifact: {artifact.name}", file=sys.stderr) + if artifact_name == artifact.name: + print(f" Found a match", file=sys.stderr) + return True + + print(f"Found no match", file=sys.stderr) + return False + + +def main(): + parser = argparse.ArgumentParser( + description="Find the latest commit that passed tests" + ) + parser.add_argument( + "--token", + dest="token", + type=str, + default="YOUR-TOKEN-HERE", + help="Your GitHub token", + ) + parser.add_argument( + "--project", + dest="project", + type=str, + default="fedora-llvm-team/llvm-snapshots", + help="The GitHub project to use (default: fedora-llvm-team/llvm-snapshots)", + ) + parser.add_argument( + "--workflow-name", + dest="workflow_name", + type=str, + default="", + help="The GitHub workflow name to look for (e.g. Test Tool Management)", + ) + parser.add_argument( + "--artifact-name", + dest="artifact_name", + type=str, + default="", + help="The artifact name to look for (e.g. tmt-test-results-big-merge-20240206-fedora-rawhide)", + ) + parser.add_argument( + "--run-date", + dest="run_date", + type=str, + default=None, + help=f"Date when the workflow run happened. (e.g. 2024-01-29)", + ) + + args = parser.parse_args() + + if not workflow_artifact_exists( + token=args.token, + project=args.project, + run_date=args.run_date, + artifact_name=args.artifact_name, + workflow_name=args.workflow_name, + ): + sys.exit(-1) + + +if __name__ == "__main__": + main()