File size: 2,915 Bytes
3498650
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import requests

WORKFLOW_ID = "90575235"

def get_daily_ci_runs(token, num_runs=7, workflow_id=None):
    """Get the workflow runs of the scheduled (daily) CI.

    This only selects the runs triggered by the `schedule` event on the `main` branch.
    """
    headers = None
    if token is not None:
        headers = {"Accept": "application/vnd.github+json", "Authorization": f"Bearer {token}"}

    # The id of a workflow (not of a workflow run).
    # From a given workflow run (where we have workflow run id), we can get the workflow id by going to
    # https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}
    # and check the `workflow_id` key.

    if not workflow_id:
        workflow_run_id = os.environ["GITHUB_RUN_ID"]
        workflow_run = requests.get(
            f"https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}", headers=headers
        ).json()
        workflow_id = workflow_run["workflow_id"]

    url = f"https://api.github.com/repos/huggingface/transformers/actions/workflows/{workflow_id}/runs"
    # On `main` branch + event being `schedule` + not returning PRs + only `num_runs` results
    url += f"?branch=main&exclude_pull_requests=true&per_page={num_runs}"

    result = requests.get(f"{url}&event=schedule", headers=headers).json()
    workflow_runs = result["workflow_runs"]
    if len(workflow_runs) == 0:
        result = requests.get(f"{url}&event=workflow_run", headers=headers).json()
        workflow_runs = result["workflow_runs"]

    return workflow_runs


def get_last_daily_ci_run(token, workflow_run_id=None, workflow_id=None, commit_sha=None):
    """Get the last completed workflow run id of the scheduled (daily) CI."""
    headers = None
    if token is not None:
        headers = {"Accept": "application/vnd.github+json", "Authorization": f"Bearer {token}"}

    workflow_run = None
    if workflow_run_id is not None and workflow_run_id != "":
        workflow_run = requests.get(
            f"https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}", headers=headers
        ).json()
        return workflow_run

    workflow_runs = get_daily_ci_runs(token, workflow_id=workflow_id)
    for run in workflow_runs:
        if commit_sha in [None, ""] and run["status"] == "completed":
            workflow_run = run
            break
        # if `commit_sha` is specified, return the latest completed run with `workflow_run["head_sha"]` matching the specified sha.
        elif commit_sha not in [None, ""] and run["head_sha"] == commit_sha and run["status"] == "completed":
            workflow_run = run
            break

    return workflow_run


def get_last_ci_run_id():
    token = os.environ.get("GITHUB_TOKEN")
    workflow_run = get_last_daily_ci_run(token, workflow_id=WORKFLOW_ID)
    return workflow_run["id"]


print(get_last_ci_run_id())