[presubmit] Add option to ignore TBR on PRESUBMIT.py checks.

Change-Id: Ibd1a5fa67aca0c273e8289d0726afeaeb88754d9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2568908
Auto-Submit: Edward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
Reviewed-by: Josip Sokcevic <sokcevic@google.com>
diff --git a/presubmit_canned_checks.py b/presubmit_canned_checks.py
index e4d5e63..0ab1c51 100644
--- a/presubmit_canned_checks.py
+++ b/presubmit_canned_checks.py
@@ -1121,7 +1121,8 @@
         'Error parsing OWNERS files:\n%s' % e)]
 
 
-def CheckOwners(input_api, output_api, source_file_filter=None):
+def CheckOwners(
+    input_api, output_api, source_file_filter=None, allow_tbr=True):
   if input_api.change.issue:
     # Skip OWNERS check when Bot-Commit label is approved. This label is
     # intended for commits made by trusted bots that don't require review nor
@@ -1134,6 +1135,9 @@
     if input_api.gerrit.IsOwnersOverrideApproved(input_api.change.issue):
       return []
 
+  # Ignore tbr if not allowed for this repo.
+  tbr = input_api.tbr and allow_tbr
+
   affected_files = set([f.LocalPath() for f in
       input_api.change.AffectedFiles(file_filter=source_file_filter)])
   owners_db = input_api.owners_db
@@ -1159,7 +1163,7 @@
   affects_owners = any('OWNERS' in name for name in missing_files)
 
   if input_api.is_committing:
-    if input_api.tbr and not affects_owners:
+    if tbr and not affects_owners:
       return [output_api.PresubmitNotifyResult(
           '--tbr was specified, skipping OWNERS check')]
     needed = 'LGTM from an OWNER'
@@ -1181,7 +1185,7 @@
     output_list = [
         output_fn('Missing %s for these files:\n    %s' %
                   (needed, '\n    '.join(sorted(missing_files))))]
-    if input_api.tbr and affects_owners:
+    if tbr and affects_owners:
       output_list.append(output_fn('TBR for OWNERS files are ignored.'))
     if not input_api.is_committing:
       suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py
index 7e96590..2bfc809 100755
--- a/tests/presubmit_unittest.py
+++ b/tests/presubmit_unittest.py
@@ -2672,7 +2672,7 @@
       reviewers=None, is_committing=True,
       response=None, uncovered_files=None, expected_output='',
       manually_specified_reviewers=None, dry_run=None,
-      modified_file='foo/xyz.cc'):
+      modified_file='foo/xyz.cc', allow_tbr=True):
     if approvers is None:
       # The set of people who lgtm'ed a change.
       approvers = set()
@@ -2745,7 +2745,7 @@
         fake_db.reviewers_for.return_value = change.author_email
 
     results = presubmit_canned_checks.CheckOwners(
-        input_api, presubmit.OutputApi)
+        input_api, presubmit.OutputApi, allow_tbr=allow_tbr)
     for result in results:
       result.handle()
     if expected_output:
@@ -3049,6 +3049,18 @@
         expected_output='--tbr was specified, skipping OWNERS check\n')
     self.AssertOwnersWorks(tbr=True, is_committing=False, expected_output='')
 
+  def testCannedCheckOwners_TBRIgnored(self):
+    self.AssertOwnersWorks(
+        tbr=True,
+        allow_tbr=False,
+        expected_output='Missing LGTM from someone other than '
+                        'john@example.com\n')
+    self.AssertOwnersWorks(
+        tbr=True,
+        allow_tbr=False,
+        is_committing=False,
+        expected_output='')
+
   def testCannedCheckOwners_TBROWNERSFile(self):
     self.AssertOwnersWorks(
         tbr=True,