From e7a7fc42b6bf58d35ecea2dd5c543f1e22e59880 Mon Sep 17 00:00:00 2001 From: Andrii Shyshkalov Date: Tue, 30 Oct 2018 17:35:09 +0000 Subject: [PATCH] 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 Commit-Queue: Andrii Shyshkalov --- git_cl.py | 17 ++++++++++++++--- tests/git_cl_test.py | 22 +++++++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/git_cl.py b/git_cl.py index a46509c81..8f7f6b77d 100755 --- a/git_cl.py +++ b/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, diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py index 9e5f57e5f..63b9b0c5d 100755 --- a/tests/git_cl_test.py +++ b/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(