From 49b8147be074d05a9a8e0bdb05e40d27dfd789c0 Mon Sep 17 00:00:00 2001 From: Michael Achenbach Date: Tue, 11 Jul 2017 10:42:18 +0200 Subject: [PATCH] Add support to query a CL's branch to gerrit recipe module Bug: 740456 Change-Id: Ic4f3c1f046cfa025d8e60172ee58e2b2e1b76ee6 Reviewed-on: https://chromium-review.googlesource.com/565560 Commit-Queue: Michael Achenbach Reviewed-by: Andrii Shyshkalov Reviewed-by: Aaron Gable --- recipes/README.recipes.md | 15 ++++- recipes/recipe_modules/gerrit/api.py | 29 ++++++++- .../gerrit/examples/full.expected/basic.json | 65 ++++++++++++++++++- .../recipe_modules/gerrit/examples/full.py | 10 +++ recipes/recipe_modules/gerrit/test_api.py | 3 + 5 files changed, 118 insertions(+), 4 deletions(-) diff --git a/recipes/README.recipes.md b/recipes/README.recipes.md index 84d568b04c..525f21a424 100644 --- a/recipes/README.recipes.md +++ b/recipes/README.recipes.md @@ -312,7 +312,18 @@ Create a new branch from given project and commit Returns: the ref of the branch created -— **def [get\_changes](/recipes/recipe_modules/gerrit/api.py#66)(self, host, query_params, start=None, limit=None, \*\*kwargs):** +— **def [get\_change\_destination\_branch](/recipes/recipe_modules/gerrit/api.py#68)(self, host, change, \*\*kwargs):** + +Get the upstream branch for a given CL. + +Args: + host: Gerrit host to query. + change: The change number. + +Returns: + the name of the branch + +— **def [get\_changes](/recipes/recipe_modules/gerrit/api.py#93)(self, host, query_params, start=None, limit=None, \*\*kwargs):** Query changes for the given host. @@ -327,7 +338,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#47)(self, host, project, branch, \*\*kwargs):** +— **def [get\_gerrit\_branch](/recipes/recipe_modules/gerrit/api.py#49)(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 a37ad2d82c..69621fca2b 100644 --- a/recipes/recipe_modules/gerrit/api.py +++ b/recipes/recipe_modules/gerrit/api.py @@ -44,6 +44,8 @@ class GerritApi(recipe_api.RecipeApi): ref = step_result.json.output.get('ref') return ref + # TODO(machenbach): Rename to get_revision? And maybe above to + # create_ref? def get_gerrit_branch(self, host, project, branch, **kwargs): """ Get a branch from given project and commit @@ -63,6 +65,31 @@ class GerritApi(recipe_api.RecipeApi): revision = step_result.json.output.get('revision') return revision + def get_change_destination_branch(self, host, change, **kwargs): + """ + Get the upstream branch for a given CL. + + 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') + changes = self.get_changes( + host, + [('change', change)], + limit=1, + **kwargs + ) + 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'] + def get_changes(self, host, query_params, start=None, limit=None, **kwargs): """ Query changes for the given host. @@ -91,7 +118,7 @@ class GerritApi(recipe_api.RecipeApi): args += ['-p', '%s=%s' % (k, v)] return self( - 'changes', + kwargs.pop('name', 'changes'), args, step_test_data=lambda: self.test_api.get_changes_response_data(), **kwargs diff --git a/recipes/recipe_modules/gerrit/examples/full.expected/basic.json b/recipes/recipe_modules/gerrit/examples/full.expected/basic.json index 1c2a6ce816..f2b2b9dfef 100644 --- a/recipes/recipe_modules/gerrit/examples/full.expected/basic.json +++ b/recipes/recipe_modules/gerrit/examples/full.expected/basic.json @@ -100,9 +100,72 @@ "@@@STEP_LOG_END@json.output@@@" ] }, + { + "cmd": [ + "python", + "-u", + "RECIPE_PACKAGE_REPO[depot_tools]/gerrit_client.py", + "changes", + "--host", + "https://chromium-review.googlesource.com", + "--json_file", + "/path/to/tmp/json", + "--limit", + "1", + "-p", + "change=123" + ], + "env": { + "PATH": ":RECIPE_PACKAGE_REPO[depot_tools]" + }, + "infra_step": true, + "name": "gerrit get_change_destination_branch", + "~followup_annotations": [ + "@@@STEP_LOG_LINE@json.output@[@@@", + "@@@STEP_LOG_LINE@json.output@ {@@@", + "@@@STEP_LOG_LINE@json.output@ \"_number\": \"91827\", @@@", + "@@@STEP_LOG_LINE@json.output@ \"branch\": \"master\", @@@", + "@@@STEP_LOG_LINE@json.output@ \"change_id\": \"Ideadbeef\", @@@", + "@@@STEP_LOG_LINE@json.output@ \"created\": \"2017-01-30 13:11:20.000000000\", @@@", + "@@@STEP_LOG_LINE@json.output@ \"has_review_started\": false, @@@", + "@@@STEP_LOG_LINE@json.output@ \"project\": \"chromium/src\", @@@", + "@@@STEP_LOG_LINE@json.output@ \"status\": \"NEW\", @@@", + "@@@STEP_LOG_LINE@json.output@ \"subject\": \"Change title\"@@@", + "@@@STEP_LOG_LINE@json.output@ }@@@", + "@@@STEP_LOG_LINE@json.output@]@@@", + "@@@STEP_LOG_END@json.output@@@" + ] + }, + { + "cmd": [ + "python", + "-u", + "RECIPE_PACKAGE_REPO[depot_tools]/gerrit_client.py", + "changes", + "--host", + "https://chromium-review.googlesource.com", + "--json_file", + "/path/to/tmp/json", + "--limit", + "1", + "-p", + "change=123" + ], + "env": { + "PATH": ":RECIPE_PACKAGE_REPO[depot_tools]" + }, + "infra_step": true, + "name": "gerrit missing_cl", + "~followup_annotations": [ + "@@@STEP_LOG_LINE@json.output@[]@@@", + "@@@STEP_LOG_END@json.output@@@", + "@@@STEP_EXCEPTION@@@" + ] + }, { "name": "$result", + "reason": "Error quering for branch of CL 123", "recipe_result": null, - "status_code": 0 + "status_code": 1 } ] \ No newline at end of file diff --git a/recipes/recipe_modules/gerrit/examples/full.py b/recipes/recipe_modules/gerrit/examples/full.py index 48784a04e3..ecab1e89a8 100644 --- a/recipes/recipe_modules/gerrit/examples/full.py +++ b/recipes/recipe_modules/gerrit/examples/full.py @@ -32,6 +32,12 @@ def RunSteps(api): limit=1, ) + api.gerrit.get_change_destination_branch(host, change=123) + + api.gerrit.get_change_destination_branch( + host, change=123, name='missing_cl') + + def GenTests(api): yield ( api.test('basic') @@ -43,4 +49,8 @@ def GenTests(api): 'gerrit get_gerrit_branch', api.gerrit.make_gerrit_get_branch_response_data() ) + + api.step_data( + 'gerrit missing_cl', + api.gerrit.get_empty_changes_response_data() + ) ) diff --git a/recipes/recipe_modules/gerrit/test_api.py b/recipes/recipe_modules/gerrit/test_api.py index 4588bc6bef..9b1be1c056 100644 --- a/recipes/recipe_modules/gerrit/test_api.py +++ b/recipes/recipe_modules/gerrit/test_api.py @@ -38,3 +38,6 @@ class GerritTestApi(recipe_test_api.RecipeTestApi): 'subject': 'Change title', }, ]) + + def get_empty_changes_response_data(self): + return self._make_gerrit_response_json([])