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 def get_local_branch_commit(branch='staging'):
28 Returns the commit sha1 for the *local* branch named "staging"
30 result = subprocess.run(['git', 'rev-parse', branch],
31 stdin=subprocess.DEVNULL,
32 stdout=subprocess.PIPE,
33 stderr=subprocess.DEVNULL,
34 cwd=os.path.dirname(__file__),
35 universal_newlines=True).stdout.strip()
37 raise ValueError("There's no local branch named '%s'" % branch)
39 raise ValueError("Branch '%s' HEAD doesn't look like a sha1" % branch)
43 def get_pipeline_status(project_id, commit_sha1):
45 Returns the JSON content of the pipeline status API response
47 url = '/api/v4/projects/{}/pipelines?sha={}'.format(project_id,
49 connection = http.client.HTTPSConnection('gitlab.com')
50 connection.request('GET', url=url)
51 response = connection.getresponse()
52 if response.code != http.HTTPStatus.OK:
53 raise ValueError("Failed to receive a successful response")
54 json_response = json.loads(response.read())
56 # As far as I can tell, there should be only one pipeline for the same
57 # project + commit. If this assumption is false, we can add further
58 # filters to the url, such as username, and order_by.
60 raise ValueError("No pipeline found")
61 return json_response[0]
64 def wait_on_pipeline_success(timeout, interval,
65 project_id, commit_sha):
67 Waits for the pipeline to finish within the given timeout
71 if time.time() >= (start + timeout):
72 msg = ("Timeout (-t/--timeout) of %i seconds reached, "
73 "won't wait any longer for the pipeline to complete")
78 status = get_pipeline_status(project_id, commit_sha)
79 if status['status'] == 'running':
84 if status['status'] == 'success':
87 msg = "Pipeline failed, check: %s" % status['web_url']
96 parser = argparse.ArgumentParser(
97 prog='pipeline-status',
98 description='check or wait on a pipeline status')
100 parser.add_argument('-t', '--timeout', type=int, default=7200,
101 help=('Amount of time (in seconds) to wait for the '
102 'pipeline to complete. Defaults to '
104 parser.add_argument('-i', '--interval', type=int, default=60,
105 help=('Amount of time (in seconds) to wait between '
106 'checks of the pipeline status. Defaults '
108 parser.add_argument('-w', '--wait', action='store_true', default=False,
109 help=('Wether to wait, instead of checking only once '
110 'the status of a pipeline'))
111 parser.add_argument('-p', '--project-id', type=int, default=11167699,
112 help=('The GitLab project ID. Defaults to the project '
113 'for https://gitlab.com/qemu-project/qemu, that '
114 'is, "%(default)s"'))
116 default_commit = get_local_branch_commit()
117 commit_required = False
120 commit_required = True
121 parser.add_argument('-c', '--commit', required=commit_required,
122 default=default_commit,
123 help=('Look for a pipeline associated with the given '
124 'commit. If one is not explicitly given, the '
125 'commit associated with the local branch named '
126 '"staging" is used. Default: %(default)s'))
127 parser.add_argument('--verbose', action='store_true', default=False,
128 help=('A minimal verbosity level that prints the '
129 'overall result of the check/wait'))
131 args = parser.parse_args()
135 success = wait_on_pipeline_success(
141 status = get_pipeline_status(args.project_id,
143 success = status['status'] == 'success'
144 except Exception as error: # pylint: disable=W0703
147 print("ERROR: %s" % error.args[0])
159 if __name__ == '__main__':