浏览代码

Add 'create tag' support to gerrit_client and gerrit recipe_module.

BUG=1052179
R=dpranke@google.com, linxinan@chromium.org

Change-Id: I6e220b70e63d990e9257d9fe4d107de7dbd2eb81
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3232849
Auto-Submit: Michael Moss <mmoss@chromium.org>
Commit-Queue: Michael Moss <mmoss@chromium.org>
Reviewed-by: Xinan Lin <linxinan@chromium.org>
Reviewed-by: Dirk Pranke <dpranke@google.com>
Michael Moss 3 年之前
父节点
当前提交
b6ce244503

+ 20 - 0
gerrit_client.py

@@ -100,6 +100,26 @@ def CMDbranch(parser, args):
   write_result(result, opt)
 
 
+@subcommand.usage('[args ...]')
+def CMDtag(parser, args):
+  """Create a tag in a gerrit project."""
+  parser.add_option('--tag', dest='tag', help='tag name')
+  parser.add_option('--commit', dest='commit', help='commit hash')
+
+  (opt, args) = parser.parse_args(args)
+  assert opt.project, "--project not defined"
+  assert opt.tag, "--tag not defined"
+  assert opt.commit, "--commit not defined"
+
+  project = quote_plus(opt.project)
+  host = urlparse.urlparse(opt.host).netloc
+  tag = quote_plus(opt.tag)
+  commit = quote_plus(opt.commit)
+  result = gerrit_util.CreateGerritTag(host, project, tag, commit)
+  logging.info(result)
+  write_result(result, opt)
+
+
 @subcommand.usage('[args ...]')
 def CMDhead(parser, args):
   """Update which branch the project HEAD points to."""

+ 17 - 0
gerrit_util.py

@@ -1015,6 +1015,23 @@ def CreateGerritBranch(host, project, branch, commit):
   raise GerritError(200, 'Unable to create gerrit branch')
 
 
