blob: a786b27edacbaaaaf99203b58908a719cc48ec53 [file] [log] [blame]
Dan Albert7c78d242015-01-09 14:12:52 -08001#!/usr/bin/env python2
Dan Albertc02df472015-01-09 17:22:00 -08002#
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#
Dan Albert7c78d242015-01-09 14:12:52 -080017import json
Dan Alberta4061cd2015-04-16 14:20:13 -070018import logging
Dan Albertd3fe4f12015-04-17 13:01:29 -070019import os
20
21from apscheduler.schedulers.background import BackgroundScheduler
22from flask import Flask, request
Dan Albert7c78d242015-01-09 14:12:52 -080023import requests
Dan Albert7c78d242015-01-09 14:12:52 -080024
Dan Albert8a15a4e2015-01-09 16:52:07 -080025import gerrit
Dan Albertd3fe4f12015-04-17 13:01:29 -070026import tasks
Dan Albert7c78d242015-01-09 14:12:52 -080027
Dan Albert7c78d242015-01-09 14:12:52 -080028app = Flask(__name__)
29
30
31def gerrit_url(endpoint):
Dan Albertc02df472015-01-09 17:22:00 -080032 gerrit_base_url = 'https://android-review.googlesource.com'
33 return gerrit_base_url + endpoint
Dan Albert7c78d242015-01-09 14:12:52 -080034
35
36@app.route('/', methods=['POST'])
37def handle_build_message():
Dan Albertc02df472015-01-09 17:22:00 -080038 result = json.loads(request.data)
Dan Albert7c78d242015-01-09 14:12:52 -080039
Dan Albertc02df472015-01-09 17:22:00 -080040 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]
Dan Albert7c78d242015-01-09 14:12:52 -080049
Dan Alberta4061cd2015-04-16 14:20:13 -070050 logging.debug('%s #%s %s: %s', name, number, status, full_url)
Dan Albert7c78d242015-01-09 14:12:52 -080051
Dan Albertc02df472015-01-09 17:22:00 -080052 # 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.']
Dan Albert7c78d242015-01-09 14:12:52 -080061
Dan Albertc02df472015-01-09 17:22:00 -080062 request_data = {
63 'message': '\n'.join(message_lines)
64 }
Dan Albert7c78d242015-01-09 14:12:52 -080065
Dan Albertc02df472015-01-09 17:22:00 -080066 label = 'Verified'
67 if status == 'FAILURE':
68 request_data['labels'] = {label: -1}
69 elif status == 'SUCCESS':
70 request_data['labels'] = {label: +1}
Dan Albert7c78d242015-01-09 14:12:52 -080071
Dan Albertc02df472015-01-09 17:22:00 -080072 url = gerrit_url('/a/changes/{}/revisions/{}/review'.format(change_id,
73 patch_set))
Dan Albert7c78d242015-01-09 14:12:52 -080074
Dan Albertc02df472015-01-09 17:22:00 -080075 headers = {'Content-Type': 'application/json;charset=UTF-8'}
Dan Alberta4061cd2015-04-16 14:20:13 -070076 logging.debug('POST %s: %s', url, request_data)
77 requests.post(url, headers=headers, json=request_data)
Dan Albertc02df472015-01-09 17:22:00 -080078 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'}
Dan Alberta4061cd2015-04-16 14:20:13 -070083 logging.debug('POST %s: %s', url, request_data)
84 requests.post(url, headers=headers, json=request_data)
Dan Albertc02df472015-01-09 17:22:00 -080085 elif name == 'bionic-lint':
Dan Alberta4061cd2015-04-16 14:20:13 -070086 logging.warning('Result for bionic-lint ignored')
Dan Albertc02df472015-01-09 17:22:00 -080087 else:
Dan Alberta4061cd2015-04-16 14:20:13 -070088 logging.error('Unknown project: %s', name)
Dan Albertc02df472015-01-09 17:22:00 -080089 return ''
Dan Albert7c78d242015-01-09 14:12:52 -080090
91
92@app.route('/drop-rejection', methods=['POST'])
93def drop_rejection():
Dan Albertc02df472015-01-09 17:22:00 -080094 revision_info = json.loads(request.data)
Dan Albert7c78d242015-01-09 14:12:52 -080095
Dan Albertc02df472015-01-09 17:22:00 -080096 change_id = revision_info['changeid']
97 patch_set = revision_info['patchset']
Dan Albert7c78d242015-01-09 14:12:52 -080098
Dan Albertc02df472015-01-09 17:22:00 -080099 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
Dan Albert7c78d242015-01-09 14:12:52 -0800105
Dan Albertc02df472015-01-09 17:22:00 -0800106 if bb_review >= 0:
Dan Alberta4061cd2015-04-16 14:20:13 -0700107 logging.info('No rejection to drop: %s %s', change_id, patch_set)
Dan Albertc02df472015-01-09 17:22:00 -0800108 return ''
109
Dan Alberta4061cd2015-04-16 14:20:13 -0700110 logging.info('Dropping rejection: %s %s', change_id, patch_set)
Dan Albertc02df472015-01-09 17:22:00 -0800111
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'}
Dan Alberta4061cd2015-04-16 14:20:13 -0700116 logging.debug('POST %s: %s', url, request_data)
117 requests.post(url, headers=headers, json=request_data)
Dan Albert7c78d242015-01-09 14:12:52 -0800118 return ''
119
Dan Albert7c78d242015-01-09 14:12:52 -0800120
121if __name__ == "__main__":
Dan Albertd3fe4f12015-04-17 13:01:29 -0700122 logging.basicConfig(level=logging.INFO)
Dan Albert21988a32015-04-17 17:51:39 -0700123 logger = logging.getLogger()
124 fh = logging.FileHandler('bionicbb.log')
125 fh.setLevel(logging.INFO)
126 logger.addHandler(fh)
Dan Albertd3fe4f12015-04-17 13:01:29 -0700127
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
Dan Albertc02df472015-01-09 17:22:00 -0800134 app.run(host='0.0.0.0', debug=True)