[git-cl] allow setting the bug during force mode

Bug: 1001709
Change-Id: I53e7699cb38d810d0a8c88d40907bf249174ad8b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1804603
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Anthony Polito <apolito@google.com>
diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py
index 82a06fc..f1cdf4d 100755
--- a/tests/git_cl_test.py
+++ b/tests/git_cl_test.py
@@ -822,7 +822,8 @@
   @classmethod
   def _gerrit_base_calls(cls, issue=None, fetched_description=None,
                          fetched_status=None, other_cl_owner=None,
-                         custom_cl_base=None, short_hostname='chromium'):
+                         custom_cl_base=None, short_hostname='chromium',
+                         change_id=None):
     calls = cls._is_gerrit_calls(True)
     if not custom_cl_base:
       calls += [
@@ -854,7 +855,7 @@
          ),
          {
            'owner': {'email': (other_cl_owner or 'owner@example.com')},
-           'change_id': '123456789',
+           'change_id': (change_id or '123456789'),
            'current_revision': 'sha1_of_current_revision',
            'revisions': {'sha1_of_current_revision': {
              'commit': {'message': fetched_description},
@@ -918,7 +919,8 @@
                            custom_cl_base=None, tbr=None,
                            short_hostname='chromium',
                            labels=None, change_id=None, original_title=None,
-                           final_description=None, gitcookies_exists=True):
+                           final_description=None, gitcookies_exists=True,
+                           force=False):
     if post_amend_description is None:
       post_amend_description = description
     cc = cc or []
@@ -970,13 +972,20 @@
          post_amend_description)
       ]
     if squash:
-      if not issue:
+      if force or not issue:
+        if issue:
+          calls += [
+            ((['git', 'config', 'rietveld.bug-prefix'],), ''),
+          ]
         # Prompting to edit description on first upload.
         calls += [
           ((['git', 'config', 'rietveld.bug-prefix'],), ''),
-          ((['git', 'config', 'core.editor'],), ''),
-          ((['RunEditor'],), description),
         ]
+        if not force:
+          calls += [
+            ((['git', 'config', 'core.editor'],), ''),
+            ((['RunEditor'],), description),
+          ]
       ref_to_push = 'abcdef0123456789'
       calls += [
         ((['git', 'config', 'branch.master.merge'],), 'refs/heads/master'),
@@ -1228,7 +1237,9 @@
       change_id=None,
       original_title=None,
       final_description=None,
-      gitcookies_exists=True):
+      gitcookies_exists=True,
+      force=False,
+      fetched_description=None):
     """Generic gerrit upload test framework."""
     if squash_mode is None:
       if '--no-squash' in upload_args:
@@ -1275,11 +1286,12 @@
 
     self.calls = self._gerrit_base_calls(
         issue=issue,
-        fetched_description=description,
+        fetched_description=fetched_description or description,
         fetched_status=fetched_status,
         other_cl_owner=other_cl_owner,
         custom_cl_base=custom_cl_base,
-        short_hostname=short_hostname)
+        short_hostname=short_hostname,
+        change_id=change_id)
     if fetched_status != 'ABANDONED':
       self.mock(tempfile, 'NamedTemporaryFile', MakeNamedTemporaryFileMock(
           expected_content=description))
@@ -1297,7 +1309,8 @@
           change_id=change_id,
           original_title=original_title,
           final_description=final_description,
-          gitcookies_exists=gitcookies_exists)
+          gitcookies_exists=gitcookies_exists,
+          force=force)
     # Uncomment when debugging.
     # print('\n'.join(map(lambda x: '%2i: %s' % x, enumerate(self.calls))))
     git_cl.main(['upload'] + upload_args)
@@ -1374,6 +1387,30 @@
         final_description=(
             'desc\n\nBUG=\nR=foo@example.com\n\nChange-Id: I123456789'))
 
+  def test_gerrit_upload_force_sets_bug(self):
+    self._run_gerrit_upload_test(
+        ['-b', '10000', '-f'],
+        u'desc=\n\nBug: 10000\nChange-Id: Ixxx',
+        [],
+        force=True,
+        expected_upstream_ref='origin/master',
+        fetched_description='desc=\n\nChange-Id: Ixxx',
+        original_title='Initial upload',
+        change_id='Ixxx')
+
+  def test_gerrit_upload_force_sets_bug_if_wrong_changeid(self):
+    self._run_gerrit_upload_test(
+        ['-b', '10000', '-f', '-m', 'Title'],
+        u'desc=\n\nChange-Id: Ixxxx\n\nChange-Id: Izzzz\nBug: 10000',
+        [],
+        force=True,
+        issue='123456',
+        expected_upstream_ref='origin/master',
+        fetched_description='desc=\n\nChange-Id: Ixxxx',
+        original_title='Title',
+        title='Title',
+        change_id='Izzzz')
+
   def test_gerrit_reviewer_multiple(self):
     self.mock(git_cl.gerrit_util, 'GetCodeReviewTbrScore',
               lambda *a: self._mocked_call('GetCodeReviewTbrScore', *a))