3 # Copyright (c) 2019-2020 Red Hat, Inc.
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.
12 Checks the GitLab pipeline status for a given commit ID
15 # pylint: disable=C0103
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):
36 Returns the commit sha1 for the *local* branch named "staging"
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()
45 raise ValueError("There's no local branch named '%s'" % branch)
47 raise ValueError("Branch '%s' HEAD doesn't look like a sha1" % branch)
51 def get_json_http_response(url):
53 Returns the JSON content of an HTTP GET request to gitlab.com
55 connection = http.client.HTTPSConnection('gitlab.com')
56 connection.request('GET', url=url)
57 response = connection.getresponse()
58 if response.code != http.HTTPStatus.OK:
59 msg = "Received unsuccessful response: %s (%s)" % (response.code,
61 raise CommunicationFailure(msg)
62 return json.loads(response.read())
65 def get_pipeline_status(project_id, commit_sha1):
67 Returns the JSON content of the pipeline status API response
69 url = '/api/v4/projects/{}/pipelines?sha={}'.format(project_id,
71 json_response = get_json_http_response(url)
73 # As far as I can tell, there should be only one pipeline for the same
74 # project + commit. If this assumption is false, we can add further
75 # filters to the url, such as username, and order_by.
77 msg = "No pipeline found for project %s and commit %s" % (project_id,
79 raise NoPipelineFound(msg)
80 return json_response[0]
83 def wait_on_pipeline_success(timeout, interval,
84 project_id, commit_sha):
86 Waits for the pipeline to finish within the given timeout
90 if time.time() >= (start + timeout):
91 msg = ("Timeout (-t/--timeout) of %i seconds reached, "
92 "won't wait any longer for the pipeline to complete")
98 status = get_pipeline_status(project_id, commit_sha)
99 except NoPipelineFound:
100 print('Pipeline has not been found, it may not have been created yet.')
104 pipeline_status = status['status']
105 status_to_wait = ('created', 'waiting_for_resource', 'preparing',
106 'pending', 'running')
107 if pipeline_status in status_to_wait:
108 print('%s...' % pipeline_status)
112 if pipeline_status == 'success':
115 msg = "Pipeline failed, check: %s" % status['web_url']
121 parser = argparse.ArgumentParser(
122 prog='pipeline-status',
123 description='check or wait on a pipeline status')
125 parser.add_argument('-t', '--timeout', type=int, default=7200,
126 help=('Amount of time (in seconds) to wait for the '
127 'pipeline to complete. Defaults to '
129 parser.add_argument('-i', '--interval', type=int, default=60,
130 help=('Amount of time (in seconds) to wait between '
131 'checks of the pipeline status. Defaults '
133 parser.add_argument('-w', '--wait', action='store_true', default=False,
134 help=('Wether to wait, instead of checking only once '
135 'the status of a pipeline'))
136 parser.add_argument('-p', '--project-id', type=int, default=11167699,
137 help=('The GitLab project ID. Defaults to the project '
138 'for https://gitlab.com/qemu-project/qemu, that '
139 'is, "%(default)s"'))
140 parser.add_argument('-b', '--branch', type=str, default="staging",
141 help=('Specify the branch to check. '
142 'Use HEAD for your current branch. '
143 'Otherwise looks at "%(default)s"'))
144 parser.add_argument('-c', '--commit',
146 help=('Look for a pipeline associated with the given '
147 'commit. If one is not explicitly given, the '
148 'commit associated with the default branch '
150 parser.add_argument('--verbose', action='store_true', default=False,
151 help=('A minimal verbosity level that prints the '
152 'overall result of the check/wait'))
159 parser = create_parser()
160 args = parser.parse_args()
163 args.commit = get_local_branch_commit(args.branch)
168 success = wait_on_pipeline_success(
174 status = get_pipeline_status(args.project_id,
176 success = status['status'] == 'success'
177 except Exception as error: # pylint: disable=W0703
179 print("ERROR: %s" % error.args[0])
180 except KeyboardInterrupt:
182 print("Exiting on user's request")
194 if __name__ == '__main__':