blob: a3d58876cf02737807f1a8b667e2e5158d4b8a6e [file] [log] [blame]
Dan Albertc02df472015-01-09 17:22:00 -08001#
2# Copyright (C) 2015 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the 'License');
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an 'AS IS' BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
Dan Albert7c78d242015-01-09 14:12:52 -080016import json
17import requests
18
19
20class GerritError(RuntimeError):
Dan Albertc02df472015-01-09 17:22:00 -080021 def __init__(self, code, url):
22 self.code = code
23 self.url = url
24 super(GerritError, self).__init__('Error {}: {}'.format(code, url))
Dan Albert7c78d242015-01-09 14:12:52 -080025
26
27def call(endpoint, method='GET'):
Dan Albertc02df472015-01-09 17:22:00 -080028 if method != 'GET':
29 raise NotImplementedError('Currently only HTTP GET is supported.')
30 gerrit_url = 'https://android-review.googlesource.com'
31 url = gerrit_url + endpoint
32 response = requests.get(url)
33 if response.status_code != 200:
34 raise GerritError(response.status_code, url)
35 return response.text[5:]
Dan Albert7c78d242015-01-09 14:12:52 -080036
37
38def ref_for_change(change_id):
Dan Albertc02df472015-01-09 17:22:00 -080039 endpoint = '/changes/{}/detail?o=CURRENT_REVISION'.format(change_id)
40 change = json.loads(call(endpoint))
41 commit = change['current_revision']
42 return change['revisions'][commit]['fetch']['http']['ref']
Dan Albert7c78d242015-01-09 14:12:52 -080043
44
45def get_labels(change_id, patch_set):
Dan Albertc02df472015-01-09 17:22:00 -080046 """Returns labels attached to a revision.
Dan Albert7c78d242015-01-09 14:12:52 -080047
Dan Albertc02df472015-01-09 17:22:00 -080048 Returned data is in the following format:
49 {
50 'Code-Review': {
51 <email>: <value>,
52 ...
53 },
54 'Verified': {
55 <email>: <value>,
56 ...
57 }
58 }
59 """
60 details = call('/changes/{}/revisions/{}/review'.format(
61 change_id, patch_set))
62 labels = {'Code-Review': {}, 'Verified': {}}
63 for review in details['labels']['Code-Review']['all']:
64 if 'value' in review and 'email' in review:
65 labels['Code-Review'][review['email']] = int(review['value'])
66 for review in details['labels']['Verified']['all']:
67 if 'value' in review and 'email' in review:
68 labels['Verified'][review['email']] = int(review['value'])
69 return labels