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

[usage]
5
    python get-workflow-status.py TRIGGER_PHRASE
6
7
8

TRIGGER_PHRASE: Code phrase that triggers workflow.
"""
9

10
11
12
13
import json
from os import environ
from sys import argv, exit
from time import sleep
14
from urllib import request
15
16


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

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

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


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