blob: 40719b48af84a7ae46a5e7eb65e087e73f77b495 [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
Dan Albertb4060332015-01-12 16:23:53 -080027def get_commit(change_id, revision):
28 return json.loads(
29 call('/changes/{}/revisions/{}/commit'.format(change_id, revision)))
30
31
Dan Albert7c78d242015-01-09 14:12:52 -080032def call(endpoint, method='GET'):
Dan Albertc02df472015-01-09 17:22:00 -080033 if method != 'GET':
34 raise NotImplementedError('Currently only HTTP GET is supported.')
35 gerrit_url = 'https://android-review.googlesource.com'
36 url = gerrit_url + endpoint
37 response = requests.get(url)
38 if response.status_code != 200:
39 raise GerritError(response.status_code, url)
40 return response.text[5:]
Dan Albert7c78d242015-01-09 14:12:52 -080041
42
43def ref_for_change(change_id):
Dan Albertc02df472015-01-09 17:22:00 -080044 endpoint = '/changes/{}/detail?o=CURRENT_REVISION'.format(change_id)
45 change = json.loads(call(endpoint))
46 commit = change['current_revision']
47 return change['revisions'][commit]['fetch']['http']['ref']
Dan Albert7c78d242015-01-09 14:12:52 -080048
49
50def get_labels(change_id, patch_set):
Dan Albertc02df472015-01-09 17:22:00 -080051 """Returns labels attached to a revision.
Dan Albert7c78d242015-01-09 14:12:52 -080052
Dan Albertc02df472015-01-09 17:22:00 -080053 Returned data is in the following format:
54 {
55 'Code-Review': {
56 <email>: <value>,
57 ...
58 },
59 'Verified': {
60 <email>: <value>,
61 ...
62 }
63 }
64 """
Dan Albert7d576232015-03-24 11:43:55 -070065 details = json.loads(call('/changes/{}/revisions/{}/review'.format(
66 change_id, patch_set)))
Dan Albertc02df472015-01-09 17:22:00 -080067 labels = {'Code-Review': {}, 'Verified': {}}
68 for review in details['labels']['Code-Review']['all']:
69 if 'value' in review and 'email' in review:
70 labels['Code-Review'][review['email']] = int(review['value'])
71 for review in details['labels']['Verified']['all']:
72 if 'value' in review and 'email' in review:
73 labels['Verified'][review['email']] = int(review['value'])
74 return labels