Add commit_queue.py tool to toggle the bit of the commit queue from command line

Add "git cl set_commit" command to set the flag more easily.
Add --commit to "git cl upload" to streamline workflow even more.
Continue conversion to Rietveld object.

R=dpranke@chromium.org
BUG=
TEST=

Review URL: http://codereview.chromium.org/7084037

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@87253 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/git_cl.py b/git_cl.py
index 5cf2580..1cd591d 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -463,9 +463,8 @@
   def GetDescription(self, pretty=False):
     if not self.has_description:
       if self.GetIssue():
-        path = '/' + self.GetIssue() + '/description'
-        rpc_server = self.RpcServer()
-        self.description = rpc_server.Send(path).strip()
+        self.description = self.RpcServer().get_description(
+            int(self.GetIssue())).strip()
       self.has_description = True
     if pretty:
       wrapper = textwrap.TextWrapper()
@@ -494,10 +493,9 @@
     self.has_patchset = False
 
   def GetPatchSetDiff(self, issue):
-    # Grab the last patchset of the issue first.
-    data = json.loads(self.RpcServer().Send('/api/%s' % issue))
-    patchset = data['patchsets'][-1]
-    return self.RpcServer().Send(
+    patchset = self.RpcServer().get_issue_properties(
+        int(issue), False)['patchsets'][-1]
+    return self.RpcServer().get(
         '/download/issue%s_%s.diff' % (issue, patchset))
 
   def SetIssue(self, issue):
@@ -564,20 +562,23 @@
     return output
 
   def CloseIssue(self):
-    rpc_server = self.RpcServer()
-    # Newer versions of Rietveld require us to pass an XSRF token to POST, so
-    # we fetch it from the server.  (The version used by Chromium has been
-    # modified so the token isn't required when closing an issue.)
-    xsrf_token = rpc_server.Send('/xsrf_token',
-                                 extra_headers={'X-Requesting-XSRF-Token': '1'})
+    return self.RpcServer().close_issue(int(self.GetIssue()))
 
-    # You cannot close an issue with a GET.
-    # We pass an empty string for the data so it is a POST rather than a GET.
-    data = [("description", self.description),
-            ("xsrf_token", xsrf_token)]
-    ctype, body = upload.EncodeMultipartFormData(data, [])
-    rpc_server.Send(
-        '/' + self.GetIssue() + '/close', payload=body, content_type=ctype)
+  def SetFlag(self, flag, value):
+    """Patchset must match."""
+    if not self.GetPatchset():
+      DieWithError('The patchset needs to match. Send another patchset.')
+    try:
+      return self.RpcServer().set_flag(
+          int(self.GetIssue()), int(self.GetPatchset()), flag, value)
+    except urllib2.HTTPError, e:
+      if e.code == 404:
+        DieWithError('The issue %s doesn\'t exist.' % self.GetIssue())
+      if e.code == 403:
+        DieWithError(
+            ('Access denied to issue %s. Maybe the patchset %s doesn\'t '
+             'match?') % (self.GetIssue(), self.GetPatchset()))
+      raise
 
   def RpcServer(self):
     """Returns an upload.RpcServer() to access this review's rietveld instance.
@@ -913,6 +914,8 @@
                     dest="from_logs",
                     help="""Squashes git commit logs into change description and
                             uses message as subject""")
+  parser.add_option('-c', '--use-commit-queue', action='store_true',
+                    help='tell the commit queue to commit this patchset')
   (options, args) = parser.parse_args(args)
 
   # Make sure index is up-to-date before running diff-index.
@@ -1021,6 +1024,9 @@
   if not cl.GetIssue():
     cl.SetIssue(issue)
   cl.SetPatchset(patchset)
+
+  if options.use_commit_queue:
+    cl.SetFlag('commit', '1')
   return 0
 
 
@@ -1265,6 +1271,8 @@
     return 1
   issue_arg = args[0]
 
+  # TODO(maruel): Use apply_issue.py
+
   if re.match(r'\d+', issue_arg):
     # Input is an issue id.  Figure out the URL.
     issue = issue_arg
@@ -1378,11 +1386,23 @@
 def CMDupstream(parser, args):
   """print the name of the upstream branch, if any"""
   _, args = parser.parse_args(args)
+  if args:
+    parser.error('Unrecognized args: %s' % ' '.join(args))
   cl = Changelist()
   print cl.GetUpstreamBranch()
   return 0
 
 
+def CMDset_commit(parser, args):
+  """set the commit bit"""
+  _, args = parser.parse_args(args)
+  if args:
+    parser.error('Unrecognized args: %s' % ' '.join(args))
+  cl = Changelist()
+  cl.SetFlag('commit', '1')
+  return 0
+
+
 def Command(name):
   return getattr(sys.modules[__name__], 'CMD' + name, None)