blob: 399f583e92d1f3d03856deb1797cb4d328584d8e [file] [log] [blame]
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +00001# Copyright (c) 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5
6"""Top-level presubmit script for Skia.
7
8See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
9for more details about the presubmit API built into gcl.
10"""
11
rmistry58276532015-10-01 08:24:03 -070012import collections
rmistry3cfd1ad2015-03-25 12:53:35 -070013import csv
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +000014import fnmatch
rmistry@google.comf6c5f752013-03-29 17:26:00 +000015import os
commit-bot@chromium.orgcfdc5962014-01-31 17:33:04 +000016import re
rmistryd223fb22015-02-26 10:16:13 -080017import subprocess
rmistry@google.comf6c5f752013-03-29 17:26:00 +000018import sys
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +000019import traceback
rmistry@google.comf6c5f752013-03-29 17:26:00 +000020
rmistry@google.comc2993442013-01-23 14:35:58 +000021
commit-bot@chromium.orgcfdc5962014-01-31 17:33:04 +000022REVERT_CL_SUBJECT_PREFIX = 'Revert '
23
rmistryf2d83ca2014-08-26 10:30:29 -070024# Please add the complete email address here (and not just 'xyz@' or 'xyz').
rmistry@google.comfb4a68d2013-08-12 14:51:20 +000025PUBLIC_API_OWNERS = (
Mike Reed7dafb092019-04-02 10:16:42 -040026 'mtklein@google.com',
rmistry@google.comfb4a68d2013-08-12 14:51:20 +000027 'reed@chromium.org',
28 'reed@google.com',
29 'bsalomon@chromium.org',
30 'bsalomon@google.com',
rmistry83fab472014-07-18 05:25:56 -070031 'djsollen@chromium.org',
32 'djsollen@google.com',
Ravi Mistryfbff3292017-01-19 12:00:08 -050033 'hcm@chromium.org',
34 'hcm@google.com',
rmistry@google.comfb4a68d2013-08-12 14:51:20 +000035)
36
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +000037AUTHORS_FILE_NAME = 'AUTHORS'
Ravi Mistry57735162019-07-25 13:45:15 -040038RELEASE_NOTES_FILE_NAME = 'RELEASE_NOTES.txt'
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +000039
Edward Lemur2b7876c2020-01-17 18:48:13 -050040DOCS_PREVIEW_URL = 'https://skia.org/?cl={issue}'
rmistryd88b0be2016-05-20 03:50:01 -070041GOLD_TRYBOT_URL = 'https://gold.skia.org/search?issue='
rmistryd223fb22015-02-26 10:16:13 -080042
Eric Boren1eec99c2018-04-26 13:09:48 -040043SERVICE_ACCOUNT_SUFFIX = [
Eric Boren47ed6f12018-04-26 14:02:43 -040044 '@%s.iam.gserviceaccount.com' % project for project in [
Eric Boren6ad3ca42018-09-07 14:22:16 -040045 'skia-buildbots.google.com', 'skia-swarming-bots', 'skia-public',
Ravi Mistry53c44232019-03-12 08:51:42 -040046 'skia-corp.google.com', 'chops-service-accounts']]
Eric Borendd988292018-01-02 13:29:21 -050047
rmistry@google.com547012d2013-04-12 19:45:46 +000048
rmistry@google.com713276b2013-01-25 18:27:34 +000049def _CheckChangeHasEol(input_api, output_api, source_file_filter=None):
Edward Lemur2b7876c2020-01-17 18:48:13 -050050 """Checks that files end with at least one \n (LF)."""
rmistry@google.com713276b2013-01-25 18:27:34 +000051 eof_files = []
52 for f in input_api.AffectedSourceFiles(source_file_filter):
53 contents = input_api.ReadFile(f, 'rb')
Edward Lemur2b7876c2020-01-17 18:48:13 -050054 # Check that the file ends in at least one newline character.
rmistry@google.com713276b2013-01-25 18:27:34 +000055 if len(contents) > 1 and contents[-1:] != '\n':
56 eof_files.append(f.LocalPath())
57
58 if eof_files:
59 return [output_api.PresubmitPromptWarning(
60 'These files should end in a newline character:',
61 items=eof_files)]
62 return []
63
64
Ben Wagnercf42e982018-02-09 17:41:20 -050065def _JsonChecks(input_api, output_api):
66 """Run checks on any modified json files."""
67 failing_files = []
68 for affected_file in input_api.AffectedFiles(None):
69 affected_file_path = affected_file.LocalPath()
70 is_json = affected_file_path.endswith('.json')
71 is_metadata = (affected_file_path.startswith('site/') and
72 affected_file_path.endswith('/METADATA'))
73 if is_json or is_metadata:
74 try:
75 input_api.json.load(open(affected_file_path, 'r'))
76 except ValueError:
77 failing_files.append(affected_file_path)
78
79 results = []
80 if failing_files:
81 results.append(
82 output_api.PresubmitError(
83 'The following files contain invalid json:\n%s\n\n' %
84 '\n'.join(failing_files)))
85 return results
86
87
rmistry01cbf6c2015-03-12 07:48:40 -070088def _IfDefChecks(input_api, output_api):
89 """Ensures if/ifdef are not before includes. See skbug/3362 for details."""
90 comment_block_start_pattern = re.compile('^\s*\/\*.*$')
91 comment_block_middle_pattern = re.compile('^\s+\*.*')
92 comment_block_end_pattern = re.compile('^\s+\*\/.*$')
93 single_line_comment_pattern = re.compile('^\s*//.*$')
94 def is_comment(line):
95 return (comment_block_start_pattern.match(line) or
96 comment_block_middle_pattern.match(line) or
97 comment_block_end_pattern.match(line) or
98 single_line_comment_pattern.match(line))
99
100 empty_line_pattern = re.compile('^\s*$')
101 def is_empty_line(line):
102 return empty_line_pattern.match(line)
103
104 failing_files = []
105 for affected_file in input_api.AffectedSourceFiles(None):
106 affected_file_path = affected_file.LocalPath()
107 if affected_file_path.endswith('.cpp') or affected_file_path.endswith('.h'):
108 f = open(affected_file_path)
109 for line in f.xreadlines():
110 if is_comment(line) or is_empty_line(line):
111 continue
112 # The below will be the first real line after comments and newlines.
113 if line.startswith('#if 0 '):
114 pass
115 elif line.startswith('#if ') or line.startswith('#ifdef '):
116 failing_files.append(affected_file_path)
117 break
118
119 results = []
120 if failing_files:
121 results.append(
122 output_api.PresubmitError(
123 'The following files have #if or #ifdef before includes:\n%s\n\n'
halcanary6950de62015-11-07 05:29:00 -0800124 'See https://bug.skia.org/3362 for why this should be fixed.' %
rmistry01cbf6c2015-03-12 07:48:40 -0700125 '\n'.join(failing_files)))
126 return results
127
128
borenetc7c91802015-03-25 04:47:02 -0700129def _CopyrightChecks(input_api, output_api, source_file_filter=None):
130 results = []
131 year_pattern = r'\d{4}'
132 year_range_pattern = r'%s(-%s)?' % (year_pattern, year_pattern)
133 years_pattern = r'%s(,%s)*,?' % (year_range_pattern, year_range_pattern)
134 copyright_pattern = (
135 r'Copyright (\([cC]\) )?%s \w+' % years_pattern)
136
137 for affected_file in input_api.AffectedSourceFiles(source_file_filter):
John Stilesd836f842020-09-14 10:21:44 -0400138 if ('third_party/' in affected_file.LocalPath() or
139 'tests/sksl/' in affected_file.LocalPath()):
borenetc7c91802015-03-25 04:47:02 -0700140 continue
141 contents = input_api.ReadFile(affected_file, 'rb')
142 if not re.search(copyright_pattern, contents):
143 results.append(output_api.PresubmitError(
144 '%s is missing a correct copyright header.' % affected_file))
145 return results
146
147
borenet2dbbfa52016-10-14 06:32:09 -0700148def _InfraTests(input_api, output_api):
149 """Run the infra tests."""
borenet1ed2ae42016-07-26 11:52:17 -0700150 results = []
mtklein3da80f52016-07-27 04:14:07 -0700151 if not any(f.LocalPath().startswith('infra')
152 for f in input_api.AffectedFiles()):
153 return results
154
borenet2dbbfa52016-10-14 06:32:09 -0700155 cmd = ['python', os.path.join('infra', 'bots', 'infra_tests.py')]
borenet60b0a2d2016-10-04 12:45:41 -0700156 try:
157 subprocess.check_output(cmd)
158 except subprocess.CalledProcessError as e:
159 results.append(output_api.PresubmitError(
160 '`%s` failed:\n%s' % (' '.join(cmd), e.output)))
161 return results
162
163
mtklein4db3b792016-08-03 14:18:22 -0700164def _CheckGNFormatted(input_api, output_api):
165 """Make sure any .gn files we're changing have been formatted."""
Ben Wagner3c4a9d32020-02-14 14:28:33 -0500166 files = []
Corentin Wallez6a5187a2020-04-08 10:24:04 +0200167 for f in input_api.AffectedFiles(include_deletes=False):
Ben Wagner3c4a9d32020-02-14 14:28:33 -0500168 if (f.LocalPath().endswith('.gn') or
169 f.LocalPath().endswith('.gni')):
170 files.append(f)
171 if not files:
172 return []
mtklein4db3b792016-08-03 14:18:22 -0700173
Ben Wagner3c4a9d32020-02-14 14:28:33 -0500174 cmd = ['python', os.path.join('bin', 'fetch-gn')]
175 try:
176 subprocess.check_output(cmd)
177 except subprocess.CalledProcessError as e:
178 return [output_api.PresubmitError(
179 '`%s` failed:\n%s' % (' '.join(cmd), e.output))]
180
181 results = []
182 for f in files:
Brian Osman70f24af2020-02-18 15:08:27 -0500183 gn = 'gn.exe' if 'win32' in sys.platform else 'gn'
Ben Wagner06265e02020-02-13 19:02:46 -0500184 gn = os.path.join(input_api.PresubmitLocalPath(), 'bin', gn)
Mike Klein7a1c53d2016-10-11 14:03:06 -0400185 cmd = [gn, 'format', '--dry-run', f.LocalPath()]
mtklein4db3b792016-08-03 14:18:22 -0700186 try:
187 subprocess.check_output(cmd)
188 except subprocess.CalledProcessError:
Ben Wagner06265e02020-02-13 19:02:46 -0500189 fix = 'bin/gn format ' + f.LocalPath()
mtklein4db3b792016-08-03 14:18:22 -0700190 results.append(output_api.PresubmitError(
mtkleind434b012016-08-10 07:30:58 -0700191 '`%s` failed, try\n\t%s' % (' '.join(cmd), fix)))
mtklein4db3b792016-08-03 14:18:22 -0700192 return results
193
Mike Kleinbb413432019-07-26 11:55:40 -0500194def _CheckIncludesFormatted(input_api, output_api):
195 """Make sure #includes in files we're changing have been formatted."""
Mike Kleinf9ad5ba2019-07-29 12:34:39 -0500196 files = [str(f) for f in input_api.AffectedFiles() if f.Action() != 'D']
Mike Kleinbb413432019-07-26 11:55:40 -0500197 cmd = ['python',
198 'tools/rewrite_includes.py',
Mike Kleinf9ad5ba2019-07-29 12:34:39 -0500199 '--dry-run'] + files
Hal Canary4df3d532019-07-30 13:49:45 -0400200 if 0 != subprocess.call(cmd):
Mike Kleinbb413432019-07-26 11:55:40 -0500201 return [output_api.PresubmitError('`%s` failed' % ' '.join(cmd))]
202 return []
borenet1ed2ae42016-07-26 11:52:17 -0700203
Eric Boren58d1f762019-07-19 08:07:44 -0400204def _CheckCompileIsolate(input_api, output_api):
205 """Ensure that gen_compile_isolate.py does not change compile.isolate."""
206 # Only run the check if files were added or removed.
207 results = []
208 script = os.path.join('infra', 'bots', 'gen_compile_isolate.py')
209 isolate = os.path.join('infra', 'bots', 'compile.isolated')
210 for f in input_api.AffectedFiles():
211 if f.Action() in ('A', 'D', 'R'):
212 break
213 if f.LocalPath() in (script, isolate):
214 break
215 else:
216 return results
217
218 cmd = ['python', script, 'test']
219 try:
220 subprocess.check_output(cmd, stderr=subprocess.STDOUT)
221 except subprocess.CalledProcessError as e:
222 results.append(output_api.PresubmitError(e.output))
223 return results
224
225
Ben Wagner88855502017-10-12 17:55:19 -0400226class _WarningsAsErrors():
227 def __init__(self, output_api):
228 self.output_api = output_api
229 self.old_warning = None
230 def __enter__(self):
231 self.old_warning = self.output_api.PresubmitPromptWarning
232 self.output_api.PresubmitPromptWarning = self.output_api.PresubmitError
233 return self.output_api
234 def __exit__(self, ex_type, ex_value, ex_traceback):
235 self.output_api.PresubmitPromptWarning = self.old_warning
236
237
Eric Boren6dc00212019-07-24 15:15:43 -0400238def _CheckDEPSValid(input_api, output_api):
239 """Ensure that DEPS contains valid entries."""
240 results = []
241 script = os.path.join('infra', 'bots', 'check_deps.py')
242 relevant_files = ('DEPS', script)
243 for f in input_api.AffectedFiles():
244 if f.LocalPath() in relevant_files:
245 break
246 else:
247 return results
248 cmd = ['python', script]
249 try:
250 subprocess.check_output(cmd, stderr=subprocess.STDOUT)
251 except subprocess.CalledProcessError as e:
252 results.append(output_api.PresubmitError(e.output))
253 return results
254
255
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000256def _CommonChecks(input_api, output_api):
257 """Presubmit checks common to upload and commit."""
258 results = []
259 sources = lambda x: (x.LocalPath().endswith('.h') or
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000260 x.LocalPath().endswith('.py') or
261 x.LocalPath().endswith('.sh') or
mtklein18e55802015-03-25 07:21:20 -0700262 x.LocalPath().endswith('.m') or
263 x.LocalPath().endswith('.mm') or
264 x.LocalPath().endswith('.go') or
265 x.LocalPath().endswith('.c') or
266 x.LocalPath().endswith('.cc') or
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000267 x.LocalPath().endswith('.cpp'))
Ben Wagner88855502017-10-12 17:55:19 -0400268 results.extend(_CheckChangeHasEol(
269 input_api, output_api, source_file_filter=sources))
270 with _WarningsAsErrors(output_api):
271 results.extend(input_api.canned_checks.CheckChangeHasNoCR(
272 input_api, output_api, source_file_filter=sources))
273 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
274 input_api, output_api, source_file_filter=sources))
Ben Wagnercf42e982018-02-09 17:41:20 -0500275 results.extend(_JsonChecks(input_api, output_api))
rmistry01cbf6c2015-03-12 07:48:40 -0700276 results.extend(_IfDefChecks(input_api, output_api))
borenetc7c91802015-03-25 04:47:02 -0700277 results.extend(_CopyrightChecks(input_api, output_api,
278 source_file_filter=sources))
Eric Boren58d1f762019-07-19 08:07:44 -0400279 results.extend(_CheckCompileIsolate(input_api, output_api))
Eric Boren6dc00212019-07-24 15:15:43 -0400280 results.extend(_CheckDEPSValid(input_api, output_api))
Mike Kleinbb413432019-07-26 11:55:40 -0500281 results.extend(_CheckIncludesFormatted(input_api, output_api))
Mike Klein96f64012020-04-03 10:59:37 -0500282 results.extend(_CheckGNFormatted(input_api, output_api))
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000283 return results
284
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000285
286def CheckChangeOnUpload(input_api, output_api):
Ravi Mistry4c0ffe72020-03-02 13:19:02 -0500287 """Presubmit checks for the change on upload."""
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000288 results = []
289 results.extend(_CommonChecks(input_api, output_api))
borenet1ed2ae42016-07-26 11:52:17 -0700290 # Run on upload, not commit, since the presubmit bot apparently doesn't have
borenet60b0a2d2016-10-04 12:45:41 -0700291 # coverage or Go installed.
borenet2dbbfa52016-10-14 06:32:09 -0700292 results.extend(_InfraTests(input_api, output_api))
Ravi Mistry57735162019-07-25 13:45:15 -0400293 results.extend(_CheckReleaseNotesForPublicAPI(input_api, output_api))
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000294 return results
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000295
296
rmistryb398ecc2016-08-29 08:13:29 -0700297class CodeReview(object):
298 """Abstracts which codereview tool is used for the specified issue."""
299
300 def __init__(self, input_api):
301 self._issue = input_api.change.issue
302 self._gerrit = input_api.gerrit
rmistryb398ecc2016-08-29 08:13:29 -0700303
304 def GetOwnerEmail(self):
Aaron Gablea49909a2017-10-09 12:50:52 -0700305 return self._gerrit.GetChangeOwner(self._issue)
rmistryb398ecc2016-08-29 08:13:29 -0700306
307 def GetSubject(self):
Aaron Gablea49909a2017-10-09 12:50:52 -0700308 return self._gerrit.GetChangeInfo(self._issue)['subject']
rmistryb398ecc2016-08-29 08:13:29 -0700309
310 def GetDescription(self):
Aaron Gablea49909a2017-10-09 12:50:52 -0700311 return self._gerrit.GetChangeDescription(self._issue)
rmistryb398ecc2016-08-29 08:13:29 -0700312
Ravi Mistry39eabb62016-10-05 08:41:12 -0400313 def GetReviewers(self):
Aaron Gablea49909a2017-10-09 12:50:52 -0700314 code_review_label = (
315 self._gerrit.GetChangeInfo(self._issue)['labels']['Code-Review'])
316 return [r['email'] for r in code_review_label.get('all', [])]
Ravi Mistry39eabb62016-10-05 08:41:12 -0400317
rmistryb398ecc2016-08-29 08:13:29 -0700318 def GetApprovers(self):
319 approvers = []
Aaron Gablea49909a2017-10-09 12:50:52 -0700320 code_review_label = (
321 self._gerrit.GetChangeInfo(self._issue)['labels']['Code-Review'])
322 for m in code_review_label.get('all', []):
323 if m.get("value") == 1:
324 approvers.append(m["email"])
rmistryb398ecc2016-08-29 08:13:29 -0700325 return approvers
326
327
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000328def _CheckOwnerIsInAuthorsFile(input_api, output_api):
329 results = []
rmistryb398ecc2016-08-29 08:13:29 -0700330 if input_api.change.issue:
331 cr = CodeReview(input_api)
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000332
rmistryb398ecc2016-08-29 08:13:29 -0700333 owner_email = cr.GetOwnerEmail()
Eric Borendd988292018-01-02 13:29:21 -0500334
335 # Service accounts don't need to be in AUTHORS.
Eric Boren1eec99c2018-04-26 13:09:48 -0400336 for suffix in SERVICE_ACCOUNT_SUFFIX:
337 if owner_email.endswith(suffix):
338 return results
Eric Borendd988292018-01-02 13:29:21 -0500339
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000340 try:
341 authors_content = ''
342 for line in open(AUTHORS_FILE_NAME):
343 if not line.startswith('#'):
344 authors_content += line
345 email_fnmatches = re.findall('<(.*)>', authors_content)
346 for email_fnmatch in email_fnmatches:
347 if fnmatch.fnmatch(owner_email, email_fnmatch):
348 # Found a match, the user is in the AUTHORS file break out of the loop
349 break
350 else:
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000351 results.append(
352 output_api.PresubmitError(
353 'The email %s is not in Skia\'s AUTHORS file.\n'
354 'Issue owner, this CL must include an addition to the Skia AUTHORS '
rmistry9806d4d2015-10-01 08:10:54 -0700355 'file.'
rmistry83fab472014-07-18 05:25:56 -0700356 % owner_email))
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000357 except IOError:
358 # Do not fail if authors file cannot be found.
359 traceback.print_exc()
360 input_api.logging.error('AUTHORS file not found!')
361
362 return results
363
364
Ravi Mistry57735162019-07-25 13:45:15 -0400365def _CheckReleaseNotesForPublicAPI(input_api, output_api):
366 """Checks to see if release notes file is updated with public API changes."""
367 results = []
368 public_api_changed = False
369 release_file_changed = False
370 for affected_file in input_api.AffectedFiles():
371 affected_file_path = affected_file.LocalPath()
372 file_path, file_ext = os.path.splitext(affected_file_path)
373 # We only care about files that end in .h and are under the top-level
374 # include dir, but not include/private.
375 if (file_ext == '.h' and
376 file_path.split(os.path.sep)[0] == 'include' and
377 'private' not in file_path):
378 public_api_changed = True
379 elif affected_file_path == RELEASE_NOTES_FILE_NAME:
380 release_file_changed = True
381
382 if public_api_changed and not release_file_changed:
383 results.append(output_api.PresubmitPromptWarning(
384 'If this change affects a client API, please add a summary line '
385 'to the %s file.' % RELEASE_NOTES_FILE_NAME))
386 return results
387
388
389
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000390def _CheckLGTMsForPublicAPI(input_api, output_api):
391 """Check LGTMs for public API changes.
392
393 For public API files make sure there is an LGTM from the list of owners in
394 PUBLIC_API_OWNERS.
395 """
396 results = []
397 requires_owner_check = False
rmistry9407ece2014-08-26 14:00:54 -0700398 for affected_file in input_api.AffectedFiles():
399 affected_file_path = affected_file.LocalPath()
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000400 file_path, file_ext = os.path.splitext(affected_file_path)
rmistry9407ece2014-08-26 14:00:54 -0700401 # We only care about files that end in .h and are under the top-level
mtkleinbda12672015-07-28 08:54:12 -0700402 # include dir, but not include/private.
403 if (file_ext == '.h' and
404 'include' == file_path.split(os.path.sep)[0] and
405 'private' not in file_path):
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000406 requires_owner_check = True
407
408 if not requires_owner_check:
409 return results
410
411 lgtm_from_owner = False
rmistryb398ecc2016-08-29 08:13:29 -0700412 if input_api.change.issue:
413 cr = CodeReview(input_api)
414
415 if re.match(REVERT_CL_SUBJECT_PREFIX, cr.GetSubject(), re.I):
commit-bot@chromium.orgcfdc5962014-01-31 17:33:04 +0000416 # It is a revert CL, ignore the public api owners check.
417 return results
rmistryf2d83ca2014-08-26 10:30:29 -0700418
Ravi Mistry39eabb62016-10-05 08:41:12 -0400419 if input_api.gerrit:
420 for reviewer in cr.GetReviewers():
421 if reviewer in PUBLIC_API_OWNERS:
422 # If an owner is specified as an reviewer in Gerrit then ignore the
423 # public api owners check.
rmistryf2d83ca2014-08-26 10:30:29 -0700424 return results
Ravi Mistry39eabb62016-10-05 08:41:12 -0400425 else:
426 match = re.search(r'^TBR=(.*)$', cr.GetDescription(), re.M)
427 if match:
428 tbr_section = match.group(1).strip().split(' ')[0]
429 tbr_entries = tbr_section.split(',')
430 for owner in PUBLIC_API_OWNERS:
431 if owner in tbr_entries or owner.split('@')[0] in tbr_entries:
432 # If an owner is specified in the TBR= line then ignore the public
433 # api owners check.
434 return results
rmistryf2d83ca2014-08-26 10:30:29 -0700435
rmistryb398ecc2016-08-29 08:13:29 -0700436 if cr.GetOwnerEmail() in PUBLIC_API_OWNERS:
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000437 # An owner created the CL that is an automatic LGTM.
438 lgtm_from_owner = True
439
rmistryb398ecc2016-08-29 08:13:29 -0700440 for approver in cr.GetApprovers():
441 if approver in PUBLIC_API_OWNERS:
442 # Found an lgtm in a message from an owner.
443 lgtm_from_owner = True
444 break
commit-bot@chromium.orgcfdc5962014-01-31 17:33:04 +0000445
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000446 if not lgtm_from_owner:
447 results.append(
448 output_api.PresubmitError(
mtkleinbda12672015-07-28 08:54:12 -0700449 "If this CL adds to or changes Skia's public API, you need an LGTM "
450 "from any of %s. If this CL only removes from or doesn't change "
Ravi Mistrydbb84c22016-10-05 12:47:44 -0400451 "Skia's public API, please add a short note to the CL saying so. "
Aaron Gablea49909a2017-10-09 12:50:52 -0700452 "Add one of the owners as a reviewer to your CL as well as to the "
453 "TBR= line. If you don't know if this CL affects Skia's public "
454 "API, treat it like it does." % str(PUBLIC_API_OWNERS)))
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000455 return results
456
457
Edward Lemur2b7876c2020-01-17 18:48:13 -0500458def PostUploadHook(gerrit, change, output_api):
rmistryd223fb22015-02-26 10:16:13 -0800459 """git cl upload will call this hook after the issue is created/modified.
460
461 This hook does the following:
462 * Adds a link to preview docs changes if there are any docs changes in the CL.
Ravi Mistry355feab2017-05-23 14:24:08 -0400463 * Adds 'No-Try: true' if the CL contains only docs changes.
rmistryd223fb22015-02-26 10:16:13 -0800464 """
Edward Lemur2b7876c2020-01-17 18:48:13 -0500465 if not change.issue:
466 return []
467
468 # Skip PostUploadHooks for all auto-commit service account bots. New
469 # patchsets (caused due to PostUploadHooks) invalidates the CQ+2 vote from
470 # the "--use-commit-queue" flag to "git cl upload".
471 for suffix in SERVICE_ACCOUNT_SUFFIX:
472 if change.author_email.endswith(suffix):
473 return []
rmistryd223fb22015-02-26 10:16:13 -0800474
475 results = []
Edward Lemur2b7876c2020-01-17 18:48:13 -0500476 at_least_one_docs_change = False
rmistryd223fb22015-02-26 10:16:13 -0800477 all_docs_changes = True
478 for affected_file in change.AffectedFiles():
479 affected_file_path = affected_file.LocalPath()
480 file_path, _ = os.path.splitext(affected_file_path)
481 if 'site' == file_path.split(os.path.sep)[0]:
Edward Lemur2b7876c2020-01-17 18:48:13 -0500482 at_least_one_docs_change = True
rmistryd223fb22015-02-26 10:16:13 -0800483 else:
484 all_docs_changes = False
Edward Lemur2b7876c2020-01-17 18:48:13 -0500485 if at_least_one_docs_change and not all_docs_changes:
rmistryd223fb22015-02-26 10:16:13 -0800486 break
487
Edward Lemur2b7876c2020-01-17 18:48:13 -0500488 footers = change.GitFootersFromDescription()
489 description_changed = False
Ravi Mistryb5e2acc2017-12-07 11:10:11 -0500490
Edward Lemur2b7876c2020-01-17 18:48:13 -0500491 # If the change includes only doc changes then add No-Try: true in the
492 # CL's description if it does not exist yet.
493 if all_docs_changes and 'true' not in footers.get('No-Try', []):
494 description_changed = True
Edward Lemurc631b7c2020-02-04 15:30:18 -0500495 change.AddDescriptionFooter('No-Try', 'true')
Edward Lemur2b7876c2020-01-17 18:48:13 -0500496 results.append(
497 output_api.PresubmitNotifyResult(
498 'This change has only doc changes. Automatically added '
499 '\'No-Try: true\' to the CL\'s description'))
rmistryd223fb22015-02-26 10:16:13 -0800500
Edward Lemur2b7876c2020-01-17 18:48:13 -0500501 # If there is at least one docs change then add preview link in the CL's
502 # description if it does not already exist there.
503 docs_preview_link = DOCS_PREVIEW_URL.format(issue=change.issue)
504 if (at_least_one_docs_change
505 and docs_preview_link not in footers.get('Docs-Preview', [])):
506 # Automatically add a link to where the docs can be previewed.
507 description_changed = True
508 change.AddDescriptionFooter('Docs-Preview', docs_preview_link)
509 results.append(
510 output_api.PresubmitNotifyResult(
511 'Automatically added a link to preview the docs changes to the '
512 'CL\'s description'))
rmistryd223fb22015-02-26 10:16:13 -0800513
Edward Lemur2b7876c2020-01-17 18:48:13 -0500514 # If the description has changed update it.
515 if description_changed:
516 gerrit.UpdateDescription(
517 change.FullDescriptionText(), change.issue)
rmistryd223fb22015-02-26 10:16:13 -0800518
Edward Lemur2b7876c2020-01-17 18:48:13 -0500519 return results
rmistryd223fb22015-02-26 10:16:13 -0800520
521
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000522def CheckChangeOnCommit(input_api, output_api):
Ravi Mistry4c0ffe72020-03-02 13:19:02 -0500523 """Presubmit checks for the change on commit."""
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000524 results = []
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000525 results.extend(_CommonChecks(input_api, output_api))
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000526 results.extend(_CheckLGTMsForPublicAPI(input_api, output_api))
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000527 results.extend(_CheckOwnerIsInAuthorsFile(input_api, output_api))
Ravi Mistrya70cb8a2017-09-12 13:52:05 -0400528 # Checks for the presence of 'DO NOT''SUBMIT' in CL description and in
529 # content of files.
530 results.extend(
531 input_api.canned_checks.CheckDoNotSubmit(input_api, output_api))
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000532 return results