| # 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 __call__(self, step_name, cmd, test_data=None): |
| self._ensure() |
| full_cmd = [self._gitiles_path] |
| full_cmd.extend(cmd) |
| return self.m.step(step_name, full_cmd, step_test_data=test_data) |
| |
| def _ensure(self): |
| if self._gitiles_path: |
| return |
| 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}', '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') |
| |
| 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 refspath.startswith('refs') |
| cmd = [ |
| 'refs', |
| '-json-output', |
| self.m.json.output(), |
| url, |
| refspath, |
| ] |
| return self(step_name, cmd, 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). |
| """ |
| cmd = [ |
| 'log', |
| '-json-output', |
| self.m.json.output(), |
| ] |
| if limit: |
| cmd.extend(['-limit', limit]) |
| cmd.extend([url, treeish]) |
| step_name = step_name or 'gitiles log: %s' % treeish |
| return self(step_name, cmd, 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) |