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: Michael Achenbach <machenbach@chromium.org>
changes/27/1029227/9
Andrii Shyshkalov 7 years ago committed by Commit Bot
parent 04566221ed
commit 3cb0767ac1

@ -309,18 +309,18 @@ revision map. This doesn't overwrite the revision if it was already set.
Module for interact with gerrit endpoints 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. 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 Create a new branch from given project and commit
Returns: Returns:
the ref of the branch created 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. Get the description for a given CL and patchset.
@ -332,10 +332,12 @@ Args:
Returns: Returns:
The description corresponding to given CL and patchset. 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. Get the upstream branch for a given CL.
Result is cached.
Args: Args:
host: Gerrit host to query. host: Gerrit host to query.
change: The change number. change: The change number.
@ -343,7 +345,7 @@ Args:
Returns: Returns:
the name of the branch 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. Query changes for the given host.
@ -361,7 +363,7 @@ Returns:
A list of change dicts as documented here: A list of change dicts as documented here:
https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-changes 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 Get a branch from given project and commit

@ -7,6 +7,10 @@ from recipe_engine import recipe_api
class GerritApi(recipe_api.RecipeApi): class GerritApi(recipe_api.RecipeApi):
"""Module for interact with gerrit endpoints""" """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): def __call__(self, name, cmd, infra_step=True, **kwargs):
"""Wrapper for easy calling of gerrit_utils steps.""" """Wrapper for easy calling of gerrit_utils steps."""
assert isinstance(cmd, (list, tuple)) assert isinstance(cmd, (list, tuple))
@ -65,10 +69,13 @@ class GerritApi(recipe_api.RecipeApi):
revision = step_result.json.output.get('revision') revision = step_result.json.output.get('revision')
return 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. Get the upstream branch for a given CL.
Result is cached.
Args: Args:
host: Gerrit host to query. host: Gerrit host to query.
change: The change number. change: The change number.
@ -76,19 +83,25 @@ class GerritApi(recipe_api.RecipeApi):
Returns: Returns:
the name of the branch the name of the branch
""" """
assert int(change) assert int(change), change
kwargs.setdefault('name', 'get_change_destination_branch') change = int(change)
branch = self._changes_target_branch_cache.get((host, change))
if branch is not None:
return branch
changes = self.get_changes( changes = self.get_changes(
host, host,
[('change', change)], [('change', change)],
limit=1, 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]: if not changes or 'branch' not in changes[0]:
self.m.step.active_result.presentation.status = self.m.step.EXCEPTION self.m.step.active_result.presentation.status = self.m.step.EXCEPTION
raise self.m.step.InfraFailure( raise self.m.step.InfraFailure(
'Error quering for branch of CL %s' % change) '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): def get_change_description(self, host, change, patchset):
""" """

@ -213,7 +213,7 @@
"--limit", "--limit",
"1", "1",
"-p", "-p",
"change=123" "change=122"
], ],
"env": { "env": {
"PATH": "<PATH>:RECIPE_PACKAGE_REPO[depot_tools]" "PATH": "<PATH>:RECIPE_PACKAGE_REPO[depot_tools]"
@ -239,7 +239,7 @@
"--limit", "--limit",
"1", "1",
"-p", "-p",
"change=123", "change=122",
"-o", "-o",
"ALL_REVISIONS", "ALL_REVISIONS",
"-o", "-o",
@ -276,7 +276,7 @@
}, },
{ {
"name": "$result", "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, "recipe_result": null,
"status_code": 1 "status_code": 1
} }

@ -36,14 +36,17 @@ def RunSteps(api):
api.gerrit.get_change_description( api.gerrit.get_change_description(
host, change=123, patchset=1) 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(): with api.step.defer_results():
api.gerrit.get_change_destination_branch( api.gerrit.get_change_destination_branch(
host, change=123, name='missing_cl') host, change=122, name='missing_cl')
api.gerrit.get_change_description( api.gerrit.get_change_description(
host, change=123, patchset=3) host, change=122, patchset=3)
def GenTests(api): def GenTests(api):

Loading…
Cancel
Save