+def CreateGerritTag(host, project, tag, commit):
+  """Creates a new tag at the given commit.
+
+  https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#create-tag
+
+  Returns:
+    A JSON object with 'ref' key.
+  """
+  path = 'projects/%s/tags/%s' % (project, tag)
+  body = {'revision': commit}
+  conn = CreateHttpConn(host, path, reqtype='PUT', body=body)
+  response = ReadHttpJsonResponse(conn, accept_statuses=[201])
+  if response:
+    return response
+  raise GerritError(200, 'Unable to create gerrit tag')
+
+
 def GetHead(host, project):
   """Retrieves current HEAD of Gerrit project
 

+ 16 - 9
recipes/README.recipes.md

@@ -253,7 +253,7 @@ Module for interact with Gerrit endpoints
 
 Wrapper for easy calling of gerrit_utils steps.
 
-&mdash; **def [abandon\_change](/recipes/recipe_modules/gerrit/api.py#209)(self, host, change, message=None, name=None, step_test_data=None):**
+&mdash; **def [abandon\_change](/recipes/recipe_modules/gerrit/api.py#228)(self, host, change, message=None, name=None, step_test_data=None):**
 
 &mdash; **def [create\_gerrit\_branch](/recipes/recipe_modules/gerrit/api.py#32)(self, host, project, branch, commit, \*\*kwargs):**
 
@@ -262,7 +262,14 @@ Creates a new branch from given project and commit
 Returns:
   The ref of the branch created
 
-&mdash; **def [get\_change\_description](/recipes/recipe_modules/gerrit/api.py#71)(self, host, change, patchset, timeout=None, step_test_data=None):**
+&mdash; **def [create\_gerrit\_tag](/recipes/recipe_modules/gerrit/api.py#51)(self, host, project, tag, commit, \*\*kwargs):**
+
+Creates a new tag at the given commit.
+
+Returns:
+  The ref of the tag created.
+
+&mdash; **def [get\_change\_description](/recipes/recipe_modules/gerrit/api.py#90)(self, host, change, patchset, timeout=None, step_test_data=None):**
 
 Gets the description for a given CL and patchset.
 
@@ -274,7 +281,7 @@ Args:
 Returns:
   The description corresponding to given CL and patchset.
 
-&mdash; **def [get\_changes](/recipes/recipe_modules/gerrit/api.py#131)(self, host, query_params, start=None, limit=None, o_params=None, step_test_data=None, \*\*kwargs):**
+&mdash; **def [get\_changes](/recipes/recipe_modules/gerrit/api.py#150)(self, host, query_params, start=None, limit=None, o_params=None, step_test_data=None, \*\*kwargs):**
 
 Queries changes for the given host.
 
@@ -293,14 +300,14 @@ 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#53)(self, host, project, branch, \*\*kwargs):**
+&mdash; **def [get\_gerrit\_branch](/recipes/recipe_modules/gerrit/api.py#72)(self, host, project, branch, \*\*kwargs):**
 
 Gets a branch from given project and commit
 
 Returns:
   The revision of the branch
 
-&mdash; **def [get\_related\_changes](/recipes/recipe_modules/gerrit/api.py#173)(self, host, change, revision='current', step_test_data=None):**
+&mdash; **def [get\_related\_changes](/recipes/recipe_modules/gerrit/api.py#192)(self, host, change, revision='current', step_test_data=None):**
 
 Queries related changes for a given host, change, and revision.
 
@@ -319,7 +326,7 @@ Returns:
   A related changes dictionary as documented here:
       https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#related-changes-info
 
-&mdash; **def [get\_revision\_info](/recipes/recipe_modules/gerrit/api.py#90)(self, host, change, patchset, timeout=None, step_test_data=None):**
+&mdash; **def [get\_revision\_info](/recipes/recipe_modules/gerrit/api.py#109)(self, host, change, patchset, timeout=None, step_test_data=None):**
 
 Returns the info for a given patchset of a given change.
 
@@ -332,11 +339,11 @@ Returns:
   A dict for the target revision as documented here:
       https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-changes
 
-&mdash; **def [move\_changes](/recipes/recipe_modules/gerrit/api.py#247)(self, host, project, from_branch, to_branch, step_test_data=None):**
+&mdash; **def [move\_changes](/recipes/recipe_modules/gerrit/api.py#266)(self, host, project, from_branch, to_branch, step_test_data=None):**
 
-&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 [set\_change\_label](/recipes/recipe_modules/gerrit/api.py#248)(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):**
+&mdash; **def [update\_files](/recipes/recipe_modules/gerrit/api.py#290)(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.
 

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

@@ -48,6 +48,25 @@ class GerritApi(recipe_api.RecipeApi):
     ref = step_result.json.output.get('ref')
     return ref
 
+  def create_gerrit_tag(self, host, project, tag, commit, **kwargs):
+    """Creates a new tag at the given commit.
+
+    Returns:
+      The ref of the tag created.
+    """
+    args = [
+        'tag',
+        '--host', host,
+        '--project', project,
+        '--tag', tag,
+        '--commit', commit,
+        '--json_file', self.m.json.output()
+    ]
+    step_name = 'create_gerrit_tag (%s %s)' % (project, tag)
+    step_result = self(step_name, args, **kwargs)
+    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):

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

@@ -58,6 +58,37 @@
       "@@@STEP_LOG_END@json.output@@@"
     ]
   },
+  {
+    "cmd": [
+      "vpython",
+      "-u",
+      "RECIPE_REPO[depot_tools]/gerrit_client.py",
+      "tag",
+      "--host",
+      "https://chromium-review.googlesource.com",
+      "--project",
+      "v8/v8",
+      "--tag",
+      "1.0",
+      "--commit",
+      "67ebf73496383c6777035e374d2d664009e2aa5c",
+      "--json_file",
+      "/path/to/tmp/json"
+    ],
+    "env": {
+      "PATH": "<PATH>:RECIPE_REPO[depot_tools]"
+    },
+    "infra_step": true,
+    "name": "gerrit create_gerrit_tag (v8/v8 1.0)",
+    "~followup_annotations": [
+      "@@@STEP_LOG_LINE@json.output@{@@@",
+      "@@@STEP_LOG_LINE@json.output@  \"can_delete\": true, @@@",
+      "@@@STEP_LOG_LINE@json.output@  \"ref\": \"refs/tags/1.0\", @@@",
+      "@@@STEP_LOG_LINE@json.output@  \"revision\": \"67ebf73496383c6777035e374d2d664009e2aa5c\"@@@",
+      "@@@STEP_LOG_LINE@json.output@}@@@",
+      "@@@STEP_LOG_END@json.output@@@"
+    ]
+  },
   {
     "cmd": [
       "vpython",

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

@@ -23,6 +23,9 @@ def RunSteps(api):
   data = api.gerrit.get_gerrit_branch(host, project, 'main')
   assert data == '67ebf73496383c6777035e374d2d664009e2aa5c'
 
+  data = api.gerrit.create_gerrit_tag(host, project, '1.0', commit)
+  assert data == 'refs/tags/1.0'
+
   api.gerrit.move_changes(host, project, 'master', 'main')
 
   change_info = api.gerrit.update_files(host,
@@ -82,6 +85,8 @@ def GenTests(api):
   yield (api.test('basic') +
          api.step_data('gerrit create_gerrit_branch (v8/v8 test)',
                        api.gerrit.make_gerrit_create_branch_response_data()) +
+         api.step_data('gerrit create_gerrit_tag (v8/v8 1.0)',
+                       api.gerrit.make_gerrit_create_tag_response_data()) +
          api.step_data('gerrit create change at (v8/v8 main)',
                        api.gerrit.update_files_response_data()) +
          api.step_data('gerrit submit change 91827',

+ 7 - 0
recipes/recipe_modules/gerrit/test_api.py

@@ -70,6 +70,13 @@ class GerritTestApi(recipe_test_api.RecipeTestApi):
       "can_delete": True
     })
 
+  def make_gerrit_create_tag_response_data(self):
+    return self._make_gerrit_response_json({
+      "ref": "refs/tags/1.0",
+      "revision": "67ebf73496383c6777035e374d2d664009e2aa5c",
+      "can_delete": True
+    })
+
   def make_gerrit_get_branch_response_data(self):
     return self._make_gerrit_response_json({
       "ref": "refs/heads/main",