2020-07-09 05:46:56 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# Copyright (c) 2019-2020 Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# Author:
|
|
|
|
# Cleber Rosa <crosa@redhat.com>
|
|
|
|
#
|
|
|
|
# This work is licensed under the terms of the GNU GPL, version 2 or
|
|
|
|
# later. See the COPYING file in the top-level directory.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Checks the GitLab pipeline status for a given commit ID
|
|
|
|
"""
|
|
|
|
|
|
|
|
# pylint: disable=C0103
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import http.client
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
2020-09-04 19:42:57 +03:00
|
|
|
class CommunicationFailure(Exception):
|
|
|
|
"""Failed to communicate to gitlab.com APIs."""
|
|
|
|
|
|
|
|
|
|
|
|
class NoPipelineFound(Exception):
|
|
|
|
"""Communication is successfull but pipeline is not found."""
|
|
|
|
|
|
|
|
|
2020-11-17 20:36:30 +03:00
|
|
|
def get_local_branch_commit(branch):
|
2020-07-09 05:46:56 +03:00
|
|
|
"""
|
|
|
|
Returns the commit sha1 for the *local* branch named "staging"
|
|
|
|
"""
|
2020-09-04 19:42:52 +03:00
|
|
|
result = subprocess.run(['git', 'rev-parse', branch],
|
2020-07-09 05:46:56 +03:00
|
|
|
stdin=subprocess.DEVNULL,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.DEVNULL,
|
|
|
|
cwd=os.path.dirname(__file__),
|
|
|
|
universal_newlines=True).stdout.strip()
|
2020-09-04 19:42:52 +03:00
|
|
|
if result == branch:
|
|
|
|
raise ValueError("There's no local branch named '%s'" % branch)
|
2020-07-09 05:46:56 +03:00
|
|
|
if len(result) != 40:
|
2020-09-04 19:42:52 +03:00
|
|
|
raise ValueError("Branch '%s' HEAD doesn't look like a sha1" % branch)
|
2020-07-09 05:46:56 +03:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2021-02-22 22:32:38 +03:00
|
|
|
def get_json_http_response(url):
|
2020-07-09 05:46:56 +03:00
|
|
|
"""
|
2021-02-22 22:32:38 +03:00
|
|
|
Returns the JSON content of an HTTP GET request to gitlab.com
|
2020-07-09 05:46:56 +03:00
|
|
|
"""
|
|
|
|
connection = http.client.HTTPSConnection('gitlab.com')
|
|
|
|
connection.request('GET', url=url)
|
|
|
|
response = connection.getresponse()
|
|
|
|
if response.code != http.HTTPStatus.OK:
|
2021-02-22 22:32:39 +03:00
|
|
|
msg = "Received unsuccessful response: %s (%s)" % (response.code,
|
|
|
|
response.reason)
|
|
|
|
raise CommunicationFailure(msg)
|
2021-02-22 22:32:38 +03:00
|
|
|
return json.loads(response.read())
|
|
|
|
|
|
|
|
|
|
|
|
def get_pipeline_status(project_id, commit_sha1):
|
|
|
|
"""
|
|
|
|
Returns the JSON content of the pipeline status API response
|
|
|
|
"""
|
|
|
|
url = '/api/v4/projects/{}/pipelines?sha={}'.format(project_id,
|
|
|
|
commit_sha1)
|
|
|
|
json_response = get_json_http_response(url)
|
2020-07-09 05:46:56 +03:00
|
|
|
|
|
|
|
# As far as I can tell, there should be only one pipeline for the same
|
|
|
|
# project + commit. If this assumption is false, we can add further
|
|
|
|
# filters to the url, such as username, and order_by.
|
|
|
|
if not json_response:
|
2021-02-22 22:32:40 +03:00
|
|
|
msg = "No pipeline found for project %s and commit %s" % (project_id,
|
|
|
|
commit_sha1)
|
|
|
|
raise NoPipelineFound(msg)
|
2020-07-09 05:46:56 +03:00
|
|
|
return json_response[0]
|
|
|
|
|
|
|
|
|
|
|
|
def wait_on_pipeline_success(timeout, interval,
|
|
|
|
project_id, commit_sha):
|
|
|
|
"""
|
|
|
|
Waits for the pipeline to finish within the given timeout
|
|
|
|
"""
|
|
|
|
start = time.time()
|
|
|
|
while True:
|
|
|
|
if time.time() >= (start + timeout):
|
2020-09-04 19:42:53 +03:00
|
|
|
msg = ("Timeout (-t/--timeout) of %i seconds reached, "
|
|
|
|
"won't wait any longer for the pipeline to complete")
|
|
|
|
msg %= timeout
|
|
|
|
print(msg)
|
2020-07-09 05:46:56 +03:00
|
|
|
return False
|
|
|
|
|
2020-09-04 19:42:58 +03:00
|
|
|
try:
|
|
|
|
status = get_pipeline_status(project_id, commit_sha)
|
|
|
|
except NoPipelineFound:
|
|
|
|
print('Pipeline has not been found, it may not have been created yet.')
|
|
|
|
time.sleep(1)
|
|
|
|
continue
|
|
|
|
|
|
|
|
pipeline_status = status['status']
|
|
|
|
status_to_wait = ('created', 'waiting_for_resource', 'preparing',
|
|
|
|
'pending', 'running')
|
|
|
|
if pipeline_status in status_to_wait:
|
|
|
|
print('%s...' % pipeline_status)
|
2020-09-04 19:42:54 +03:00
|
|
|
time.sleep(interval)
|
2020-07-09 05:46:56 +03:00
|
|
|
continue
|
|
|
|
|
2020-09-04 19:42:58 +03:00
|
|
|
if pipeline_status == 'success':
|
2020-07-09 05:46:56 +03:00
|
|
|
return True
|
|
|
|
|
|
|
|
msg = "Pipeline failed, check: %s" % status['web_url']
|
|
|
|
print(msg)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-09-04 19:42:55 +03:00
|
|
|
def create_parser():
|
2020-07-09 05:46:56 +03:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
prog='pipeline-status',
|
|
|
|
description='check or wait on a pipeline status')
|
|
|
|
|
|
|
|
parser.add_argument('-t', '--timeout', type=int, default=7200,
|
|
|
|
help=('Amount of time (in seconds) to wait for the '
|
|
|
|
'pipeline to complete. Defaults to '
|
|
|
|
'%(default)s'))
|
|
|
|
parser.add_argument('-i', '--interval', type=int, default=60,
|
|
|
|
help=('Amount of time (in seconds) to wait between '
|
|
|
|
'checks of the pipeline status. Defaults '
|
|
|
|
'to %(default)s'))
|
|
|
|
parser.add_argument('-w', '--wait', action='store_true', default=False,
|
|
|
|
help=('Wether to wait, instead of checking only once '
|
|
|
|
'the status of a pipeline'))
|
|
|
|
parser.add_argument('-p', '--project-id', type=int, default=11167699,
|
|
|
|
help=('The GitLab project ID. Defaults to the project '
|
|
|
|
'for https://gitlab.com/qemu-project/qemu, that '
|
|
|
|
'is, "%(default)s"'))
|
2020-11-17 20:36:30 +03:00
|
|
|
parser.add_argument('-b', '--branch', type=str, default="staging",
|
|
|
|
help=('Specify the branch to check. '
|
|
|
|
'Use HEAD for your current branch. '
|
|
|
|
'Otherwise looks at "%(default)s"'))
|
|
|
|
parser.add_argument('-c', '--commit',
|
|
|
|
default=None,
|
2020-07-09 05:46:56 +03:00
|
|
|
help=('Look for a pipeline associated with the given '
|
|
|
|
'commit. If one is not explicitly given, the '
|
2020-11-17 20:36:30 +03:00
|
|
|
'commit associated with the default branch '
|
|
|
|
'is used.'))
|
2020-07-09 05:46:56 +03:00
|
|
|
parser.add_argument('--verbose', action='store_true', default=False,
|
|
|
|
help=('A minimal verbosity level that prints the '
|
|
|
|
'overall result of the check/wait'))
|
2020-09-04 19:42:55 +03:00
|
|
|
return parser
|
2020-07-09 05:46:56 +03:00
|
|
|
|
2020-09-04 19:42:55 +03:00
|
|
|
def main():
|
|
|
|
"""
|
|
|
|
Script entry point
|
|
|
|
"""
|
|
|
|
parser = create_parser()
|
2020-07-09 05:46:56 +03:00
|
|
|
args = parser.parse_args()
|
2020-11-17 20:36:30 +03:00
|
|
|
|
|
|
|
if not args.commit:
|
|
|
|
args.commit = get_local_branch_commit(args.branch)
|
|
|
|
|
2020-09-04 19:42:56 +03:00
|
|
|
success = False
|
2020-07-09 05:46:56 +03:00
|
|
|
try:
|
|
|
|
if args.wait:
|
|
|
|
success = wait_on_pipeline_success(
|
|
|
|
args.timeout,
|
|
|
|
args.interval,
|
|
|
|
args.project_id,
|
|
|
|
args.commit)
|
|
|
|
else:
|
|
|
|
status = get_pipeline_status(args.project_id,
|
|
|
|
args.commit)
|
|
|
|
success = status['status'] == 'success'
|
|
|
|
except Exception as error: # pylint: disable=W0703
|
|
|
|
if args.verbose:
|
|
|
|
print("ERROR: %s" % error.args[0])
|
2020-09-04 19:42:56 +03:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
if args.verbose:
|
|
|
|
print("Exiting on user's request")
|
2020-07-09 05:46:56 +03:00
|
|
|
|
|
|
|
if success:
|
|
|
|
if args.verbose:
|
|
|
|
print('success')
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
if args.verbose:
|
|
|
|
print('failure')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|