summaryrefslogtreecommitdiffstats
path: root/hacking/azp/get_recent_coverage_runs.py
blob: 6a7fdae71fcef721852fea093c074296b1a3bcc5 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python

# (c) 2020 Red Hat, Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.utils.color import stringc
import requests
import sys
import datetime

# Following changes should be made to improve the overall style:
# TODO use argparse for arguments.
# TODO use new style formatting method.
# TODO use requests session.
# TODO type hints.

BRANCH = 'devel'
PIPELINE_ID = 20
MAX_AGE = datetime.timedelta(hours=24)

if len(sys.argv) > 1:
    BRANCH = sys.argv[1]


def get_coverage_runs():
    list_response = requests.get("https://dev.azure.com/ansible/ansible/_apis/pipelines/%s/runs?api-version=6.0-preview.1" % PIPELINE_ID)
    list_response.raise_for_status()

    runs = list_response.json()

    coverage_runs = []
    for run_summary in runs["value"][0:1000]:
        run_response = requests.get(run_summary['url'])
        run_response.raise_for_status()
        run = run_response.json()

        if run['resources']['repositories']['self']['refName'] != 'refs/heads/%s' % BRANCH:
            continue

        if 'finishedDate' in run_summary:
            age = datetime.datetime.now() - datetime.datetime.strptime(run['finishedDate'].split(".")[0], "%Y-%m-%dT%H:%M:%S")
            if age > MAX_AGE:
                break

        artifact_response = requests.get("https://dev.azure.com/ansible/ansible/_apis/build/builds/%s/artifacts?api-version=6.0" % run['id'])
        artifact_response.raise_for_status()

        artifacts = artifact_response.json()['value']
        if any([a["name"].startswith("Coverage") for a in artifacts]):
            # TODO wrongfully skipped if all jobs failed.
            coverage_runs.append(run)

    return coverage_runs


def pretty_coverage_runs(runs):
    ended = []
    in_progress = []
    for run in runs:
        if run.get('finishedDate'):
            ended.append(run)
        else:
            in_progress.append(run)

    for run in sorted(ended, key=lambda x: x['finishedDate']):
        if run['result'] == "succeeded":
            print('🙂 [%s] https://dev.azure.com/ansible/ansible/_build/results?buildId=%s (%s)' % (
                stringc('PASS', 'green'),
                run['id'],
                run['finishedDate']))
        else:
            print('😢 [%s] https://dev.azure.com/ansible/ansible/_build/results?buildId=%s (%s)' % (
                stringc('FAIL', 'red'),
                run['id'],
                run['finishedDate']))

    if in_progress:
        print('The following runs are ongoing:')
        for run in in_progress:
            print('🤔 [%s] https://dev.azure.com/ansible/ansible/_build/results?buildId=%s' % (
                stringc('FATE', 'yellow'),
                run['id']))


def main():
    pretty_coverage_runs(get_coverage_runs())


if __name__ == '__main__':
    main()