| Dan Albert | c02df47 | 2015-01-09 17:22:00 -0800 | [diff] [blame] | 1 | # | 
|  | 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 Albert | 7c78d24 | 2015-01-09 14:12:52 -0800 | [diff] [blame] | 16 | import json | 
|  | 17 | import requests | 
|  | 18 |  | 
|  | 19 |  | 
|  | 20 | class GerritError(RuntimeError): | 
| Dan Albert | c02df47 | 2015-01-09 17:22:00 -0800 | [diff] [blame] | 21 | def __init__(self, code, url): | 
|  | 22 | self.code = code | 
|  | 23 | self.url = url | 
|  | 24 | super(GerritError, self).__init__('Error {}: {}'.format(code, url)) | 
| Dan Albert | 7c78d24 | 2015-01-09 14:12:52 -0800 | [diff] [blame] | 25 |  | 
|  | 26 |  | 
| Dan Albert | b406033 | 2015-01-12 16:23:53 -0800 | [diff] [blame] | 27 | def get_commit(change_id, revision): | 
|  | 28 | return json.loads( | 
|  | 29 | call('/changes/{}/revisions/{}/commit'.format(change_id, revision))) | 
|  | 30 |  | 
|  | 31 |  | 
| Dan Albert | dadac10 | 2015-04-06 12:43:55 -0700 | [diff] [blame] | 32 | def get_files_for_revision(change_id, revision): | 
|  | 33 | return json.loads( | 
|  | 34 | call('/changes/{}/revisions/{}/files'.format( | 
|  | 35 | change_id, revision))).keys() | 
|  | 36 |  | 
|  | 37 |  | 
| Dan Albert | 7c78d24 | 2015-01-09 14:12:52 -0800 | [diff] [blame] | 38 | def call(endpoint, method='GET'): | 
| Dan Albert | c02df47 | 2015-01-09 17:22:00 -0800 | [diff] [blame] | 39 | if method != 'GET': | 
|  | 40 | raise NotImplementedError('Currently only HTTP GET is supported.') | 
|  | 41 | gerrit_url = 'https://android-review.googlesource.com' | 
|  | 42 | url = gerrit_url + endpoint | 
|  | 43 | response = requests.get(url) | 
|  | 44 | if response.status_code != 200: | 
|  | 45 | raise GerritError(response.status_code, url) | 
|  | 46 | return response.text[5:] | 
| Dan Albert | 7c78d24 | 2015-01-09 14:12:52 -0800 | [diff] [blame] | 47 |  | 
|  | 48 |  | 
|  | 49 | def ref_for_change(change_id): | 
| Dan Albert | c02df47 | 2015-01-09 17:22:00 -0800 | [diff] [blame] | 50 | endpoint = '/changes/{}/detail?o=CURRENT_REVISION'.format(change_id) | 
|  | 51 | change = json.loads(call(endpoint)) | 
|  | 52 | commit = change['current_revision'] | 
|  | 53 | return change['revisions'][commit]['fetch']['http']['ref'] | 
| Dan Albert | 7c78d24 | 2015-01-09 14:12:52 -0800 | [diff] [blame] | 54 |  | 
|  | 55 |  | 
|  | 56 | def get_labels(change_id, patch_set): | 
| Dan Albert | c02df47 | 2015-01-09 17:22:00 -0800 | [diff] [blame] | 57 | """Returns labels attached to a revision. | 
| Dan Albert | 7c78d24 | 2015-01-09 14:12:52 -0800 | [diff] [blame] | 58 |  | 
| Dan Albert | c02df47 | 2015-01-09 17:22:00 -0800 | [diff] [blame] | 59 | Returned data is in the following format: | 
|  | 60 | { | 
|  | 61 | 'Code-Review': { | 
|  | 62 | <email>: <value>, | 
|  | 63 | ... | 
|  | 64 | }, | 
|  | 65 | 'Verified': { | 
|  | 66 | <email>: <value>, | 
|  | 67 | ... | 
|  | 68 | } | 
|  | 69 | } | 
|  | 70 | """ | 
| Dan Albert | 7d57623 | 2015-03-24 11:43:55 -0700 | [diff] [blame] | 71 | details = json.loads(call('/changes/{}/revisions/{}/review'.format( | 
|  | 72 | change_id, patch_set))) | 
| Dan Albert | c02df47 | 2015-01-09 17:22:00 -0800 | [diff] [blame] | 73 | labels = {'Code-Review': {}, 'Verified': {}} | 
|  | 74 | for review in details['labels']['Code-Review']['all']: | 
|  | 75 | if 'value' in review and 'email' in review: | 
|  | 76 | labels['Code-Review'][review['email']] = int(review['value']) | 
|  | 77 | for review in details['labels']['Verified']['all']: | 
|  | 78 | if 'value' in review and 'email' in review: | 
|  | 79 | labels['Verified'][review['email']] = int(review['value']) | 
|  | 80 | return labels |