Debian Codegraph @

hacking/azp/get_recent_coverage_runs.py

raw
1 #!/usr/bin/env python
2
3 # (c) 2020 Red Hat, Inc.
4 #
5 # This file is part of Ansible
6 #
7 # Ansible is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Ansible is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
19
20 from __future__ import annotations
21
22 from ansible.utils.color import stringc
23 import requests
24 import sys
25 import datetime
26
27 # Following changes should be made to improve the overall style:
28 # TODO use argparse for arguments.
29 # TODO use new style formatting method.
30 # TODO use requests session.
31 # TODO type hints.
32
33 BRANCH = 'devel'
34 PIPELINE_ID = 20
35 MAX_AGE = datetime.timedelta(hours=24)
36
37 if len(sys.argv) > 1:
38 BRANCH = sys.argv[1]
39
40
41 def get_coverage_runs():
42 list_response = requests.get("https://dev.azure.com/ansible/ansible/_apis/pipelines/%s/runs?api-version=6.0-preview.1" % PIPELINE_ID)
43 list_response.raise_for_status()
44
45 runs = list_response.json()
46
47 coverage_runs = []
48 for run_summary in runs["value"][0:1000]:
49 run_response = requests.get(run_summary['url'])
50
51 if run_response.status_code == 500 and 'Cannot serialize type Microsoft.Azure.Pipelines.WebApi.ContainerResource' in run_response.json()['message']:
52 # This run used a container resource, which AZP can no longer serialize for anonymous requests.
53 # Assume all older requests have this issue as well and stop further processing of runs.
54 # The issue was reported here: https://developercommunity.visualstudio.com/t/Pipelines-API-serialization-error-for-an/10294532
55 # A work-around for this issue was applied in: https://github.com/ansible/ansible/pull/80299
56 break
57
58 run_response.raise_for_status()
59 run = run_response.json()
60
61 if run['resources']['repositories']['self']['refName'] != 'refs/heads/%s' % BRANCH:
62 continue
63
64 if 'finishedDate' in run_summary:
65 age = datetime.datetime.now() - datetime.datetime.strptime(run['finishedDate'].split(".")[0], "%Y-%m-%dT%H:%M:%S")
66 if age > MAX_AGE:
67 break
68
69 artifact_response = requests.get("https://dev.azure.com/ansible/ansible/_apis/build/builds/%s/artifacts?api-version=6.0" % run['id'])
70 artifact_response.raise_for_status()
71
72 artifacts = artifact_response.json()['value']
73 if any(a["name"].startswith("Coverage") for a in artifacts):
74 # TODO wrongfully skipped if all jobs failed.
75 coverage_runs.append(run)
76
77 return coverage_runs
78
79
80 def pretty_coverage_runs(runs):
81 ended = []
82 in_progress = []
83 for run in runs:
84 if run.get('finishedDate'):
85 ended.append(run)
86 else:
87 in_progress.append(run)
88
89 for run in sorted(ended, key=lambda x: x['finishedDate']):
90 if run['result'] == "succeeded":
91 print('🙂 [%s] https://dev.azure.com/ansible/ansible/_build/results?buildId=%s (%s)' % (
92 stringc('PASS', 'green'),
93 run['id'],
94 run['finishedDate']))
95 else:
96 print('😢 [%s] https://dev.azure.com/ansible/ansible/_build/results?buildId=%s (%s)' % (
97 stringc('FAIL', 'red'),
98 run['id'],
99 run['finishedDate']))
100
101 if in_progress:
102 print('The following runs are ongoing:')
103 for run in in_progress:
104 print('🤔 [%s] https://dev.azure.com/ansible/ansible/_build/results?buildId=%s' % (
105 stringc('FATE', 'yellow'),
106 run['id']))
107
108
109 def main():
110 pretty_coverage_runs(get_coverage_runs())
111
112
113 if __name__ == '__main__':
114 main()