diff --git a/recipes/README.recipes.md b/recipes/README.recipes.md index d083ee120..47cab5cc4 100644 --- a/recipes/README.recipes.md +++ b/recipes/README.recipes.md @@ -309,18 +309,18 @@ revision map. This doesn't overwrite the revision if it was already set. Module for interact with gerrit endpoints -— **def [\_\_call\_\_](/recipes/recipe_modules/gerrit/api.py#10)(self, name, cmd, infra_step=True, \*\*kwargs):** +— **def [\_\_call\_\_](/recipes/recipe_modules/gerrit/api.py#14)(self, name, cmd, infra_step=True, \*\*kwargs):** Wrapper for easy calling of gerrit_utils steps. -— **def [create\_gerrit\_branch](/recipes/recipe_modules/gerrit/api.py#27)(self, host, project, branch, commit, \*\*kwargs):** +— **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 -— **def [get\_change\_description](/recipes/recipe_modules/gerrit/api.py#93)(self, host, change, patchset):** +— **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. -— **def [get\_change\_destination\_branch](/recipes/recipe_modules/gerrit/api.py#68)(self, host, change, \*\*kwargs):** +— **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 -— **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):** +— **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 -— **def [get\_gerrit\_branch](/recipes/recipe_modules/gerrit/api.py#49)(self, host, project, branch, \*\*kwargs):** +— **def [get\_gerrit\_branch](/recipes/recipe_modules/gerrit/api.py#53)(self, host, project, branch, \*\*kwargs):** Get a branch from given project and commit diff --git a/recipes/recipe_modules/gerrit/api.py b/recipes/recipe_modules/gerrit/api.py index c294a3674..3f97d4848 100644 --- a/recipes/recipe_modules/gerrit/api.py +++ b/recipes/recipe_modules/gerrit/api.py @@ -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): """ diff --git a/recipes/recipe_modules/gerrit/examples/full.expected/basic.json b/recipes/recipe_modules/gerrit/examples/full.expected/basic.json index 3a4249fac..251783ba0 100644 --- a/recipes/recipe_modules/gerrit/examples/full.expected/basic.json +++ b/recipes/recipe_modules/gerrit/examples/full.expected/basic.json @@ -213,7 +213,7 @@ "--limit", "1", "-p", - "change=123" + "change=122" ], "env": { "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 } diff --git a/recipes/recipe_modules/gerrit/examples/full.py b/recipes/recipe_modules/gerrit/examples/full.py index 76dd66498..09dbf2f0c 100644 --- a/recipes/recipe_modules/gerrit/examples/full.py +++ b/recipes/recipe_modules/gerrit/examples/full.py @@ -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):