diff --git a/gerrit_client.py b/gerrit_client.py index 89ca0cc08f..a4ed70ccf9 100755 --- a/gerrit_client.py +++ b/gerrit_client.py @@ -284,6 +284,22 @@ def CMDsetbotcommit(parser, args): write_result(result, opt) +@subcommand.usage('[args ...]') +def CMDsetlabel(parser, args): + """Sets a label to a specific value on a given change.""" + parser.add_option('-c', '--change', type=int, help='change number') + parser.add_option('-l', + '--label', + nargs=2, + metavar=('label_name', 'label_value')) + (opt, args) = parser.parse_args(args) + result = gerrit_util.SetReview(urlparse.urlparse(opt.host).netloc, + opt.change, + labels={opt.label[0]: opt.label[1]}) + logging.info(result) + write_result(result, opt) + + @subcommand.usage('') def CMDabandon(parser, args): """Abandons a Gerrit change.""" diff --git a/recipes/README.recipes.md b/recipes/README.recipes.md index 3e587c2cfe..639ef2432a 100644 --- a/recipes/README.recipes.md +++ b/recipes/README.recipes.md @@ -332,9 +332,11 @@ Returns: A dict for the target revision as documented here: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-changes -— **def [move\_changes](/recipes/recipe_modules/gerrit/api.py#229)(self, host, project, from_branch, to_branch, step_test_data=None):** +— **def [move\_changes](/recipes/recipe_modules/gerrit/api.py#247)(self, host, project, from_branch, to_branch, step_test_data=None):** -— **def [update\_files](/recipes/recipe_modules/gerrit/api.py#253)(self, host, project, branch, new_contents_by_file_path, commit_msg, params=frozenset(['status=NEW']), submit=False):** +— **def [set\_change\_label](/recipes/recipe_modules/gerrit/api.py#229)(self, host, change, label_name, label_value, name=None, step_test_data=None):** + +— **def [update\_files](/recipes/recipe_modules/gerrit/api.py#271)(self, host, project, branch, new_contents_by_file_path, commit_msg, params=frozenset(['status=NEW']), submit=False):** Update a set of files by creating and submitting a Gerrit CL. diff --git a/recipes/recipe_modules/gerrit/api.py b/recipes/recipe_modules/gerrit/api.py index d11c51086e..790c8eb5eb 100644 --- a/recipes/recipe_modules/gerrit/api.py +++ b/recipes/recipe_modules/gerrit/api.py @@ -226,6 +226,24 @@ class GerritApi(recipe_api.RecipeApi): step_test_data=step_test_data, ).json.output + def set_change_label(self, + host, + change, + label_name, + label_value, + name=None, + step_test_data=None): + args = [ + 'setlabel', '--host', host, '--change', + int(change), '--json_file', + self.m.json.output(), '-l', label_name, label_value + ] + return self( + name or 'setlabel', + args, + step_test_data=step_test_data, + ).json.output + def move_changes(self, host, project, diff --git a/recipes/recipe_modules/gerrit/examples/full.expected/basic.json b/recipes/recipe_modules/gerrit/examples/full.expected/basic.json index 449d7ac5b1..95e55f7ab3 100644 --- a/recipes/recipe_modules/gerrit/examples/full.expected/basic.json +++ b/recipes/recipe_modules/gerrit/examples/full.expected/basic.json @@ -450,6 +450,60 @@ "@@@STEP_LOG_END@json.output@@@" ] }, + { + "cmd": [ + "vpython", + "-u", + "RECIPE_REPO[depot_tools]/gerrit_client.py", + "setlabel", + "--host", + "https://chromium-review.googlesource.com", + "--change", + "123", + "--json_file", + "/path/to/tmp/json", + "-l", + "code-review", + "-1" + ], + "env": { + "PATH": ":RECIPE_REPO[depot_tools]" + }, + "infra_step": true, + "name": "gerrit setlabel", + "~followup_annotations": [ + "@@@STEP_LOG_END@json.output (invalid)@@@", + "@@@STEP_LOG_LINE@json.output (exception)@No JSON object could be decoded@@@", + "@@@STEP_LOG_END@json.output (exception)@@@" + ] + }, + { + "cmd": [ + "vpython", + "-u", + "RECIPE_REPO[depot_tools]/gerrit_client.py", + "setlabel", + "--host", + "https://chromium-review.googlesource.com", + "--change", + "123", + "--json_file", + "/path/to/tmp/json", + "-l", + "commit-queue", + "1" + ], + "env": { + "PATH": ":RECIPE_REPO[depot_tools]" + }, + "infra_step": true, + "name": "gerrit setlabel (2)", + "~followup_annotations": [ + "@@@STEP_LOG_END@json.output (invalid)@@@", + "@@@STEP_LOG_LINE@json.output (exception)@No JSON object could be decoded@@@", + "@@@STEP_LOG_END@json.output (exception)@@@" + ] + }, { "cmd": [ "vpython", diff --git a/recipes/recipe_modules/gerrit/examples/full.py b/recipes/recipe_modules/gerrit/examples/full.py index ec617f7278..1a413766b2 100644 --- a/recipes/recipe_modules/gerrit/examples/full.py +++ b/recipes/recipe_modules/gerrit/examples/full.py @@ -65,6 +65,9 @@ def RunSteps(api): api.gerrit.get_change_description( host, change=123, patchset=1) + api.gerrit.set_change_label(host, 123, 'code-review', -1) + api.gerrit.set_change_label(host, 123, 'commit-queue', 1) + api.gerrit.abandon_change(host, 123, 'bad roll') with api.step.defer_results(): diff --git a/tests/gerrit_client_test.py b/tests/gerrit_client_test.py index 1520b9ea27..9cabc9c2b0 100755 --- a/tests/gerrit_client_test.py +++ b/tests/gerrit_client_test.py @@ -105,6 +105,22 @@ class TestGerritClient(unittest.TestCase): ]) util_mock.assert_called_once_with('example.org', 1, 'bar') + @mock.patch('gerrit_util.SetReview', return_value='') + def test_setlabel(self, util_mock): + gerrit_client.main([ + 'setlabel', + '--host', + 'https://example.org/foo', + '-c', + '1', + '-l', + 'some-label', + '-2', + ]) + util_mock.assert_called_once_with('example.org', + 1, + labels={'some-label': '-2'}) + if __name__ == '__main__': logging.basicConfig(