|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | + |
| 5 | +from github import Auth, Github |
| 6 | + |
| 7 | +# parse args |
| 8 | +parser = argparse.ArgumentParser() |
| 9 | +parser.add_argument("-t", "--token", required=True, help="GitHub access token") |
| 10 | +parser.add_argument("-o", "--owner", required=False, default="nginx", help="GitHub repository owner") |
| 11 | +parser.add_argument("-r", "--repo", help="GitHub repository name", required=False, default="kubernetes-ingress") |
| 12 | +parser.add_argument("-w", "--workflow", help="GitHub Actions workflow name", required=False, default="CI") |
| 13 | +parser.add_argument("-b", "--branch", help="GitHub repository branch", required=False, default="main") |
| 14 | +parser.add_argument( |
| 15 | + "-d", "--duration", help="Minimum duration of jobs in seconds", required=False, default=900, type=int |
| 16 | +) |
| 17 | +args = parser.parse_args() |
| 18 | +TOKEN = args.token |
| 19 | +OWNER = args.owner |
| 20 | +REPO = args.repo |
| 21 | +BRANCH = args.branch |
| 22 | +DURATION = args.duration |
| 23 | +WORKFLOW = args.workflow |
| 24 | + |
| 25 | + |
| 26 | +def get_github_repo(owner, repo, token): |
| 27 | + # Authenticate to GitHub |
| 28 | + auth = Auth.Token(token) |
| 29 | + g = Github(auth=auth) |
| 30 | + |
| 31 | + # Get the repository |
| 32 | + repository = g.get_repo(f"{owner}/{repo}") |
| 33 | + g.close() # Close the connection |
| 34 | + return repository |
| 35 | + |
| 36 | + |
| 37 | +def get_workflow_runs(repo, workflow_name, branch=None): |
| 38 | + workflows = repo.get_workflows() |
| 39 | + for workflow in workflows: |
| 40 | + if workflow.name == workflow_name: |
| 41 | + return workflow.get_runs(branch=branch, status="completed") |
| 42 | + return None |
| 43 | + |
| 44 | + |
| 45 | +def get_run_branch_jobs(runs): |
| 46 | + results = {} |
| 47 | + for run in runs: |
| 48 | + results[run.id] = run.jobs() |
| 49 | + return results |
| 50 | + |
| 51 | + |
| 52 | +def convert_seconds(seconds): |
| 53 | + min, sec = divmod(seconds, 60) |
| 54 | + hour, min = divmod(min, 60) |
| 55 | + return "%d:%02d:%02d" % (hour, min, sec) |
| 56 | + |
| 57 | + |
| 58 | +r = get_github_repo(OWNER, REPO, TOKEN) |
| 59 | + |
| 60 | +# Get the latest workflow runs |
| 61 | +runs = get_workflow_runs(r, WORKFLOW, branch=BRANCH) |
| 62 | +if not runs: |
| 63 | + print("No workflow runs found.") |
| 64 | + exit(1) |
| 65 | +wj = get_run_branch_jobs(runs) |
| 66 | +for run_id in sorted(wj.keys()): |
| 67 | + print(f"Workflow Run ID: {run_id}") |
| 68 | + for job in wj[run_id]: |
| 69 | + job_duration = (job.completed_at - job.started_at).total_seconds() |
| 70 | + if job.status == "completed" and job.conclusion == "success" and job_duration > DURATION: |
| 71 | + print(f" Job: {job.name}, Duration: {convert_seconds(job_duration)}, URL: {job.html_url}") |
0 commit comments