1#!/usr/bin/env python2
2#
3# Copyright (C) 2015 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the 'License');
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an 'AS IS' BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17import json
18import logging
19import os
20
21from apscheduler.schedulers.background import BackgroundScheduler
22from flask import Flask, request
23import requests
24
25import gerrit
26import tasks
27
28app = Flask(__name__)
29
30
31def gerrit_url(endpoint):
32    gerrit_base_url = 'https://android-review.googlesource.com'
33    return gerrit_base_url + endpoint
34
35
36@app.route('/', methods=['POST'])
37def handle_build_message():
38    result = json.loads(request.data)
39
40    name = result['name']
41    number = result['build']['number']
42    status = result['build']['status']
43    go_url = 'http://go/bionicbb/' + result['build']['url']
44    full_url = result['build']['full_url']
45    params = result['build']['parameters']
46    change_id = params['CHANGE_ID']
47    ref = params['REF']
48    patch_set = ref.split('/')[-1]
49
50    logging.debug('%s #%s %s: %s', name, number, status, full_url)
51
52    # bionic-lint is always broken, so we don't want to reject changes for
53    # those failures until we clean things up.
54    if name == 'bionic-presubmit':
55        message_lines = ['{} #{} checkbuild {}: {}'.format(
56            name, number, status, go_url)]
57        if status == 'FAILURE':
58            message_lines += ['If you believe this Verified-1 was in error, '
59                              '+1 the change and bionicbb will remove the -1 '
60                              'shortly.']
61
62        request_data = {
63            'message': '\n'.join(message_lines)
64        }
65
66        label = 'Verified'
67        if status == 'FAILURE':
68            request_data['labels'] = {label: -1}
69        elif status == 'SUCCESS':
70            request_data['labels'] = {label: +1}
71
72        url = gerrit_url('/a/changes/{}/revisions/{}/review'.format(change_id,
73                                                                    patch_set))
74
75        headers = {'Content-Type': 'application/json;charset=UTF-8'}
76        logging.debug('POST %s: %s', url, request_data)
77        requests.post(url, headers=headers, json=request_data)
78    elif name == 'clean-bionic-presubmit':
79        request_data = {'message': 'out/ directory removed'}
80        url = gerrit_url('/a/changes/{}/revisions/{}/review'.format(change_id,
81                                                                    patch_set))
82        headers = {'Content-Type': 'application/json;charset=UTF-8'}
83        logging.debug('POST %s: %s', url, request_data)
84        requests.post(url, headers=headers, json=request_data)
85    elif name == 'bionic-lint':
86        logging.warning('Result for bionic-lint ignored')
87    else:
88        logging.error('Unknown project: %s', name)
89    return ''
90
91
92@app.route('/drop-rejection', methods=['POST'])
93def drop_rejection():
94    revision_info = json.loads(request.data)
95
96    change_id = revision_info['changeid']
97    patch_set = revision_info['patchset']
98
99    bb_email = 'bionicbb@android.com'
100    labels = gerrit.get_labels(change_id, patch_set)
101    if bb_email in labels['Verified']:
102        bb_review = labels['Verified'][bb_email]
103    else:
104        bb_review = 0
105
106    if bb_review >= 0:
107        logging.info('No rejection to drop: %s %s', change_id, patch_set)
108        return ''
109
110    logging.info('Dropping rejection: %s %s', change_id, patch_set)
111
112    request_data = {'labels': {'Verified': 0}}
113    url = gerrit_url('/a/changes/{}/revisions/{}/review'.format(change_id,
114                                                                patch_set))
115    headers = {'Content-Type': 'application/json;charset=UTF-8'}
116    logging.debug('POST %s: %s', url, request_data)
117    requests.post(url, headers=headers, json=request_data)
118    return ''
119
120
121if __name__ == "__main__":
122    logging.basicConfig(level=logging.INFO)
123    logger = logging.getLogger()
124    fh = logging.FileHandler('bionicbb.log')
125    fh.setLevel(logging.INFO)
126    logger.addHandler(fh)
127
128    # Prevent the job from being rescheduled by the reloader.
129    if os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
130        scheduler = BackgroundScheduler()
131        scheduler.start()
132        scheduler.add_job(tasks.get_and_process_jobs, 'interval', minutes=5)
133
134    app.run(host='0.0.0.0', debug=True)
135