Debian Codegraph @

hacking/azp/run.py

raw
1 #!/usr/bin/env python
2 # PYTHON_ARGCOMPLETE_OK
3
4 # (c) 2016 Red Hat, Inc.
5 #
6 # This file is part of Ansible
7 #
8 # Ansible is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Ansible is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
20
21 """CLI tool for starting new CI runs."""
22
23 from __future__ import annotations
24
25 # noinspection PyCompatibility
26 import argparse
27 import json
28 import os
29 import sys
30 import requests
31 import requests.auth
32
33 try:
34 import argcomplete
35 except ImportError:
36 argcomplete = None
37
38 # TODO: Dev does not have a token for AZP, somebody please test this.
39
40 # Following changes should be made to improve the overall style:
41 # TODO use new style formatting method.
42 # TODO type hints.
43
44
45 def main():
46 """Main program body."""
47
48 args = parse_args()
49
50 key = os.environ.get('AZP_TOKEN', None)
51 if not key:
52 sys.stderr.write("please set you AZP token in AZP_TOKEN")
53 sys.exit(1)
54
55 start_run(args, key)
56
57
58 def parse_args():
59 """Parse and return args."""
60
61 parser = argparse.ArgumentParser(description='Start a new CI run.')
62
63 parser.add_argument('-p', '--pipeline-id', type=int, default=20, help='pipeline to download the job from')
64 parser.add_argument('--ref', help='git ref name to run on')
65
66 parser.add_argument('--env',
67 nargs=2,
68 metavar=('KEY', 'VALUE'),
69 action='append',
70 help='environment variable to pass')
71
72 if argcomplete:
73 argcomplete.autocomplete(parser)
74
75 args = parser.parse_args()
76
77 return args
78
79
80 def start_run(args, key):
81 """Start a new CI run."""
82
83 url = "https://dev.azure.com/ansible/ansible/_apis/pipelines/%s/runs?api-version=6.0-preview.1" % args.pipeline_id
84 payload = {"resources": {"repositories": {"self": {"refName": args.ref}}}}
85
86 resp = requests.post(url, auth=requests.auth.HTTPBasicAuth('user', key), data=payload)
87 resp.raise_for_status()
88
89 print(json.dumps(resp.json(), indent=4, sort_keys=True))
90
91
92 if __name__ == '__main__':
93 main()