blob: 69b23afb447c6bdb1c2f66d8ab06d5403d681363 [file] [log] [blame]
# Copyright 2017 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import base64
from recipe_engine import recipe_api
class GitilesApi(recipe_api.RecipeApi):
"""Module for polling a Git repository using the Gitiles web interface."""
def __init__(self, *args, **kwargs):
super(GitilesApi, self).__init__(*args, **kwargs)
self._gitiles_path = None
def ensure(self, version=None):
with self.m.step.nest('ensure_gitiles'):
with self.m.context(infra_steps=True):
pkgs = self.m.cipd.EnsureFile()
pkgs.add_package('infra/tools/luci/gitiles/${platform}', version or
'latest')
gitiles_dir = self.m.path['start_dir'].join('cipd', 'gitiles')
self.m.cipd.ensure(gitiles_dir, pkgs)
self._gitiles_path = gitiles_dir.join('gitiles')
return self._gitiles_path
def refs(self, url, refspath='refs', step_name='refs', test_data=None):
"""Resolves each ref in a repository to git revision
Args:
url (str): URL of the remote repository.
refspath (str): limits which refs to resolve.
"""
assert self._gitiles_path
assert refspath.startswith('refs')
cmd = [
self._gitiles_path,
'refs',
'-json-output',
self.m.json.output(),
]
cmd.extend([url, refspath])
return self.m.step(step_name, cmd, step_test_data=test_data).json.output
def log(self, url, treeish, limit=0, step_name=None, test_data=None):
"""Returns the most recent commits for treeish object.
Args:
url (str): base URL of the remote repository.
treeish (str): tree object identifier.
limit (int): number of commits to limit the fetching to.
step_name (str): custom name for this step (optional).
"""
assert self._gitiles_path
cmd = [
self._gitiles_path,
'log',
'-json-output',
self.m.json.output(),
]
if limit:
cmd.extend(['-limit', limit])
cmd.extend([url, treeish])
return self.m.step(
step_name or 'gitiles log: %s' % treeish, cmd,
step_test_data=test_data).json.output
def fetch(self,
host,
project,
path,
ref='master',
step_name=None,
timeout_secs=None):
"""Fetches raw file content from a Gitiles repository.
Args:
host (str): The Gerrit hostname.
project (str): The project name.
path (str): The path to the file relative to the project root.
ref (str): A ref to fetch from.
step_name (str): (optional) A custom step name.
timeout_secs (int): (optional) The number of seconds to wait before
failing.
Returns:
Raw file content.
"""
url = "https://{host}/{project}/+/{ref}/{path}?format=TEXT".format(
host=host, project=project, ref=ref, path=path)
step_name = step_name or 'fetch'
step_result = self.m.url.get_text(
url=url,
step_name=step_name,
timeout=timeout_secs,
)
return base64.b64decode(step_result.output)