Use API to set TBR code-review

This prevents TBR (self code-review) permissions from blocking
the CL from being uploaded at all. Instead, it will fully
upload, and then show a better error message after upload is
complete.

Bug: 729967
Change-Id: I55e3e98e200143076afcaab858064d9f5c62f8ef
Reviewed-on: https://chromium-review.googlesource.com/527325
Reviewed-by: Andrii Shyshkalov <tandrii@chromium.org>
Commit-Queue: Aaron Gable <agable@chromium.org>
diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py
index b966c06..e1845c8 100755
--- a/tests/git_cl_test.py
+++ b/tests/git_cl_test.py
@@ -599,6 +599,9 @@
     self.mock(git_cl.gerrit_util, 'AddReviewers',
               lambda h, i, reviewers, ccs, notify: self._mocked_call(
                   'AddReviewers', h, i, reviewers, ccs, notify))
+    self.mock(git_cl.gerrit_util, 'SetReview',
+              lambda h, i, labels, notify: self._mocked_call(
+                  'SetReview', h, i, labels, notify))
     self.mock(git_cl.gerrit_util.GceAuthenticator, 'is_gce',
               classmethod(lambda _: False))
     self.mock(git_cl, 'DieWithError',
@@ -1414,10 +1417,9 @@
   def _gerrit_upload_calls(cls, description, reviewers, squash,
                            squash_mode='default',
                            expected_upstream_ref='origin/refs/heads/master',
-                           ref_suffix='', title=None, notify=False,
+                           title=None, notify=False,
                            post_amend_description=None, issue=None, cc=None,
-                           git_mirror=None,
-                           custom_cl_base=None):
+                           git_mirror=None, custom_cl_base=None, tbr=None):
     if post_amend_description is None:
       post_amend_description = description
     cc = cc or []
@@ -1517,11 +1519,9 @@
       '1hashPerLine\n'),
     ]
 
+    ref_suffix = ''
     if title:
-      if ref_suffix:
-        ref_suffix += ',m=' + title
-      else:
-        ref_suffix = '%m=' + title
+      ref_suffix += '%m=' + title
 
     notify_suffix = 'notify=NONE'
     if ref_suffix:
@@ -1578,6 +1578,11 @@
            123456 if squash else None, sorted(reviewers),
           ['joe@example.com'] + cc, notify), ''),
     ]
+    if tbr:
+      calls += [
+        (('SetReview', 'chromium-review.googlesource.com',
+          123456 if squash else None, {'Code-Review': 1}, notify), ''),
+      ]
     calls += cls._git_post_upload_calls()
     return calls
 
@@ -1589,7 +1594,6 @@
       squash=True,
       squash_mode=None,
       expected_upstream_ref='origin/refs/heads/master',
-      ref_suffix='',
       title=None,
       notify=False,
       post_amend_description=None,
@@ -1598,7 +1602,8 @@
       git_mirror=None,
       fetched_status=None,
       other_cl_owner=None,
-      custom_cl_base=None):
+      custom_cl_base=None,
+      tbr=None):
     """Generic gerrit upload test framework."""
     if squash_mode is None:
       if '--no-squash' in upload_args:
@@ -1639,10 +1644,10 @@
           description, reviewers, squash,
           squash_mode=squash_mode,
           expected_upstream_ref=expected_upstream_ref,
-          ref_suffix=ref_suffix, title=title, notify=notify,
+          title=title, notify=notify,
           post_amend_description=post_amend_description,
           issue=issue, cc=cc, git_mirror=git_mirror,
-          custom_cl_base=custom_cl_base)
+          custom_cl_base=custom_cl_base, tbr=tbr)
     # Uncomment when debugging.
     # print '\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls)))
     git_cl.main(['upload'] + upload_args)
@@ -1704,8 +1709,8 @@
         ['reviewer@example.com', 'another@example.com'],
         squash=False,
         squash_mode='override_nosquash',
-        ref_suffix='%l=Code-Review+1',
-        cc=['more@example.com', 'people@example.com'])
+        cc=['more@example.com', 'people@example.com'],
+        tbr='reviewer@example.com')
 
   def test_gerrit_upload_squash_first_is_default(self):
     self._run_gerrit_upload_test(