Commit 49b8147b authored by Michael Achenbach's avatar Michael Achenbach Committed by Commit Bot

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 <machenbach@chromium.org>
Reviewed-by: 's avatarAndrii Shyshkalov <tandrii@chromium.org>
Reviewed-by: 's avatarAaron Gable <agable@chromium.org>
parent 4cb9d742
......@@ -312,7 +312,18 @@ Create a new branch from given project and commit
Returns:
the ref of the branch created
&mdash; **def [get\_changes](/recipes/recipe_modules/gerrit/api.py#66)(self, host, query_params, start=None, limit=None, \*\*kwargs):**
&mdash; **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
&mdash; **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
&mdash; **def [get\_gerrit\_branch](/recipes/recipe_modules/gerrit/api.py#47)(self, host, project, branch, \*\*kwargs):**
&mdash; **def [get\_gerrit\_branch](/recipes/recipe_modules/gerrit/api.py#49)(self, host, project, branch, \*\*kwargs):**
Get a branch from given project and commit
......
......@@ -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
......
......@@ -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": "<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": "<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
......@@ -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()
)
)
......@@ -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([])
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