transformers
70 строк · 2.7 Кб
1import os
2import zipfile
3
4import requests
5from get_ci_error_statistics import download_artifact, get_artifacts_links
6
7
8def get_daily_ci_runs(token, num_runs=7):
9"""Get the workflow runs of the scheduled (daily) CI.
10
11This only selects the runs triggered by the `schedule` event on the `main` branch.
12"""
13headers = None
14if token is not None:
15headers = {"Accept": "application/vnd.github+json", "Authorization": f"Bearer {token}"}
16
17# The id of a workflow (not of a workflow run)
18workflow_id = "636036"
19
20url = f"https://api.github.com/repos/huggingface/transformers/actions/workflows/{workflow_id}/runs"
21# On `main` branch + event being `schedule` + not returning PRs + only `num_runs` results
22url += f"?branch=main&event=schedule&exclude_pull_requests=true&per_page={num_runs}"
23
24result = requests.get(url, headers=headers).json()
25
26return result["workflow_runs"]
27
28
29def get_last_daily_ci_runs(token):
30"""Get the last completed workflow run id of the scheduled (daily) CI."""
31workflow_runs = get_daily_ci_runs(token)
32workflow_run_id = None
33for workflow_run in workflow_runs:
34if workflow_run["status"] == "completed":
35workflow_run_id = workflow_run["id"]
36break
37
38return workflow_run_id
39
40
41def get_last_daily_ci_artifacts(artifact_names, output_dir, token):
42"""Get the artifacts of last completed workflow run id of the scheduled (daily) CI."""
43workflow_run_id = get_last_daily_ci_runs(token)
44if workflow_run_id is not None:
45artifacts_links = get_artifacts_links(worflow_run_id=workflow_run_id, token=token)
46for artifact_name in artifact_names:
47if artifact_name in artifacts_links:
48artifact_url = artifacts_links[artifact_name]
49download_artifact(
50artifact_name=artifact_name, artifact_url=artifact_url, output_dir=output_dir, token=token
51)
52
53
54def get_last_daily_ci_reports(artifact_names, output_dir, token):
55"""Get the artifacts' content of the last completed workflow run id of the scheduled (daily) CI."""
56get_last_daily_ci_artifacts(artifact_names, output_dir, token)
57
58results = {}
59for artifact_name in artifact_names:
60artifact_zip_path = os.path.join(output_dir, f"{artifact_name}.zip")
61if os.path.isfile(artifact_zip_path):
62results[artifact_name] = {}
63with zipfile.ZipFile(artifact_zip_path) as z:
64for filename in z.namelist():
65if not os.path.isdir(filename):
66# read the file
67with z.open(filename) as f:
68results[artifact_name][filename] = f.read().decode("UTF-8")
69
70return results
71