Commit 3cb0767a authored by Andrii Shyshkalov's avatar Andrii Shyshkalov Committed by Commit Bot

recipes: cache destination branch of a Gerrit change.

It doesn't change after change is created.

R=jchinlee@chromium.org

Recipe-Nontrivial-Roll: build
Recipe-Nontrivial-Roll: infra
Recipe-Nontrivial-Roll: build_limited_scripts_slave
Change-Id: I4c4758085f6cfa7c2ef5267c6c71c9b324263f95
Reviewed-on: https://chromium-review.googlesource.com/1029227
Commit-Queue: Andrii Shyshkalov <tandrii@chromium.org>
Reviewed-by: 's avatarMichael Achenbach <machenbach@chromium.org>
parent 04566221
......@@ -309,18 +309,18 @@ revision map. This doesn't overwrite the revision if it was already set.
Module for interact with gerrit endpoints
&mdash; **def [\_\_call\_\_](/recipes/recipe_modules/gerrit/api.py#10)(self, name, cmd, infra_step=True, \*\*kwargs):**
&mdash; **def [\_\_call\_\_](/recipes/recipe_modules/gerrit/api.py#14)(self, name, cmd, infra_step=True, \*\*kwargs):**
Wrapper for easy calling of gerrit_utils steps.
&mdash; **def [create\_gerrit\_branch](/recipes/recipe_modules/gerrit/api.py#27)(self, host, project, branch, commit, \*\*kwargs):**
&mdash; **def [create\_gerrit\_branch](/recipes/recipe_modules/gerrit/api.py#31)(self, host, project, branch, commit, \*\*kwargs):**
Create a new branch from given project and commit
Returns:
the ref of the branch created
&mdash; **def [get\_change\_description](/recipes/recipe_modules/gerrit/api.py#93)(self, host, change, patchset):**
&mdash; **def [get\_change\_description](/recipes/recipe_modules/gerrit/api.py#106)(self, host, change, patchset):**
Get the description for a given CL and patchset.
......@@ -332,10 +332,12 @@ Args:
Returns:
The description corresponding to given CL and patchset.
&mdash; **def [get\_change\_destination\_branch](/recipes/recipe_modules/gerrit/api.py#68)(self, host, change, \*\*kwargs):**
&mdash; **def [get\_change\_destination\_branch](/recipes/recipe_modules/gerrit/api.py#72)(self, host, change, name=None, step_test_data=None):**
Get the upstream branch for a given CL.
Result is cached.
Args:
host: Gerrit host to query.
change: The change number.
......@@ -343,7 +345,7 @@ Args:
Returns:
the name of the branch
&mdash; **def [get\_changes](/recipes/recipe_modules/gerrit/api.py#122)(self, host, query_params, start=None, limit=None, o_params=None, step_test_data=None, \*\*kwargs):**
&mdash; **def [get\_changes](/recipes/recipe_modules/gerrit/api.py#135)(self, host, query_params, start=None, limit=None, o_params=None, step_test_data=None, \*\*kwargs):**
Query changes for the given host.
......@@ -361,7 +363,7 @@ Returns:
A list of change dicts as documented here:
https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-changes
&mdash; **def [get\_gerrit\_branch](/recipes/recipe_modules/gerrit/api.py#49)(self, host, project, branch, \*\*kwargs):**
&mdash; **def [get\_gerrit\_branch](/recipes/recipe_modules/gerrit/api.py#53)(self, host, project, branch, \*\*kwargs):**
Get a branch from given project and commit
......
......@@ -7,6 +7,10 @@ from recipe_engine import recipe_api
class GerritApi(recipe_api.RecipeApi):
"""Module for interact with gerrit endpoints"""
def __init__(self, *args, **kwargs):
super(GerritApi, self).__init__(*args, **kwargs)
self._changes_target_branch_cache = {}
def __call__(self, name, cmd, infra_step=True, **kwargs):
"""Wrapper for easy calling of gerrit_utils steps."""
assert isinstance(cmd, (list, tuple))
......@@ -65,10 +69,13 @@ class GerritApi(recipe_api.RecipeApi):
revision = step_result.json.output.get('revision')
return revision
def get_change_destination_branch(self, host, change, **kwargs):
def get_change_destination_branch(
self, host, change, name=None, step_test_data=None):
"""
Get the upstream branch for a given CL.
Result is cached.
Args:
host: Gerrit host to query.
change: The change number.
......@@ -76,19 +83,25 @@ class GerritApi(recipe_api.RecipeApi):
Returns:
the name of the branch
"""
assert int(change)
kwargs.setdefault('name', 'get_change_destination_branch')
assert int(change), change
change = int(change)
branch = self._changes_target_branch_cache.get((host, change))
if branch is not None:
return branch
changes = self.get_changes(
host,
[('change', change)],
limit=1,
**kwargs
name=name or 'get_change_destination_branch',
step_test_data=step_test_data,
)
if not changes or 'branch' not in changes[0]:
self.m.step.active_result.presentation.status = self.m.step.EXCEPTION
raise self.m.step.InfraFailure(
'Error quering for branch of CL %s' % change)
return changes[0]['branch']
branch = changes[0]['branch']
self._changes_target_branch_cache[(host, change)] = branch
return branch
def get_change_description(self, host, change, patchset):
"""
......
......@@ -213,7 +213,7 @@
"--limit",
"1",
"-p",
"change=123"
"change=122"
],
"env": {
"PATH": "<PATH>:RECIPE_PACKAGE_REPO[depot_tools]"
......@@ -239,7 +239,7 @@
"--limit",
"1",
"-p",
"change=123",
"change=122",
"-o",
"ALL_REVISIONS",
"-o",
......@@ -276,7 +276,7 @@
},
{
"name": "$result",
"reason": "2 out of 2 aggregated steps failed. Failures: Error quering for branch of CL 123, Error querying for CL description: host:'https://chromium-review.googlesource.com' change:123; patchset:3",
"reason": "2 out of 2 aggregated steps failed. Failures: Error quering for branch of CL 122, Error querying for CL description: host:'https://chromium-review.googlesource.com' change:122; patchset:3",
"recipe_result": null,
"status_code": 1
}
......
......@@ -36,14 +36,17 @@ def RunSteps(api):
api.gerrit.get_change_description(
host, change=123, patchset=1)
api.gerrit.get_change_destination_branch(host, change=123)
first = api.gerrit.get_change_destination_branch(host, change=123)
# Second call returns cached data.
second = api.gerrit.get_change_destination_branch(host, change=123)
assert first == second
with api.step.defer_results():
api.gerrit.get_change_destination_branch(
host, change=123, name='missing_cl')
host, change=122, name='missing_cl')
api.gerrit.get_change_description(
host, change=123, patchset=3)
host, change=122, patchset=3)
def GenTests(api):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment