blob: 0606136fdbb0b5f776baab7081f8b86d029c4934 [file] [log] [blame]
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from recipe_engine import recipe_api
import re
class CrrevApi(recipe_api.RecipeApi):
"""Recipe module for making requests to"""
def __call__(self, step_name, request_path, request_params=None, attempts=3,
step_result =
'--params-file', self.m.json.input(request_params or {}),
'--attempts', str(attempts),
show_path=False, # Can't dump path, since we use STDOUT.
return step_result.stdout
def to_commit_hash(
self, commit_position, project='chromium', repo='chromium/src',
attempts=3, step_name=None, **kwargs):
"""Fetches the corresponding commit hash for a commit position."""
branch, number = self.m.commit_position.parse(commit_position)
params = {
'numbering_type': 'COMMIT_POSITION',
'numbering_identifier': branch,
'number': number,
'project': project,
'repo': repo,
step_name = step_name or 'crrev get commit hash for ' + commit_position
result = self(step_name, 'get_numbering', params, attempts, **kwargs)
return result['git_sha']
except (self.m.step.StepFailure, KeyError):
raise self.m.step.StepFailure('Could not resolve ' + commit_position)
def to_commit_position(self, commit_hash, attempts=3, step_name=None):
"""Fetches a commit position string given a commit hash."""
if not re.match(r'^[0-9a-zA-Z]{40}$', commit_hash):
raise ValueError('Not a full 40-digit SHA1 hash (%s)' % commit_hash)
step_name = step_name or 'crrev get commit position for ' + commit_hash
result = self(step_name, 'commit_path/' + commit_hash, attempts=attempts)
numberings = result['numberings']
except (self.m.step.StepFailure, KeyError):
raise self.m.step.StepFailure('Could not resolve ' + commit_hash)
for numbering in numberings:
if numbering['numbering_type'] == 'COMMIT_POSITION':
branch = numbering['numbering_identifier']
number = numbering['number']
return self.m.commit_position.construct(branch, number)
raise self.m.step.StepFailure('No commit position for ' + commit_hash)