| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 from recipe_engine import recipe_api | 5 from recipe_engine import recipe_api |
| 6 | 6 |
| 7 import re | 7 import re |
| 8 | 8 |
| 9 | 9 |
| 10 class CrrevApi(recipe_api.RecipeApi): | 10 class CrrevApi(recipe_api.RecipeApi): |
| 11 """Recipe module for making requests to crrev.com.""" | 11 """Recipe module for making requests to crrev.com.""" |
| 12 | 12 |
| 13 def __call__(self, step_name, request_path, request_params=None, attempts=3): | 13 def __call__(self, step_name, request_path, request_params=None, attempts=3, |
| 14 **kwargs): |
| 14 step_result = self.m.python( | 15 step_result = self.m.python( |
| 15 step_name, | 16 step_name, |
| 16 self.resource('crrev_client.py'), | 17 self.resource('crrev_client.py'), |
| 17 [ | 18 [ |
| 18 request_path, | 19 request_path, |
| 19 '--params-file', self.m.json.input(request_params or {}), | 20 '--params-file', self.m.json.input(request_params or {}), |
| 20 '--attempts', str(attempts), | 21 '--attempts', str(attempts), |
| 21 ], | 22 ], |
| 22 stdout=self.m.json.output()) | 23 stdout=self.m.json.output(), **kwargs) |
| 23 return step_result.stdout | 24 return step_result.stdout |
| 24 | 25 |
| 25 def to_commit_hash( | 26 def to_commit_hash( |
| 26 self, commit_position, project='chromium', repo='chromium/src', | 27 self, commit_position, project='chromium', repo='chromium/src', |
| 27 attempts=3, step_name=None): | 28 attempts=3, step_name=None, **kwargs): |
| 28 """Fetches the corresponding commit hash for a commit position.""" | 29 """Fetches the corresponding commit hash for a commit position.""" |
| 29 branch, number = self.m.commit_position.parse(commit_position) | 30 branch, number = self.m.commit_position.parse(commit_position) |
| 30 params = { | 31 params = { |
| 31 'numbering_type': 'COMMIT_POSITION', | 32 'numbering_type': 'COMMIT_POSITION', |
| 32 'numbering_identifier': branch, | 33 'numbering_identifier': branch, |
| 33 'number': number, | 34 'number': number, |
| 34 'project': project, | 35 'project': project, |
| 35 'repo': repo, | 36 'repo': repo, |
| 36 } | 37 } |
| 37 step_name = step_name or 'crrev get commit hash for ' + commit_position | 38 step_name = step_name or 'crrev get commit hash for ' + commit_position |
| 38 try: | 39 try: |
| 39 result = self(step_name, 'get_numbering', params, attempts) | 40 result = self(step_name, 'get_numbering', params, attempts, **kwargs) |
| 40 return result['git_sha'] | 41 return result['git_sha'] |
| 41 except (self.m.step.StepFailure, KeyError): | 42 except (self.m.step.StepFailure, KeyError): |
| 42 raise self.m.step.StepFailure('Could not resolve ' + commit_position) | 43 raise self.m.step.StepFailure('Could not resolve ' + commit_position) |
| 43 | 44 |
| 44 def to_commit_position(self, commit_hash, attempts=3, step_name=None): | 45 def to_commit_position(self, commit_hash, attempts=3, step_name=None): |
| 45 """Fetches a commit position string given a commit hash.""" | 46 """Fetches a commit position string given a commit hash.""" |
| 46 if not re.match(r'^[0-9a-zA-Z]{40}$', commit_hash): | 47 if not re.match(r'^[0-9a-zA-Z]{40}$', commit_hash): |
| 47 raise ValueError('Not a full 40-digit SHA1 hash (%s)' % commit_hash) | 48 raise ValueError('Not a full 40-digit SHA1 hash (%s)' % commit_hash) |
| 48 step_name = step_name or 'crrev get commit position for ' + commit_hash | 49 step_name = step_name or 'crrev get commit position for ' + commit_hash |
| 49 try: | 50 try: |
| 50 result = self(step_name, 'commit_path/' + commit_hash, attempts=attempts) | 51 result = self(step_name, 'commit_path/' + commit_hash, attempts=attempts) |
| 51 numberings = result['numberings'] | 52 numberings = result['numberings'] |
| 52 except (self.m.step.StepFailure, KeyError): | 53 except (self.m.step.StepFailure, KeyError): |
| 53 raise self.m.step.StepFailure('Could not resolve ' + commit_hash) | 54 raise self.m.step.StepFailure('Could not resolve ' + commit_hash) |
| 54 for numbering in numberings: | 55 for numbering in numberings: |
| 55 if numbering['numbering_type'] == 'COMMIT_POSITION': | 56 if numbering['numbering_type'] == 'COMMIT_POSITION': |
| 56 branch = numbering['numbering_identifier'] | 57 branch = numbering['numbering_identifier'] |
| 57 number = numbering['number'] | 58 number = numbering['number'] |
| 58 return self.m.commit_position.construct(branch, number) | 59 return self.m.commit_position.construct(branch, number) |
| 59 raise self.m.step.StepFailure('No commit position for ' + commit_hash) | 60 raise self.m.step.StepFailure('No commit position for ' + commit_hash) |
| 60 | 61 |
| OLD | NEW |