Browse Source

git cl upload: set CQ and Auto Submit labels through refspec.

Except when TBR is active, because it's not clear how to
compute max score for Code-Review label before uploading a change.

Tested with this very own CL:
  To https://chromium.googlesource.com/chromium/tools/depot_tools.git
   * [new branch]        c78de165c7be156bdf8032c0ce3d8bad42778f1c ->
     refs/for/refs/heads/master%ready,notify=ALL,m=Initial_upload,r=ehmaldonado,
         cc=ajp,cc=chromium-reviews@chromium.org,cc=iannucci+depot_tools@chromium.org,
         l=Commit-Queue+1,hashtag=git-cl-upload
R=ehmaldonado

Bug: 877717
Change-Id: I464d9df2814bd830dd608bb881040e2dd3c41dbb
Reviewed-on: https://chromium-review.googlesource.com/c/1307052
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Andrii Shyshkalov <tandrii@chromium.org>
Andrii Shyshkalov 6 years ago
parent
commit
e7a7fc42b6
2 changed files with 33 additions and 6 deletions
  1. 14 3
      git_cl.py
  2. 19 3
      tests/git_cl_test.py

+ 14 - 3
git_cl.py

@@ -2927,6 +2927,17 @@ class _GerritChangelistImpl(_ChangelistCodereviewBase):
       # https://gerrit-review.googlesource.com/Documentation/user-upload.html#topic
       refspec_opts.append('topic=%s' % options.topic)
 
+    if not change_desc.get_reviewers(tbr_only=True):
+      # Change is not TBR, so we can inline setting other labels, too.
+      # TODO(crbug.com/877717): make this working for TBR, too, by figuring out
+      # max score for CR label somehow.
+      if options.enable_auto_submit:
+        refspec_opts.append('l=Auto-Submit+1')
+      if options.use_commit_queue:
+        refspec_opts.append('l=Commit-Queue+2')
+      elif options.cq_dry_run:
+        refspec_opts.append('l=Commit-Queue+1')
+
     # Gerrit sorts hashtags, so order is not important.
     hashtags = {change_desc.sanitize_hash_tag(t) for t in options.hashtags}
     if not self.GetIssue():
@@ -3042,9 +3053,9 @@ class _GerritChangelistImpl(_ChangelistCodereviewBase):
           self._GerritChangeIdentifier(),
           msg='Self-approving for TBR',
           labels={'Code-Review': score})
-
-    self.SetLabels(options.enable_auto_submit, options.use_commit_queue,
-                   options.cq_dry_run)
+      # Labels aren't set through refspec only if tbr is set (see check above).
+      self.SetLabels(options.enable_auto_submit, options.use_commit_queue,
+                     options.cq_dry_run)
     return 0
 
   def _ComputeParent(self, remote, upstream_branch, custom_cl_base, force,

+ 19 - 3
tests/git_cl_test.py

@@ -905,7 +905,8 @@ class TestGitCl(TestCase):
                            title=None, notify=False,
                            post_amend_description=None, issue=None, cc=None,
                            custom_cl_base=None, tbr=None,
-                           short_hostname='chromium'):
+                           short_hostname='chromium',
+                           labels=None):
     if post_amend_description is None:
       post_amend_description = description
     cc = cc or []
@@ -1048,6 +1049,10 @@ class TestGitCl(TestCase):
         if c in cc:
           cc.remove(c)
 
+    if not tbr:
+      for k, v in sorted((labels or {}).items()):
+        ref_suffix += ',l=%s+%d' % (k, v)
+
     calls.append((
       (['git', 'push',
         'https://%s.googlesource.com/my/repo' % short_hostname,
@@ -1130,7 +1135,8 @@ class TestGitCl(TestCase):
       other_cl_owner=None,
       custom_cl_base=None,
       tbr=None,
-      short_hostname='chromium'):
+      short_hostname='chromium',
+      labels=None):
     """Generic gerrit upload test framework."""
     if squash_mode is None:
       if '--no-squash' in upload_args:
@@ -1172,7 +1178,8 @@ class TestGitCl(TestCase):
           post_amend_description=post_amend_description,
           issue=issue, cc=cc,
           custom_cl_base=custom_cl_base, tbr=tbr,
-          short_hostname=short_hostname)
+          short_hostname=short_hostname,
+          labels=labels)
     # Uncomment when debugging.
     # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls)))
     git_cl.main(['upload'] + upload_args)
@@ -1256,6 +1263,15 @@ class TestGitCl(TestCase):
         squash=True,
         expected_upstream_ref='origin/master')
 
+  def test_gerrit_upload_squash_first_with_labels(self):
+    self._run_gerrit_upload_test(
+        ['--squash', '--cq-dry-run', '--enable-auto-submit'],
+        'desc\nBUG=\n\nChange-Id: 123456789',
+        [],
+        squash=True,
+        expected_upstream_ref='origin/master',
+        labels={'Commit-Queue': 1, 'Auto-Submit': 1})
+
   def test_gerrit_upload_squash_first_against_rev(self):
     custom_cl_base = 'custom_cl_base_rev_or_branch'
     self._run_gerrit_upload_test(