Эх сурвалжийг харах

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 <gavinmak@google.com>
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
Josip Sokcevic 4 жил өмнө
parent
commit
df9a802ad2
2 өөрчлөгдсөн 65 нэмэгдсэн , 0 устгасан
  1. 29 0
      gerrit_client.py
  2. 36 0
      gerrit_util.py

+ 29 - 0
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',

+ 36 - 0
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.