1#!/usr/bin/env python3
2#
3# Copyright (c) 2019-2020 Red Hat, Inc.
4#
5# Author:
6#  Cleber Rosa <crosa@redhat.com>
7#
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.
10
11"""
12Checks the GitLab pipeline status for a given commit ID
13"""
14
15# pylint: disable=C0103
16
17import argparse
18import http.client
19import json
20import os
21import subprocess
22import time
23import sys
24
25
26class CommunicationFailure(Exception):
27    """Failed to communicate to gitlab.com APIs."""
28
29
30class NoPipelineFound(Exception):
31    """Communication is successfull but pipeline is not found."""
32
33
34def get_local_branch_commit(branch):
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
49
50
51def get_json_http_response(url):
52    """
53    Returns the JSON content of an HTTP GET request to gitlab.com
54    """
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,
60                                                           response.reason)
61        raise CommunicationFailure(msg)
62    return json.loads(response.read())
63
64
65def get_pipeline_status(project_id, commit_sha1):
66    """
67    Returns the JSON content of the pipeline status API response
68    """
69    url = '/api/v4/projects/{}/pipelines?sha={}'.format(project_id,
70                                                        commit_sha1)
71    json_response = get_json_http_response(url)
72
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.
76    if not json_response:
77        msg = "No pipeline found for project %s and commit %s" % (project_id,
78                                                                  commit_sha1)
79        raise NoPipelineFound(msg)
80    return json_response[0]
81
82
83def wait_on_pipeline_success(timeout, interval,
84                             project_id, commit_sha):
85    """
86    Waits for the pipeline to finish within the given timeout
87    """
88    start = time.time()
89    while True:
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")
93            msg %= timeout
94            print(msg)
95            return False
96
97        try:
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.')
101            time.sleep(1)
102            continue
103
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)
109            time.sleep(interval)
110            continue
111
112        if pipeline_status == 'success':
113            return True
114
115        msg = "Pipeline failed, check: %s" % status['web_url']
116        print(msg)
117        return False
118
119
120def create_parser():
121    parser = argparse.ArgumentParser(
122        prog='pipeline-status',
123        description='check or wait on a pipeline status')
124
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 '
128                              '%(default)s'))
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 '
132                              'to %(default)s'))
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',
145                        default=None,
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 '
149                              'is used.'))
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'))
153    return parser
154
155def main():
156    """
157    Script entry point
158    """
159    parser = create_parser()
160    args = parser.parse_args()
161
162    if not args.commit:
163        args.commit = get_local_branch_commit(args.branch)
164
165    success = False
166    try:
167        if args.wait:
168            success = wait_on_pipeline_success(
169                args.timeout,
170                args.interval,
171                args.project_id,
172                args.commit)
173        else:
174            status = get_pipeline_status(args.project_id,
175                                         args.commit)
176            success = status['status'] == 'success'
177    except Exception as error:      # pylint: disable=W0703
178        if args.verbose:
179            print("ERROR: %s" % error.args[0])
180    except KeyboardInterrupt:
181        if args.verbose:
182            print("Exiting on user's request")
183
184    if success:
185        if args.verbose:
186            print('success')
187        sys.exit(0)
188    else:
189        if args.verbose:
190            print('failure')
191        sys.exit(1)
192
193
194if __name__ == '__main__':
195    main()
196