Commit df9a802a authored by Josip Sokcevic's avatar Josip Sokcevic Committed by LUCI CQ

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/+/2575884Reviewed-by: 's avatarGavin Mak <gavinmak@google.com>
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
parent 8bbab2d8
...@@ -97,6 +97,35 @@ def CMDbranch(parser, args): ...@@ -97,6 +97,35 @@ def CMDbranch(parser, args):
write_result(result, opt) 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 ...]') @subcommand.usage('[args ...]')
def CMDchanges(parser, args): def CMDchanges(parser, args):
parser.add_option('-p', '--param', dest='params', action='append', parser.add_option('-p', '--param', dest='params', action='append',
......
...@@ -75,6 +75,9 @@ class GerritError(Exception): ...@@ -75,6 +75,9 @@ class GerritError(Exception):
self.http_status = http_status self.http_status = http_status
self.message = '(%d) %s' % (self.http_status, message) self.message = '(%d) %s' % (self.http_status, message)
def __str__(self):
return self.message
def _QueryString(params, first_param=None): def _QueryString(params, first_param=None):
"""Encodes query parameters in the key:val[+key:val...] format specified here: """Encodes query parameters in the key:val[+key:val...] format specified here:
...@@ -909,6 +912,39 @@ def CreateGerritBranch(host, project, branch, commit): ...@@ -909,6 +912,39 @@ def CreateGerritBranch(host, project, branch, commit):
raise GerritError(200, 'Unable to create gerrit branch') 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): def GetGerritBranch(host, project, branch):
"""Gets a branch from given project and commit. """Gets a branch from given project and commit.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment