summaryrefslogtreecommitdiffstats
path: root/ci/travis.py
blob: f0909ae157aee3b4211fdf5305f6200522850c22 (plain)
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
#!/usr/bin/python3
import json
import time
import sys

import requests


BRANCH_API_ENDPOINT = "https://api.travis-ci.com/repos/CZ-NIC/knot-resolver/branches/{branch}"
JOB_URL = "https://travis-ci.com/CZ-NIC/knot-resolver/jobs/{job_id}"
TIMEOUT = 600  # 10 mins max
POLL_DELAY = 15

job_id = None


def exit(msg='', code=1):
    print(msg, file=sys.stderr)
    if job_id is not None:
        print(JOB_URL.format(job_id=job_id))
    sys.exit(code)


end_time = time.time() + TIMEOUT
while time.time() < end_time:
    response = requests.get(
        BRANCH_API_ENDPOINT.format(branch=sys.argv[1]),
        headers={"Accept": "application/vnd.travis-ci.2.1+json"})
    if response.status_code == 404:
        pass  # not created yet?
    elif response.status_code == 200:
        data = json.loads(response.content.decode('utf-8'))
        state = data['branch']['state']
        try:
            job_id = data['branch']['job_ids'][0]
        except KeyError:
            pass

        if state == "errored":
            exit("Travis CI Result: ERRORED!")
        elif state == "passed":
            exit("Travis CI Result: PASSED!", code=0)
    else:
        exit("API Response Code: {code}".format(response.status_code), code=2)
    time.sleep(POLL_DELAY)

exit("Timed out!")