hw/arm/bcm2835_peripherals: Correctly wire the SYS_timer IRQs
[qemu/ar7.git] / scripts / ci / gitlab-pipeline-status
blobbac8233079bb60d57074314be7af8b234e3ac90e
1 #!/usr/bin/env python3
3 # Copyright (c) 2019-2020 Red Hat, Inc.
5 # Author:
6 #  Cleber Rosa <crosa@redhat.com>
8 # This work is licensed under the terms of the GNU GPL, version 2 or
9 # later.  See the COPYING file in the top-level directory.
11 """
12 Checks the GitLab pipeline status for a given commit ID
13 """
15 # pylint: disable=C0103
17 import argparse
18 import http.client
19 import json
20 import os
21 import subprocess
22 import time
23 import sys
26 class CommunicationFailure(Exception):
27     """Failed to communicate to gitlab.com APIs."""
30 class NoPipelineFound(Exception):
31     """Communication is successfull but pipeline is not found."""
34 def get_local_branch_commit(branch='staging'):
35     """
36     Returns the commit sha1 for the *local* branch named "staging"
37     """
38     result = subprocess.run(['git', 'rev-parse', branch],
39                             stdin=subprocess.DEVNULL,
40                             stdout=subprocess.PIPE,
41                             stderr=subprocess.DEVNULL,
42                             cwd=os.path.dirname(__file__),
43                             universal_newlines=True).stdout.strip()
44     if result == branch:
45         raise ValueError("There's no local branch named '%s'" % branch)
46     if len(result) != 40:
47         raise ValueError("Branch '%s' HEAD doesn't look like a sha1" % branch)
48     return result
51 def get_pipeline_status(project_id, commit_sha1):
52     """
53     Returns the JSON content of the pipeline status API response
54     """
55     url = '/api/v4/projects/{}/pipelines?sha={}'.format(project_id,
56                                                         commit_sha1)
57     connection = http.client.HTTPSConnection('gitlab.com')
58     connection.request('GET', url=url)
59     response = connection.getresponse()
60     if response.code != http.HTTPStatus.OK:
61         raise CommunicationFailure("Failed to receive a successful response")
62     json_response = json.loads(response.read())
64     # As far as I can tell, there should be only one pipeline for the same
65     # project + commit. If this assumption is false, we can add further
66     # filters to the url, such as username, and order_by.
67     if not json_response:
68         raise NoPipelineFound("No pipeline found")
69     return json_response[0]
72 def wait_on_pipeline_success(timeout, interval,
73                              project_id, commit_sha):
74     """
75     Waits for the pipeline to finish within the given timeout
76     """
77     start = time.time()
78     while True:
79         if time.time() >= (start + timeout):
80             msg = ("Timeout (-t/--timeout) of %i seconds reached, "
81                    "won't wait any longer for the pipeline to complete")
82             msg %= timeout
83             print(msg)
84             return False
86         try:
87             status = get_pipeline_status(project_id, commit_sha)
88         except NoPipelineFound:
89             print('Pipeline has not been found, it may not have been created yet.')
90             time.sleep(1)
91             continue
93         pipeline_status = status['status']
94         status_to_wait = ('created', 'waiting_for_resource', 'preparing',
95                           'pending', 'running')
96         if pipeline_status in status_to_wait:
97             print('%s...' % pipeline_status)
98             time.sleep(interval)
99             continue
101         if pipeline_status == 'success':
102             return True
104         msg = "Pipeline failed, check: %s" % status['web_url']
105         print(msg)
106         return False
109 def create_parser():
110     parser = argparse.ArgumentParser(
111         prog='pipeline-status',
112         description='check or wait on a pipeline status')
114     parser.add_argument('-t', '--timeout', type=int, default=7200,
115                         help=('Amount of time (in seconds) to wait for the '
116                               'pipeline to complete.  Defaults to '
117                               '%(default)s'))
118     parser.add_argument('-i', '--interval', type=int, default=60,
119                         help=('Amount of time (in seconds) to wait between '
120                               'checks of the pipeline status.  Defaults '
121                               'to %(default)s'))
122     parser.add_argument('-w', '--wait', action='store_true', default=False,
123                         help=('Wether to wait, instead of checking only once '
124                               'the status of a pipeline'))
125     parser.add_argument('-p', '--project-id', type=int, default=11167699,
126                         help=('The GitLab project ID. Defaults to the project '
127                               'for https://gitlab.com/qemu-project/qemu, that '
128                               'is, "%(default)s"'))
129     try:
130         default_commit = get_local_branch_commit()
131         commit_required = False
132     except ValueError:
133         default_commit = ''
134         commit_required = True
135     parser.add_argument('-c', '--commit', required=commit_required,
136                         default=default_commit,
137                         help=('Look for a pipeline associated with the given '
138                               'commit.  If one is not explicitly given, the '
139                               'commit associated with the local branch named '
140                               '"staging" is used.  Default: %(default)s'))
141     parser.add_argument('--verbose', action='store_true', default=False,
142                         help=('A minimal verbosity level that prints the '
143                               'overall result of the check/wait'))
144     return parser
146 def main():
147     """
148     Script entry point
149     """
150     parser = create_parser()
151     args = parser.parse_args()
152     success = False
153     try:
154         if args.wait:
155             success = wait_on_pipeline_success(
156                 args.timeout,
157                 args.interval,
158                 args.project_id,
159                 args.commit)
160         else:
161             status = get_pipeline_status(args.project_id,
162                                          args.commit)
163             success = status['status'] == 'success'
164     except Exception as error:      # pylint: disable=W0703
165         if args.verbose:
166             print("ERROR: %s" % error.args[0])
167     except KeyboardInterrupt:
168         if args.verbose:
169             print("Exiting on user's request")
171     if success:
172         if args.verbose:
173             print('success')
174         sys.exit(0)
175     else:
176         if args.verbose:
177             print('failure')
178         sys.exit(1)
181 if __name__ == '__main__':
182     main()