File: get-github-actions-status.py

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (54 lines) | stat: -rwxr-xr-x 1,559 bytes parent folder | download | duplicates (3)
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
#!/bin/env python3

import json
import sys
import urllib.parse
import urllib.request
from time import sleep


def get_workflow_runs(workflow):
    url = "https://api.github.com/repos/syslog-ng/syslog-ng/actions/workflows/{}.yml/runs?branch=master".format(workflow)
    request = urllib.request.Request(url, None, {})
    try:
        with urllib.request.urlopen(request) as response:
            binary_response = response.read()
    except urllib.error.HTTPError:
        print('"' + workflow + '" is not a valid workflow. See .github/workflows/')
        sys.exit(1)

    runs = json.loads(binary_response.decode())["workflow_runs"]
    if len(runs) == 0:
        print('No runs have been executed of "' + workflow + '" yet.')
        sys.exit(1)

    return runs


def main():
    workflow = sys.argv[1]
    while True:
        runs = get_workflow_runs(workflow)
        not_pr_runs = filter(lambda run: run["event"] != "pull_request", runs)
        latest_run = max(not_pr_runs, key=lambda run: run["updated_at"])

        if latest_run["status"] != "completed":
            print("Job is still running. Trying again in 10 minutes.")
            sleep(10 * 60)
            continue

        result = latest_run["conclusion"]

        print('"' + latest_run["name"] + '" job finished.')
        print("Date: " + latest_run["updated_at"])
        print("URL: " + latest_run["html_url"])
        print("Result: " + result)

        if result == "success":
            sys.exit(0)
        else:
            sys.exit(1)


if __name__ == "__main__":
    main()