get_workflow_status.py 2.7 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

14
15
16
17
18
19
try:
    from urllib import request
except ImportError:
    import urllib2 as request


20
def get_runs(trigger_phrase):
21
22
23
24
    """Get all triggering workflow comments in the current PR.

    Parameters
    ----------
25
    trigger_phrase : str
26
        Code phrase that triggers workflow.
27
28
29
30
31
32
33
34

    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":
35
        pr_number = int(environ.get("GITHUB_REF").split("/")[-2])
36
37
38
39
        page = 1
        while True:
            req = request.Request(
                url="{}/repos/microsoft/LightGBM/issues/{}/comments?page={}&per_page=100".format(
40
                    environ.get("GITHUB_API_URL"), pr_number, page
41
                ),
42
                headers={"Accept": "application/vnd.github.v3+json"},
43
44
            )
            url = request.urlopen(req)
45
            data = json.loads(url.read().decode("utf-8"))
46
47
48
            url.close()
            if not data:
                break
49
50
51
52
53
54
            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))
            ]
55
56
            pr_runs.extend(runs_on_page)
            page += 1
57
58
59
60
61
62
63
64
65
66
67
68
69
    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
    -------
70
    status : str
71
72
73
        The most recent status of workflow.
        Can be 'success', 'failure' or 'in-progress'.
    """
74
    status = "success"
75
    for run in runs:
76
        body = run["body"]
77
78
79
80
        if "Status: " in body:
            if "Status: skipped" in body:
                continue
            if "Status: failure" in body:
81
                status = "failure"
82
83
                break
            if "Status: success" in body:
84
                status = "success"
85
86
                break
        else:
87
            status = "in-progress"
88
89
90
91
92
            break
    return status


if __name__ == "__main__":
93
    trigger_phrase = argv[1]
94
    while True:
95
        status = get_status(get_runs(trigger_phrase))
96
        if status != "in-progress":
97
98
            break
        sleep(60)
99
    if status == "failure":
100
        exit(1)