summaryrefslogtreecommitdiffstats
path: root/tools/bionicbb/bionicbb.py
blob: 20d6460ed25c7663d087cf66755455fe470ac977 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python2
#
# Copyright (C) 2015 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import json
import logging
import os

from apscheduler.schedulers.background import BackgroundScheduler
from flask import Flask, request
import requests

import gerrit
import tasks

app = Flask(__name__)


def gerrit_url(endpoint):
    gerrit_base_url = 'https://android-review.googlesource.com'
    return gerrit_base_url + endpoint


@app.route('/', methods=['POST'])
def handle_build_message():
    result = json.loads(request.data)

    name = result['name']
    number = result['build']['number']
    status = result['build']['status']
    go_url = 'http://go/bionicbb/' + result['build']['url']
    full_url = result['build']['full_url']
    params = result['build']['parameters']
    change_id = params['CHANGE_ID']
    ref = params['REF']
    patch_set = ref.split('/')[-1]

    logging.debug('%s #%s %s: %s', name, number, status, full_url)

    # bionic-lint is always broken, so we don't want to reject changes for
    # those failures until we clean things up.
    if name == 'bionic-presubmit':
        message_lines = ['{} #{} checkbuild {}: {}'.format(
            name, number, status, go_url)]
        if status == 'FAILURE':
            message_lines += ['If you believe this Verified-1 was in error, '
                              '+1 the change and bionicbb will remove the -1 '
                              'shortly.']

        request_data = {
            'message': '\n'.join(message_lines)
        }

        label = 'Verified'
        if status == 'FAILURE':
            request_data['labels'] = {label: -1}
        elif status == 'SUCCESS':
            request_data['labels'] = {label: +1}

        url = gerrit_url('/a/changes/{}/revisions/{}/review'.format(change_id,
                                                                    patch_set))

        headers = {'Content-Type': 'application/json;charset=UTF-8'}
        logging.debug('POST %s: %s', url, request_data)
        requests.post(url, headers=headers, json=request_data)
    elif name == 'clean-bionic-presubmit':
        request_data = {'message': 'out/ directory removed'}
        url = gerrit_url('/a/changes/{}/revisions/{}/review'.format(change_id,
                                                                    patch_set))
        headers = {'Content-Type': 'application/json;charset=UTF-8'}
        logging.debug('POST %s: %s', url, request_data)
        requests.post(url, headers=headers, json=request_data)
    elif name == 'bionic-lint':
        logging.warning('Result for bionic-lint ignored')
    else:
        logging.error('Unknown project: %s', name)
    return ''


@app.route('/drop-rejection', methods=['POST'])
def drop_rejection():
    revision_info = json.loads(request.data)

    change_id = revision_info['changeid']
    patch_set = revision_info['patchset']

    bb_email = 'bionicbb@android.com'
    labels = gerrit.get_labels(change_id, patch_set)
    if bb_email in labels['Verified']:
        bb_review = labels['Verified'][bb_email]
    else:
        bb_review = 0

    if bb_review >= 0:
        logging.info('No rejection to drop: %s %s', change_id, patch_set)
        return ''

    logging.info('Dropping rejection: %s %s', change_id, patch_set)

    request_data = {'labels': {'Verified': 0}}
    url = gerrit_url('/a/changes/{}/revisions/{}/review'.format(change_id,
                                                                patch_set))
    headers = {'Content-Type': 'application/json;charset=UTF-8'}
    logging.debug('POST %s: %s', url, request_data)
    requests.post(url, headers=headers, json=request_data)
    return ''


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)

    # Prevent the job from being rescheduled by the reloader.
    if os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
        scheduler = BackgroundScheduler()
        scheduler.start()
        scheduler.add_job(tasks.get_and_process_jobs, 'interval', minutes=5)

    app.run(host='0.0.0.0', debug=True)