Browse Source

Add a 'setlabel' command to gerrit_client.py

And add a hook for it in the gerrit recipe_module.

This will allow invocations of gerrit_client to set votes like
"code-review-1" or "commit-queue+1" on CLs. This will be used by bots
to control CLs they just created with update_files().

Bug: 1250084
Change-Id: I888458983165fed5882e5c37cba4a69dbfb9b810
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3207266
Commit-Queue: Ben Pastene <bpastene@chromium.org>
Reviewed-by: Josip Sokcevic <sokcevic@google.com>
Ben Pastene 3 years ago
parent
commit
281edf7577

+ 16 - 0
gerrit_client.py

@@ -284,6 +284,22 @@ def CMDsetbotcommit(parser, args):
   write_result(result, opt)
   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('')
 @subcommand.usage('')
 def CMDabandon(parser, args):
 def CMDabandon(parser, args):
   """Abandons a Gerrit change."""
   """Abandons a Gerrit change."""

+ 4 - 2
recipes/README.recipes.md

@@ -332,9 +332,11 @@ Returns:
   A dict for the target revision as documented here:
   A dict for the target revision as documented here:
       https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-changes
       https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-changes
 
 
-&mdash; **def [move\_changes](/recipes/recipe_modules/gerrit/api.py#229)(self, host, project, from_branch, to_branch, step_test_data=None):**
+&mdash; **def [move\_changes](/recipes/recipe_modules/gerrit/api.py#247)(self, host, project, from_branch, to_branch, step_test_data=None):**
 
 
-&mdash; **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):**
+&mdash; **def [set\_change\_label](/recipes/recipe_modules/gerrit/api.py#229)(self, host, change, label_name, label_value, name=None, step_test_data=None):**
+
+&mdash; **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.
 Update a set of files by creating and submitting a Gerrit CL.
 
 

+ 18 - 0
recipes/recipe_modules/gerrit/api.py

@@ -226,6 +226,24 @@ class GerritApi(recipe_api.RecipeApi):
         step_test_data=step_test_data,
         step_test_data=step_test_data,
     ).json.output
     ).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,
   def move_changes(self,
                    host,
                    host,
                    project,
                    project,

+ 54 - 0
recipes/recipe_modules/gerrit/examples/full.expected/basic.json

@@ -450,6 +450,60 @@
       "@@@STEP_LOG_END@json.output@@@"
       "@@@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": "<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": "<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": [
     "cmd": [
       "vpython",
       "vpython",

+ 3 - 0
recipes/recipe_modules/gerrit/examples/full.py

@@ -65,6 +65,9 @@ def RunSteps(api):
   api.gerrit.get_change_description(
   api.gerrit.get_change_description(
       host, change=123, patchset=1)
       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')
   api.gerrit.abandon_change(host, 123, 'bad roll')
 
 
   with api.step.defer_results():
   with api.step.defer_results():

+ 16 - 0
tests/gerrit_client_test.py

@@ -105,6 +105,22 @@ class TestGerritClient(unittest.TestCase):
     ])
     ])
     util_mock.assert_called_once_with('example.org', 1, 'bar')
     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__':
 if __name__ == '__main__':
   logging.basicConfig(
   logging.basicConfig(