Kaynağa Gözat

[gerrit_client.py] Add restore command

Bug: 40275665
Change-Id: I9b26758b958473dd839b114347b01249cc0a7c46
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5545091
Commit-Queue: Yiwei Zhang <yiwzhang@google.com>
Auto-Submit: Rob Mohr <mohrr@google.com>
Reviewed-by: Yiwei Zhang <yiwzhang@google.com>
Rob Mohr 1 yıl önce
ebeveyn
işleme
46087b7a8c

+ 17 - 0
gerrit_client.py

@@ -393,6 +393,23 @@ def CMDsetlabel(parser, args):
     write_result(result, opt)
     write_result(result, opt)
 
 
 
 
+@subcommand.usage('')
+def CMDrestore(parser, args):
+    """Restores a Gerrit change."""
+    parser.add_option('-c', '--change', type=str, help='change number')
+    parser.add_option('-m',
+                      '--message',
+                      default='',
+                      help='reason for restoring')
+
+    (opt, args) = parser.parse_args(args)
+    assert opt.change, "-c not defined"
+    result = gerrit_util.RestoreChange(
+        urllib.parse.urlparse(opt.host).netloc, opt.change, opt.message)
+    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."""

+ 5 - 3
recipes/README.recipes.md

@@ -370,11 +370,13 @@ 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#298)(self, host, project, from_branch, to_branch, step_test_data=None):**
+&mdash; **def [move\_changes](/recipes/recipe_modules/gerrit/api.py#318)(self, host, project, from_branch, to_branch, step_test_data=None):**
 
 
-&mdash; **def [set\_change\_label](/recipes/recipe_modules/gerrit/api.py#280)(self, host, change, label_name, label_value, name=None, step_test_data=None):**
+&mdash; **def [restore\_change](/recipes/recipe_modules/gerrit/api.py#280)(self, host, change, message=None, name=None, step_test_data=None):**
 
 
-&mdash; **def [update\_files](/recipes/recipe_modules/gerrit/api.py#322)(self, host, project, branch, new_contents_by_file_path, commit_msg, params=frozenset(['status=NEW']), cc_list=frozenset([]), submit=False, submit_later=False, step_test_data_create_change=None, step_test_data_submit_change=None):**
+&mdash; **def [set\_change\_label](/recipes/recipe_modules/gerrit/api.py#300)(self, host, change, label_name, label_value, name=None, step_test_data=None):**
+
+&mdash; **def [update\_files](/recipes/recipe_modules/gerrit/api.py#342)(self, host, project, branch, new_contents_by_file_path, commit_msg, params=frozenset(['status=NEW']), cc_list=frozenset([]), submit=False, submit_later=False, step_test_data_create_change=None, step_test_data_submit_change=None):**
 
 
 Update a set of files by creating and submitting a Gerrit CL.
 Update a set of files by creating and submitting a Gerrit CL.
 
 

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

@@ -277,6 +277,26 @@ class GerritApi(recipe_api.RecipeApi):
         step_test_data=step_test_data,
         step_test_data=step_test_data,
     ).json.output
     ).json.output
 
 
+  def restore_change(self, host, change, message=None, name=None,
+                     step_test_data=None):
+    args = [
+        'restore',
+        '--host', host,
+        '--change', change,
+        '--json_file', self.m.json.output(),
+    ]
+    if message:
+      args.extend(('--message', message))
+    if not step_test_data:
+      step_test_data = lambda: self.test_api.get_one_change_response_data(
+          status='NEW', _number=str(change))
+
+    return self(
+        name or 'restore',
+        args,
+        step_test_data=step_test_data,
+    ).json.output
+
   def set_change_label(self,
   def set_change_label(self,
                        host,
                        host,
                        change,
                        change,

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

@@ -696,6 +696,50 @@
       "@@@STEP_LOG_END@json.output@@@"
       "@@@STEP_LOG_END@json.output@@@"
     ]
     ]
   },
   },
+  {
+    "cmd": [
+      "vpython3",
+      "RECIPE_REPO[depot_tools]/gerrit_client.py",
+      "restore",
+      "--host",
+      "https://chromium-review.googlesource.com",
+      "--change",
+      "123",
+      "--json_file",
+      "/path/to/tmp/json",
+      "--message",
+      "nevermind"
+    ],
+    "env": {
+      "PATH": "<PATH>:RECIPE_REPO[depot_tools]"
+    },
+    "infra_step": true,
+    "name": "gerrit restore",
+    "~followup_annotations": [
+      "@@@STEP_LOG_LINE@json.output@[@@@",
+      "@@@STEP_LOG_LINE@json.output@  {@@@",
+      "@@@STEP_LOG_LINE@json.output@    \"_number\": \"123\",@@@",
+      "@@@STEP_LOG_LINE@json.output@    \"branch\": \"main\",@@@",
+      "@@@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@    \"id\": \"fully~qualified~changeid\",@@@",
+      "@@@STEP_LOG_LINE@json.output@    \"project\": \"chromium/src\",@@@",
+      "@@@STEP_LOG_LINE@json.output@    \"revisions\": {@@@",
+      "@@@STEP_LOG_LINE@json.output@      \"184ebe53805e102605d11f6b143486d15c23a09c\": {@@@",
+      "@@@STEP_LOG_LINE@json.output@        \"_number\": \"1\",@@@",
+      "@@@STEP_LOG_LINE@json.output@        \"commit\": {@@@",
+      "@@@STEP_LOG_LINE@json.output@          \"message\": \"Change commit message\"@@@",
+      "@@@STEP_LOG_LINE@json.output@        }@@@",
+      "@@@STEP_LOG_LINE@json.output@      }@@@",
+      "@@@STEP_LOG_LINE@json.output@    },@@@",
+      "@@@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": [
     "cmd": [
       "vpython3",
       "vpython3",

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

@@ -88,6 +88,7 @@ def RunSteps(api):
   api.gerrit.set_change_label(host, 123, 'commit-queue', 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')
+  api.gerrit.restore_change(host, 123, 'nevermind')
 
 
   api.gerrit.get_change_description(
   api.gerrit.get_change_description(
       host,
       host,