Skip to content

Commit

Permalink
recipes: cache destination branch of a Gerrit change.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
shishkander authored and Commit Bot committed Apr 26, 2018
1 parent 0456622 commit 3cb0767
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
14 changes: 8 additions & 6 deletions recipes/README.recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -332,18 +332,20 @@ 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.

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.

Expand All @@ -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

Expand Down
23 changes: 18 additions & 5 deletions recipes/recipe_modules/gerrit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
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))
Expand Down Expand Up @@ -65,30 +69,39 @@ def get_gerrit_branch(self, host, project, branch, **kwargs):
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.
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):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
"--limit",
"1",
"-p",
"change=123"
"change=122"
],
"env": {
"PATH": "<PATH>:RECIPE_PACKAGE_REPO[depot_tools]"
Expand All @@ -239,7 +239,7 @@
"--limit",
"1",
"-p",
"change=123",
"change=122",
"-o",
"ALL_REVISIONS",
"-o",
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 6 additions & 3 deletions recipes/recipe_modules/gerrit/examples/full.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 3cb0767

Please sign in to comment.