|
| 1 | +import pathlib |
| 2 | +import shutil |
| 3 | + |
| 4 | +import requests |
| 5 | +import zipfile |
| 6 | +import os |
| 7 | + |
| 8 | +# To test this locally, set these environment variables |
| 9 | +REPO_OWNER = os.environ.get("REPO_OWNER", "IfcOpenShell/IfcOpenShell") |
| 10 | +MY_WORKFLOW = os.environ.get("MY_WORKFLOW", "ci-ifcopenshell-conda-daily") |
| 11 | +WORKFLOW_RUN_ID = os.environ.get("WORKFLOW_RUN_ID", None) |
| 12 | +TOKEN = os.environ.get("GITHUB_TOKEN", None) |
| 13 | +# To test this locally create a fine-grained personal access token for this repo with permissions "actions:read" |
| 14 | +# See https://github.com/settings/tokens?type=beta |
| 15 | + |
| 16 | +# This is a list of strings that indicate that a job has stopped abruptly. |
| 17 | +SIGNS_OF_STOPPAGE = [ |
| 18 | + "Error: The operation was canceled.", |
| 19 | + "fatal error C1060: compiler is out of heap space", |
| 20 | +] |
| 21 | + |
| 22 | + |
| 23 | +def start_request_session(): |
| 24 | + s = requests.Session() |
| 25 | + headers = { |
| 26 | + "Accept": "application/vnd.github+json", |
| 27 | + "X-GitHub-Api-Version": "2022-11-28", |
| 28 | + } |
| 29 | + if TOKEN: |
| 30 | + headers["Authorization"] = f"Bearer {TOKEN}" |
| 31 | + |
| 32 | + s.headers = headers |
| 33 | + return s |
| 34 | + |
| 35 | + |
| 36 | +def get_ci_run(repo_name, run_id): |
| 37 | + url = f"https://api.github.com/repos/{repo_name}/actions/runs/{run_id}" |
| 38 | + s = start_request_session() |
| 39 | + response = s.get(url) |
| 40 | + return response.json() |
| 41 | + |
| 42 | + |
| 43 | +def evaluate_log_file_for_abrupt_stop(log_file): |
| 44 | + with open(log_file) as f: |
| 45 | + for i, line in enumerate(f): |
| 46 | + for sign in SIGNS_OF_STOPPAGE: |
| 47 | + if sign in line: |
| 48 | + print(f"Found sign of abrupt stoppage in [line {i}]: '{sign}'") |
| 49 | + return True |
| 50 | + return False |
| 51 | + |
| 52 | + |
| 53 | +def get_ci_specific_run_specific_failure_details(repo_name, run_id): |
| 54 | + url = f"https://api.github.com/repos/{repo_name}/actions/runs/{run_id}/attempts/1/logs" |
| 55 | + s = start_request_session() |
| 56 | + response = s.get(url) |
| 57 | + |
| 58 | + # SAVE zip file |
| 59 | + with open("logs.zip", "wb") as f: |
| 60 | + f.write(response.content) |
| 61 | + |
| 62 | + # unzip file |
| 63 | + with zipfile.ZipFile("logs.zip", "r") as zip_ref: |
| 64 | + zip_ref.extractall("logs") |
| 65 | + |
| 66 | + failed_logs = [] |
| 67 | + for file in pathlib.Path("logs").iterdir(): |
| 68 | + if file.is_dir(): |
| 69 | + continue |
| 70 | + |
| 71 | + if evaluate_log_file_for_abrupt_stop(file): |
| 72 | + failed_logs.append(file) |
| 73 | + |
| 74 | + return failed_logs |
| 75 | + |
| 76 | + |
| 77 | +def restart_job(repo_name, job_id): |
| 78 | + url = f"https://api.github.com/repos/{repo_name}/actions/runs/{job_id}/rerun-failed-jobs" |
| 79 | + s = start_request_session() |
| 80 | + response = s.post(url) |
| 81 | + return response.json() |
| 82 | + |
| 83 | + |
| 84 | +def eval_jobs(): |
| 85 | + run = get_ci_run(REPO_OWNER, WORKFLOW_RUN_ID) |
| 86 | + if run["run_attempt"] > 1: |
| 87 | + print("This is not the first attempt. Exiting") |
| 88 | + return |
| 89 | + failed_logs = get_ci_specific_run_specific_failure_details(REPO_OWNER, WORKFLOW_RUN_ID) |
| 90 | + |
| 91 | + if len(failed_logs) == 0: |
| 92 | + print("No runs exhibit signs of abrupt stoppage") |
| 93 | + return |
| 94 | + |
| 95 | + print("restarting job", WORKFLOW_RUN_ID) |
| 96 | + r = restart_job(REPO_OWNER, WORKFLOW_RUN_ID) |
| 97 | + print(r) |
| 98 | + |
| 99 | + shutil.rmtree("logs") |
| 100 | + os.remove("logs.zip") |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + eval_jobs() |
0 commit comments