From b6ce244503d33dff56ed404fea9228cc0753029e Mon Sep 17 00:00:00 2001 From: Michael Moss Date: Wed, 20 Oct 2021 04:36:24 +0000 Subject: [PATCH] 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 Commit-Queue: Michael Moss Reviewed-by: Xinan Lin Reviewed-by: Dirk Pranke --- gerrit_client.py | 20 ++++++++++++ gerrit_util.py | 17 ++++++++++ recipes/README.recipes.md | 25 +++++++++------ recipes/recipe_modules/gerrit/api.py | 19 ++++++++++++ .../gerrit/examples/full.expected/basic.json | 31 +++++++++++++++++++ .../recipe_modules/gerrit/examples/full.py | 5 +++ recipes/recipe_modules/gerrit/test_api.py | 7 +++++ 7 files changed, 115 insertions(+), 9 deletions(-) diff --git a/gerrit_client.py b/gerrit_client.py index e83a49a98f..07b6c0e513 100755 --- a/gerrit_client.py +++ b/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.""" diff --git a/gerrit_util.py b/gerrit_util.py index 0fec632ad3..6680ee06c9 100644 --- a/gerrit_util.py +++ b/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 diff --git a/recipes/README.recipes.md b/recipes/README.recipes.md index 241319d828..14e691fb7f 100644 --- a/recipes/README.recipes.md +++ b/recipes/README.recipes.md @@ -253,7 +253,7 @@ Module for interact with Gerrit endpoints Wrapper for easy calling of gerrit_utils steps. -— **def [abandon\_change](/recipes/recipe_modules/gerrit/api.py#209)(self, host, change, message=None, name=None, step_test_data=None):** +— **def [abandon\_change](/recipes/recipe_modules/gerrit/api.py#228)(self, host, change, message=None, name=None, step_test_data=None):** — **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 -— **def [get\_change\_description](/recipes/recipe_modules/gerrit/api.py#71)(self, host, change, patchset, timeout=None, step_test_data=None):** +— **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. + +— **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. -— **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):** +— **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 -— **def [get\_gerrit\_branch](/recipes/recipe_modules/gerrit/api.py#53)(self, host, project, branch, \*\*kwargs):** +— **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 -— **def [get\_related\_changes](/recipes/recipe_modules/gerrit/api.py#173)(self, host, change, revision='current', step_test_data=None):** +— **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 -— **def [get\_revision\_info](/recipes/recipe_modules/gerrit/api.py#90)(self, host, change, patchset, timeout=None, step_test_data=None):** +— **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 -— **def [move\_changes](/recipes/recipe_modules/gerrit/api.py#247)(self, host, project, from_branch, to_branch, step_test_data=None):** +— **def [move\_changes](/recipes/recipe_modules/gerrit/api.py#266)(self, host, project, from_branch, to_branch, step_test_data=None):** -— **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 [set\_change\_label](/recipes/recipe_modules/gerrit/api.py#248)(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):** +— **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. diff --git a/recipes/recipe_modules/gerrit/api.py b/recipes/recipe_modules/gerrit/api.py index 790c8eb5eb..be4dac2aa2 100644 --- a/recipes/recipe_modules/gerrit/api.py +++ b/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): diff --git a/recipes/recipe_modules/gerrit/examples/full.expected/basic.json b/recipes/recipe_modules/gerrit/examples/full.expected/basic.json index 95e55f7ab3..59c664f7be 100644 --- a/recipes/recipe_modules/gerrit/examples/full.expected/basic.json +++ b/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": ":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", diff --git a/recipes/recipe_modules/gerrit/examples/full.py b/recipes/recipe_modules/gerrit/examples/full.py index 1a413766b2..b063cbcdea 100644 --- a/recipes/recipe_modules/gerrit/examples/full.py +++ b/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', diff --git a/recipes/recipe_modules/gerrit/test_api.py b/recipes/recipe_modules/gerrit/test_api.py index fc11a86dec..08a2e929a7 100644 --- a/recipes/recipe_modules/gerrit/test_api.py +++ b/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",