From df9a802ad2711b10ea128b71312653ef6f209444 Mon Sep 17 00:00:00 2001 From: Josip Sokcevic Date: Tue, 8 Dec 2020 00:10:19 +0000 Subject: [PATCH] Add change HEAD cli option to gerrit_client This option modifies default branch of desired repository. R=ehmaldonaldo@chromium.org, gavinmak@google.com Change-Id: I1e20dc8d333c4301eacffff5049e3a98c3d59f75 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2575884 Reviewed-by: Gavin Mak Commit-Queue: Josip Sokcevic --- gerrit_client.py | 29 +++++++++++++++++++++++++++++ gerrit_util.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/gerrit_client.py b/gerrit_client.py index 1a333a5b9..6a174b384 100755 --- a/gerrit_client.py +++ b/gerrit_client.py @@ -97,6 +97,35 @@ def CMDbranch(parser, args): write_result(result, opt) +@subcommand.usage('[args ...]') +def CMDhead(parser, args): + parser.add_option('--branch', dest='branch', help='branch name') + + (opt, args) = parser.parse_args(args) + assert opt.project, "--project not defined" + assert opt.branch, "--branch not defined" + + project = quote_plus(opt.project) + host = urlparse.urlparse(opt.host).netloc + branch = quote_plus(opt.branch) + result = gerrit_util.UpdateHead(host, project, branch) + logging.info(result) + write_result(result, opt) + + +@subcommand.usage('[args ...]') +def CMDheadinfo(parser, args): + + (opt, args) = parser.parse_args(args) + assert opt.project, "--project not defined" + + project = quote_plus(opt.project) + host = urlparse.urlparse(opt.host).netloc + result = gerrit_util.GetHead(host, project) + logging.info(result) + write_result(result, opt) + + @subcommand.usage('[args ...]') def CMDchanges(parser, args): parser.add_option('-p', '--param', dest='params', action='append', diff --git a/gerrit_util.py b/gerrit_util.py index aa8a39617..6312b1622 100644 --- a/gerrit_util.py +++ b/gerrit_util.py @@ -75,6 +75,9 @@ class GerritError(Exception): self.http_status = http_status self.message = '(%d) %s' % (self.http_status, message) + def __str__(self): + return self.message + def _QueryString(params, first_param=None): """Encodes query parameters in the key:val[+key:val...] format specified here: @@ -909,6 +912,39 @@ def CreateGerritBranch(host, project, branch, commit): raise GerritError(200, 'Unable to create gerrit branch') +def GetHead(host, project): + """Retrieves current HEAD of Gerrit project + + https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-head + + Returns: + A JSON object with 'ref' key. + """ + path = 'projects/%s/HEAD' % (project) + conn = CreateHttpConn(host, path, reqtype='GET') + response = ReadHttpJsonResponse(conn, accept_statuses=[200]) + if response: + return response + raise GerritError(200, 'Unable to update gerrit HEAD') + + +def UpdateHead(host, project, branch): + """Updates Gerrit HEAD to point to branch + + https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#set-head + + Returns: + A JSON object with 'ref' key. + """ + path = 'projects/%s/HEAD' % (project) + body = {'ref': branch} + conn = CreateHttpConn(host, path, reqtype='PUT', body=body) + response = ReadHttpJsonResponse(conn, accept_statuses=[200]) + if response: + return response + raise GerritError(200, 'Unable to update gerrit HEAD') + + def GetGerritBranch(host, project, branch): """Gets a branch from given project and commit.