get_workflow_status.py 2.64 KB
Newer Older
1
# coding: utf-8
2
3
4
5
6
7
8
"""Get the most recent status of workflow for the current PR.

[usage]
    python get_workflow_status.py TRIGGER_PHRASE

TRIGGER_PHRASE: Code phrase that triggers workflow.
"""
9
10
11
12
import json
from os import environ
from sys import argv, exit
from time import sleep
13
from urllib import request
14
15


16
def get_runs(trigger_phrase):
17
18
19
20
    """Get all triggering workflow comments in the current PR.

    Parameters
    ----------
21
    trigger_phrase : str
22
        Code phrase that triggers workflow.
23
24
25
26
27
28
29
30

    Returns
    -------
    pr_runs : list
        List of comment objects sorted by the time of creation in decreasing order.
    """
    pr_runs = []
    if environ.get("GITHUB_EVENT_NAME", "") == "pull_request":
31
        pr_number = int(environ.get("GITHUB_REF").split("/")[-2])
32
33
34
35
        page = 1
        while True:
            req = request.Request(
                url="{}/repos/microsoft/LightGBM/issues/{}/comments?page={}&per_page=100".format(
36
                    environ.get("GITHUB_API_URL"), pr_number, page
37
                ),
38
                headers={"Accept": "application/vnd.github.v3+json"},
39
40
            )
            url = request.urlopen(req)
41
            data = json.loads(url.read().decode("utf-8"))
42
43
44
            url.close()
            if not data:
                break
45
46
47
48
49
50
            runs_on_page = [
                i
                for i in data
                if i["author_association"].lower() in {"owner", "member", "collaborator"}
                and i["body"].startswith("/gha run {}".format(trigger_phrase))
            ]
51
52
            pr_runs.extend(runs_on_page)
            page += 1
53
54
55
56
57
58
59
60
61
62
63
64
65
    return pr_runs[::-1]


def get_status(runs):
    """Get the most recent status of workflow for the current PR.

    Parameters
    ----------
    runs : list
        List of comment objects sorted by the time of creation in decreasing order.

    Returns
    -------
66
    status : str
67
68
69
        The most recent status of workflow.
        Can be 'success', 'failure' or 'in-progress'.
    """
70
    status = "success"
71
    for run in runs:
72
        body = run["body"]
73
74
75
76
        if "Status: " in body:
            if "Status: skipped" in body:
                continue
            if "Status: failure" in body:
77
                status = "failure"
78
79
                break
            if "Status: success" in body:
80
                status = "success"
81
82
                break
        else:
83
            status = "in-progress"
84
85
86
87
88
            break
    return status


if __name__ == "__main__":
89
    trigger_phrase = argv[1]
90
    while True:
91
        status = get_status(get_runs(trigger_phrase))
92
        if status != "in-progress":
93
94
            break
        sleep(60)
95
    if status == "failure":
96
        exit(1)