blob: 51df4fbce47b8a5d84aa4a07891d0d73a8a60cc0 [file] [log] [blame]
Dan Albert7c78d242015-01-09 14:12:52 -08001# pylint: disable=bad-indentation
2# vim: set sw=2 ts=2:
3import json
4import requests
5
6
7class GerritError(RuntimeError):
8 def __init__(self, code, url):
9 self.code = code
10 self.url = url
11 super(GerritError, self).__init__('Error {}: {}'.format(code, url))
12
13
14def call(endpoint, method='GET'):
15 if method != 'GET':
16 raise NotImplementedError('Currently only HTTP GET is supported.')
17 gerrit_url = 'https://android-review.googlesource.com'
18 url = gerrit_url + endpoint
19 response = requests.get(url)
20 if response.status_code != 200:
21 raise GerritError(response.status_code, url)
22 return response.text[5:]
23
24
25def ref_for_change(change_id):
26 endpoint = '/changes/{}/detail?o=CURRENT_REVISION'.format(change_id)
27 change = json.loads(call(endpoint))
28 commit = change['current_revision']
29 return change['revisions'][commit]['fetch']['http']['ref']
30
31
32def get_labels(change_id, patch_set):
33 """Returns labels attached to a revision.
34
35 Returned data is in the following format:
36 {
37 'Code-Review': {
38 <email>: <value>,
39 ...
40 },
41 'Verified': {
42 <email>: <value>,
43 ...
44 }
45 }
46 """
47 details = call('/changes/{}/revisions/{}/review'.format(
48 change_id, patch_set))
49 labels = {'Code-Review': {}, 'Verified': {}}
50 for review in details['labels']['Code-Review']['all']:
51 if 'value' in review and 'email' in review:
52 labels['Code-Review'][review['email']] = int(review['value'])
53 for review in details['labels']['Verified']['all']:
54 if 'value' in review and 'email' in review:
55 labels['Verified'][review['email']] = int(review['value'])
56 return labels