blob: 99c0fd315946afaa93b8c16f668e01384dac8673 [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
rmistry@google.com547012d2013-04-12 19:45:46 +000024SKIA_TREE_STATUS_URL = 'http://skia-tree-status.appspot.com'
25
rmistryf2d83ca2014-08-26 10:30:29 -070026# Please add the complete email address here (and not just 'xyz@' or 'xyz').
rmistry@google.comfb4a68d2013-08-12 14:51:20 +000027PUBLIC_API_OWNERS = (
Mike Reed7dafb092019-04-02 10:16:42 -040028 'mtklein@chromium.org',
29 'mtklein@google.com',
rmistry@google.comfb4a68d2013-08-12 14:51:20 +000030 'reed@chromium.org',
31 'reed@google.com',
32 'bsalomon@chromium.org',
33 'bsalomon@google.com',
rmistry83fab472014-07-18 05:25:56 -070034 'djsollen@chromium.org',
35 'djsollen@google.com',
Ravi Mistryfbff3292017-01-19 12:00:08 -050036 'hcm@chromium.org',
37 'hcm@google.com',
rmistry@google.comfb4a68d2013-08-12 14:51:20 +000038)
39
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +000040AUTHORS_FILE_NAME = 'AUTHORS'
Ravi Mistry57735162019-07-25 13:45:15 -040041RELEASE_NOTES_FILE_NAME = 'RELEASE_NOTES.txt'
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +000042
Joe Gregorio73259422018-05-22 15:29:03 +000043DOCS_PREVIEW_URL = 'https://skia.org/?cl='
rmistryd88b0be2016-05-20 03:50:01 -070044GOLD_TRYBOT_URL = 'https://gold.skia.org/search?issue='
rmistryd223fb22015-02-26 10:16:13 -080045
Eric Boren1eec99c2018-04-26 13:09:48 -040046SERVICE_ACCOUNT_SUFFIX = [
Eric Boren47ed6f12018-04-26 14:02:43 -040047 '@%s.iam.gserviceaccount.com' % project for project in [
Eric Boren6ad3ca42018-09-07 14:22:16 -040048 'skia-buildbots.google.com', 'skia-swarming-bots', 'skia-public',
Ravi Mistry53c44232019-03-12 08:51:42 -040049 'skia-corp.google.com', 'chops-service-accounts']]
Eric Borendd988292018-01-02 13:29:21 -050050
rmistry@google.com547012d2013-04-12 19:45:46 +000051
rmistry@google.com713276b2013-01-25 18:27:34 +000052def _CheckChangeHasEol(input_api, output_api, source_file_filter=None):
53 """Checks that files end with atleast one \n (LF)."""
54 eof_files = []
55 for f in input_api.AffectedSourceFiles(source_file_filter):
56 contents = input_api.ReadFile(f, 'rb')
57 # Check that the file ends in atleast one newline character.
58 if len(contents) > 1 and contents[-1:] != '\n':
59 eof_files.append(f.LocalPath())
60
61 if eof_files:
62 return [output_api.PresubmitPromptWarning(
63 'These files should end in a newline character:',
64 items=eof_files)]
65 return []
66
67
Ben Wagnercf42e982018-02-09 17:41:20 -050068def _JsonChecks(input_api, output_api):
69 """Run checks on any modified json files."""
70 failing_files = []
71 for affected_file in input_api.AffectedFiles(None):
72 affected_file_path = affected_file.LocalPath()
73 is_json = affected_file_path.endswith('.json')
74 is_metadata = (affected_file_path.startswith('site/') and
75 affected_file_path.endswith('/METADATA'))
76 if is_json or is_metadata:
77 try:
78 input_api.json.load(open(affected_file_path, 'r'))
79 except ValueError:
80 failing_files.append(affected_file_path)
81
82 results = []
83 if failing_files:
84 results.append(
85 output_api.PresubmitError(
86 'The following files contain invalid json:\n%s\n\n' %
87 '\n'.join(failing_files)))
88 return results
89
90
rmistry01cbf6c2015-03-12 07:48:40 -070091def _IfDefChecks(input_api, output_api):
92 """Ensures if/ifdef are not before includes. See skbug/3362 for details."""
93 comment_block_start_pattern = re.compile('^\s*\/\*.*$')
94 comment_block_middle_pattern = re.compile('^\s+\*.*')
95 comment_block_end_pattern = re.compile('^\s+\*\/.*$')
96 single_line_comment_pattern = re.compile('^\s*//.*$')
97 def is_comment(line):
98 return (comment_block_start_pattern.match(line) or
99 comment_block_middle_pattern.match(line) or
100 comment_block_end_pattern.match(line) or
101 single_line_comment_pattern.match(line))
102
103 empty_line_pattern = re.compile('^\s*$')
104 def is_empty_line(line):
105 return empty_line_pattern.match(line)
106
107 failing_files = []
108 for affected_file in input_api.AffectedSourceFiles(None):
109 affected_file_path = affected_file.LocalPath()
110 if affected_file_path.endswith('.cpp') or affected_file_path.endswith('.h'):
111 f = open(affected_file_path)
112 for line in f.xreadlines():
113 if is_comment(line) or is_empty_line(line):
114 continue
115 # The below will be the first real line after comments and newlines.
116 if line.startswith('#if 0 '):
117 pass
118 elif line.startswith('#if ') or line.startswith('#ifdef '):
119 failing_files.append(affected_file_path)
120 break
121
122 results = []
123 if failing_files:
124 results.append(
125 output_api.PresubmitError(
126 'The following files have #if or #ifdef before includes:\n%s\n\n'
halcanary6950de62015-11-07 05:29:00 -0800127 'See https://bug.skia.org/3362 for why this should be fixed.' %
rmistry01cbf6c2015-03-12 07:48:40 -0700128 '\n'.join(failing_files)))
129 return results
130
131
borenetc7c91802015-03-25 04:47:02 -0700132def _CopyrightChecks(input_api, output_api, source_file_filter=None):
133 results = []
134 year_pattern = r'\d{4}'
135 year_range_pattern = r'%s(-%s)?' % (year_pattern, year_pattern)
136 years_pattern = r'%s(,%s)*,?' % (year_range_pattern, year_range_pattern)
137 copyright_pattern = (
138 r'Copyright (\([cC]\) )?%s \w+' % years_pattern)
139
140 for affected_file in input_api.AffectedSourceFiles(source_file_filter):
141 if 'third_party' in affected_file.LocalPath():
142 continue
143 contents = input_api.ReadFile(affected_file, 'rb')
144 if not re.search(copyright_pattern, contents):
145 results.append(output_api.PresubmitError(
146 '%s is missing a correct copyright header.' % affected_file))
147 return results
148
149
mtkleine4b19c42015-05-05 10:28:44 -0700150def _ToolFlags(input_api, output_api):
151 """Make sure `{dm,nanobench}_flags.py test` passes if modified."""
152 results = []
153 sources = lambda x: ('dm_flags.py' in x.LocalPath() or
154 'nanobench_flags.py' in x.LocalPath())
155 for f in input_api.AffectedSourceFiles(sources):
156 if 0 != subprocess.call(['python', f.LocalPath(), 'test']):
157 results.append(output_api.PresubmitError('`python %s test` failed' % f))
158 return results
159
160
borenet2dbbfa52016-10-14 06:32:09 -0700161def _InfraTests(input_api, output_api):
162 """Run the infra tests."""
borenet1ed2ae42016-07-26 11:52:17 -0700163 results = []
mtklein3da80f52016-07-27 04:14:07 -0700164 if not any(f.LocalPath().startswith('infra')
165 for f in input_api.AffectedFiles()):
166 return results
167
borenet2dbbfa52016-10-14 06:32:09 -0700168 cmd = ['python', os.path.join('infra', 'bots', 'infra_tests.py')]
borenet60b0a2d2016-10-04 12:45:41 -0700169 try:
170 subprocess.check_output(cmd)
171 except subprocess.CalledProcessError as e:
172 results.append(output_api.PresubmitError(
173 '`%s` failed:\n%s' % (' '.join(cmd), e.output)))
174 return results
175
176
mtklein4db3b792016-08-03 14:18:22 -0700177def _CheckGNFormatted(input_api, output_api):
178 """Make sure any .gn files we're changing have been formatted."""
179 results = []
180 for f in input_api.AffectedFiles():
Mike Kleina5fb6152016-10-26 14:17:04 -0400181 if (not f.LocalPath().endswith('.gn') and
182 not f.LocalPath().endswith('.gni')):
mtklein4db3b792016-08-03 14:18:22 -0700183 continue
184
Mike Klein7a1c53d2016-10-11 14:03:06 -0400185 gn = 'gn.bat' if 'win32' in sys.platform else 'gn'
186 cmd = [gn, 'format', '--dry-run', f.LocalPath()]
mtklein4db3b792016-08-03 14:18:22 -0700187 try:
188 subprocess.check_output(cmd)
189 except subprocess.CalledProcessError:
mtkleind434b012016-08-10 07:30:58 -0700190 fix = 'gn format ' + f.LocalPath()
mtklein4db3b792016-08-03 14:18:22 -0700191 results.append(output_api.PresubmitError(
mtkleind434b012016-08-10 07:30:58 -0700192 '`%s` failed, try\n\t%s' % (' '.join(cmd), fix)))
mtklein4db3b792016-08-03 14:18:22 -0700193 return results
194
Mike Kleinbb413432019-07-26 11:55:40 -0500195def _CheckIncludesFormatted(input_api, output_api):
196 """Make sure #includes in files we're changing have been formatted."""
Mike Kleinf9ad5ba2019-07-29 12:34:39 -0500197 files = [str(f) for f in input_api.AffectedFiles() if f.Action() != 'D']
Mike Kleinbb413432019-07-26 11:55:40 -0500198 cmd = ['python',
199 'tools/rewrite_includes.py',
Mike Kleinf9ad5ba2019-07-29 12:34:39 -0500200 '--dry-run'] + files
Chris Daltone68b5bd2019-07-29 16:05:15 -0600201 # TODO(mtklein): tools/rewrite_includes.py doesn't work on Windows.
202 if 'win32' not in sys.platform and 0 != subprocess.call(cmd):
Mike Kleinbb413432019-07-26 11:55:40 -0500203 return [output_api.PresubmitError('`%s` failed' % ' '.join(cmd))]
204 return []
borenet1ed2ae42016-07-26 11:52:17 -0700205
Eric Boren58d1f762019-07-19 08:07:44 -0400206def _CheckCompileIsolate(input_api, output_api):
207 """Ensure that gen_compile_isolate.py does not change compile.isolate."""
208 # Only run the check if files were added or removed.
209 results = []
210 script = os.path.join('infra', 'bots', 'gen_compile_isolate.py')
211 isolate = os.path.join('infra', 'bots', 'compile.isolated')
212 for f in input_api.AffectedFiles():
213 if f.Action() in ('A', 'D', 'R'):
214 break
215 if f.LocalPath() in (script, isolate):
216 break
217 else:
218 return results
219
220 cmd = ['python', script, 'test']
221 try:
222 subprocess.check_output(cmd, stderr=subprocess.STDOUT)
223 except subprocess.CalledProcessError as e:
224 results.append(output_api.PresubmitError(e.output))
225 return results
226
227
Ben Wagner88855502017-10-12 17:55:19 -0400228class _WarningsAsErrors():
229 def __init__(self, output_api):
230 self.output_api = output_api
231 self.old_warning = None
232 def __enter__(self):
233 self.old_warning = self.output_api.PresubmitPromptWarning
234 self.output_api.PresubmitPromptWarning = self.output_api.PresubmitError
235 return self.output_api
236 def __exit__(self, ex_type, ex_value, ex_traceback):
237 self.output_api.PresubmitPromptWarning = self.old_warning
238
239
Eric Boren6dc00212019-07-24 15:15:43 -0400240def _CheckDEPSValid(input_api, output_api):
241 """Ensure that DEPS contains valid entries."""
242 results = []
243 script = os.path.join('infra', 'bots', 'check_deps.py')
244 relevant_files = ('DEPS', script)
245 for f in input_api.AffectedFiles():
246 if f.LocalPath() in relevant_files:
247 break
248 else:
249 return results
250 cmd = ['python', script]
251 try:
252 subprocess.check_output(cmd, stderr=subprocess.STDOUT)
253 except subprocess.CalledProcessError as e:
254 results.append(output_api.PresubmitError(e.output))
255 return results
256
257
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000258def _CommonChecks(input_api, output_api):
259 """Presubmit checks common to upload and commit."""
260 results = []
261 sources = lambda x: (x.LocalPath().endswith('.h') or
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000262 x.LocalPath().endswith('.py') or
263 x.LocalPath().endswith('.sh') or
mtklein18e55802015-03-25 07:21:20 -0700264 x.LocalPath().endswith('.m') or
265 x.LocalPath().endswith('.mm') or
266 x.LocalPath().endswith('.go') or
267 x.LocalPath().endswith('.c') or
268 x.LocalPath().endswith('.cc') or
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000269 x.LocalPath().endswith('.cpp'))
Ben Wagner88855502017-10-12 17:55:19 -0400270 results.extend(_CheckChangeHasEol(
271 input_api, output_api, source_file_filter=sources))
272 with _WarningsAsErrors(output_api):
273 results.extend(input_api.canned_checks.CheckChangeHasNoCR(
274 input_api, output_api, source_file_filter=sources))
275 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
276 input_api, output_api, source_file_filter=sources))
Ben Wagnercf42e982018-02-09 17:41:20 -0500277 results.extend(_JsonChecks(input_api, output_api))
rmistry01cbf6c2015-03-12 07:48:40 -0700278 results.extend(_IfDefChecks(input_api, output_api))
borenetc7c91802015-03-25 04:47:02 -0700279 results.extend(_CopyrightChecks(input_api, output_api,
280 source_file_filter=sources))
mtkleine4b19c42015-05-05 10:28:44 -0700281 results.extend(_ToolFlags(input_api, output_api))
Eric Boren58d1f762019-07-19 08:07:44 -0400282 results.extend(_CheckCompileIsolate(input_api, output_api))
Eric Boren6dc00212019-07-24 15:15:43 -0400283 results.extend(_CheckDEPSValid(input_api, output_api))
Mike Kleinbb413432019-07-26 11:55:40 -0500284 results.extend(_CheckIncludesFormatted(input_api, output_api))
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000285 return results
286
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000287
288def CheckChangeOnUpload(input_api, output_api):
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000289 """Presubmit checks for the change on upload.
290
291 The following are the presubmit checks:
292 * Check change has one and only one EOL.
293 """
294 results = []
295 results.extend(_CommonChecks(input_api, output_api))
borenet1ed2ae42016-07-26 11:52:17 -0700296 # Run on upload, not commit, since the presubmit bot apparently doesn't have
borenet60b0a2d2016-10-04 12:45:41 -0700297 # coverage or Go installed.
borenet2dbbfa52016-10-14 06:32:09 -0700298 results.extend(_InfraTests(input_api, output_api))
borenet60b0a2d2016-10-04 12:45:41 -0700299
mtklein4db3b792016-08-03 14:18:22 -0700300 results.extend(_CheckGNFormatted(input_api, output_api))
Ravi Mistry57735162019-07-25 13:45:15 -0400301 results.extend(_CheckReleaseNotesForPublicAPI(input_api, output_api))
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000302 return results
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000303
304
rmistry@google.comc2993442013-01-23 14:35:58 +0000305def _CheckTreeStatus(input_api, output_api, json_url):
306 """Check whether to allow commit.
307
308 Args:
309 input_api: input related apis.
310 output_api: output related apis.
311 json_url: url to download json style status.
312 """
313 tree_status_results = input_api.canned_checks.CheckTreeIsOpen(
314 input_api, output_api, json_url=json_url)
315 if not tree_status_results:
316 # Check for caution state only if tree is not closed.
317 connection = input_api.urllib2.urlopen(json_url)
318 status = input_api.json.loads(connection.read())
319 connection.close()
rmistry@google.comf6c5f752013-03-29 17:26:00 +0000320 if ('caution' in status['message'].lower() and
321 os.isatty(sys.stdout.fileno())):
322 # Display a prompt only if we are in an interactive shell. Without this
323 # check the commit queue behaves incorrectly because it considers
324 # prompts to be failures.
rmistry@google.comc2993442013-01-23 14:35:58 +0000325 short_text = 'Tree state is: ' + status['general_state']
326 long_text = status['message'] + '\n' + json_url
327 tree_status_results.append(
328 output_api.PresubmitPromptWarning(
329 message=short_text, long_text=long_text))
rmistry@google.com547012d2013-04-12 19:45:46 +0000330 else:
331 # Tree status is closed. Put in message about contacting sheriff.
332 connection = input_api.urllib2.urlopen(
333 SKIA_TREE_STATUS_URL + '/current-sheriff')
334 sheriff_details = input_api.json.loads(connection.read())
335 if sheriff_details:
336 tree_status_results[0]._message += (
337 '\n\nPlease contact the current Skia sheriff (%s) if you are trying '
338 'to submit a build fix\nand do not know how to submit because the '
339 'tree is closed') % sheriff_details['username']
rmistry@google.comc2993442013-01-23 14:35:58 +0000340 return tree_status_results
341
342
rmistryb398ecc2016-08-29 08:13:29 -0700343class CodeReview(object):
344 """Abstracts which codereview tool is used for the specified issue."""
345
346 def __init__(self, input_api):
347 self._issue = input_api.change.issue
348 self._gerrit = input_api.gerrit
rmistryb398ecc2016-08-29 08:13:29 -0700349
350 def GetOwnerEmail(self):
Aaron Gablea49909a2017-10-09 12:50:52 -0700351 return self._gerrit.GetChangeOwner(self._issue)
rmistryb398ecc2016-08-29 08:13:29 -0700352
353 def GetSubject(self):
Aaron Gablea49909a2017-10-09 12:50:52 -0700354 return self._gerrit.GetChangeInfo(self._issue)['subject']
rmistryb398ecc2016-08-29 08:13:29 -0700355
356 def GetDescription(self):
Aaron Gablea49909a2017-10-09 12:50:52 -0700357 return self._gerrit.GetChangeDescription(self._issue)
rmistryb398ecc2016-08-29 08:13:29 -0700358
359 def IsDryRun(self):
Aaron Gablea49909a2017-10-09 12:50:52 -0700360 return self._gerrit.GetChangeInfo(
361 self._issue)['labels']['Commit-Queue'].get('value', 0) == 1
rmistryb398ecc2016-08-29 08:13:29 -0700362
Ravi Mistry39eabb62016-10-05 08:41:12 -0400363 def GetReviewers(self):
Aaron Gablea49909a2017-10-09 12:50:52 -0700364 code_review_label = (
365 self._gerrit.GetChangeInfo(self._issue)['labels']['Code-Review'])
366 return [r['email'] for r in code_review_label.get('all', [])]
Ravi Mistry39eabb62016-10-05 08:41:12 -0400367
rmistryb398ecc2016-08-29 08:13:29 -0700368 def GetApprovers(self):
369 approvers = []
Aaron Gablea49909a2017-10-09 12:50:52 -0700370 code_review_label = (
371 self._gerrit.GetChangeInfo(self._issue)['labels']['Code-Review'])
372 for m in code_review_label.get('all', []):
373 if m.get("value") == 1:
374 approvers.append(m["email"])
rmistryb398ecc2016-08-29 08:13:29 -0700375 return approvers
376
377
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000378def _CheckOwnerIsInAuthorsFile(input_api, output_api):
379 results = []
rmistryb398ecc2016-08-29 08:13:29 -0700380 if input_api.change.issue:
381 cr = CodeReview(input_api)
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000382
rmistryb398ecc2016-08-29 08:13:29 -0700383 owner_email = cr.GetOwnerEmail()
Eric Borendd988292018-01-02 13:29:21 -0500384
385 # Service accounts don't need to be in AUTHORS.
Eric Boren1eec99c2018-04-26 13:09:48 -0400386 for suffix in SERVICE_ACCOUNT_SUFFIX:
387 if owner_email.endswith(suffix):
388 return results
Eric Borendd988292018-01-02 13:29:21 -0500389
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000390 try:
391 authors_content = ''
392 for line in open(AUTHORS_FILE_NAME):
393 if not line.startswith('#'):
394 authors_content += line
395 email_fnmatches = re.findall('<(.*)>', authors_content)
396 for email_fnmatch in email_fnmatches:
397 if fnmatch.fnmatch(owner_email, email_fnmatch):
398 # Found a match, the user is in the AUTHORS file break out of the loop
399 break
400 else:
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000401 results.append(
402 output_api.PresubmitError(
403 'The email %s is not in Skia\'s AUTHORS file.\n'
404 'Issue owner, this CL must include an addition to the Skia AUTHORS '
rmistry9806d4d2015-10-01 08:10:54 -0700405 'file.'
rmistry83fab472014-07-18 05:25:56 -0700406 % owner_email))
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000407 except IOError:
408 # Do not fail if authors file cannot be found.
409 traceback.print_exc()
410 input_api.logging.error('AUTHORS file not found!')
411
412 return results
413
414
Ravi Mistry57735162019-07-25 13:45:15 -0400415def _CheckReleaseNotesForPublicAPI(input_api, output_api):
416 """Checks to see if release notes file is updated with public API changes."""
417 results = []
418 public_api_changed = False
419 release_file_changed = False
420 for affected_file in input_api.AffectedFiles():
421 affected_file_path = affected_file.LocalPath()
422 file_path, file_ext = os.path.splitext(affected_file_path)
423 # We only care about files that end in .h and are under the top-level
424 # include dir, but not include/private.
425 if (file_ext == '.h' and
426 file_path.split(os.path.sep)[0] == 'include' and
427 'private' not in file_path):
428 public_api_changed = True
429 elif affected_file_path == RELEASE_NOTES_FILE_NAME:
430 release_file_changed = True
431
432 if public_api_changed and not release_file_changed:
433 results.append(output_api.PresubmitPromptWarning(
434 'If this change affects a client API, please add a summary line '
435 'to the %s file.' % RELEASE_NOTES_FILE_NAME))
436 return results
437
438
439
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000440def _CheckLGTMsForPublicAPI(input_api, output_api):
441 """Check LGTMs for public API changes.
442
443 For public API files make sure there is an LGTM from the list of owners in
444 PUBLIC_API_OWNERS.
445 """
446 results = []
447 requires_owner_check = False
rmistry9407ece2014-08-26 14:00:54 -0700448 for affected_file in input_api.AffectedFiles():
449 affected_file_path = affected_file.LocalPath()
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000450 file_path, file_ext = os.path.splitext(affected_file_path)
rmistry9407ece2014-08-26 14:00:54 -0700451 # We only care about files that end in .h and are under the top-level
mtkleinbda12672015-07-28 08:54:12 -0700452 # include dir, but not include/private.
453 if (file_ext == '.h' and
454 'include' == file_path.split(os.path.sep)[0] and
455 'private' not in file_path):
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000456 requires_owner_check = True
457
458 if not requires_owner_check:
459 return results
460
461 lgtm_from_owner = False
rmistryb398ecc2016-08-29 08:13:29 -0700462 if input_api.change.issue:
463 cr = CodeReview(input_api)
464
465 if re.match(REVERT_CL_SUBJECT_PREFIX, cr.GetSubject(), re.I):
commit-bot@chromium.orgcfdc5962014-01-31 17:33:04 +0000466 # It is a revert CL, ignore the public api owners check.
467 return results
rmistryf2d83ca2014-08-26 10:30:29 -0700468
rmistryb398ecc2016-08-29 08:13:29 -0700469 if cr.IsDryRun():
rmistry04abf132015-04-07 07:41:51 -0700470 # Ignore public api owners check for dry run CLs since they are not
rmistryf91b7172015-03-12 09:48:10 -0700471 # going to be committed.
472 return results
473
Ravi Mistry39eabb62016-10-05 08:41:12 -0400474 if input_api.gerrit:
475 for reviewer in cr.GetReviewers():
476 if reviewer in PUBLIC_API_OWNERS:
477 # If an owner is specified as an reviewer in Gerrit then ignore the
478 # public api owners check.
rmistryf2d83ca2014-08-26 10:30:29 -0700479 return results
Ravi Mistry39eabb62016-10-05 08:41:12 -0400480 else:
481 match = re.search(r'^TBR=(.*)$', cr.GetDescription(), re.M)
482 if match:
483 tbr_section = match.group(1).strip().split(' ')[0]
484 tbr_entries = tbr_section.split(',')
485 for owner in PUBLIC_API_OWNERS:
486 if owner in tbr_entries or owner.split('@')[0] in tbr_entries:
487 # If an owner is specified in the TBR= line then ignore the public
488 # api owners check.
489 return results
rmistryf2d83ca2014-08-26 10:30:29 -0700490
rmistryb398ecc2016-08-29 08:13:29 -0700491 if cr.GetOwnerEmail() in PUBLIC_API_OWNERS:
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000492 # An owner created the CL that is an automatic LGTM.
493 lgtm_from_owner = True
494
rmistryb398ecc2016-08-29 08:13:29 -0700495 for approver in cr.GetApprovers():
496 if approver in PUBLIC_API_OWNERS:
497 # Found an lgtm in a message from an owner.
498 lgtm_from_owner = True
499 break
commit-bot@chromium.orgcfdc5962014-01-31 17:33:04 +0000500
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000501 if not lgtm_from_owner:
502 results.append(
503 output_api.PresubmitError(
mtkleinbda12672015-07-28 08:54:12 -0700504 "If this CL adds to or changes Skia's public API, you need an LGTM "
505 "from any of %s. If this CL only removes from or doesn't change "
Ravi Mistrydbb84c22016-10-05 12:47:44 -0400506 "Skia's public API, please add a short note to the CL saying so. "
Aaron Gablea49909a2017-10-09 12:50:52 -0700507 "Add one of the owners as a reviewer to your CL as well as to the "
508 "TBR= line. If you don't know if this CL affects Skia's public "
509 "API, treat it like it does." % str(PUBLIC_API_OWNERS)))
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000510 return results
511
512
Ravi Mistry355feab2017-05-23 14:24:08 -0400513def _FooterExists(footers, key, value):
514 for k, v in footers:
515 if k == key and v == value:
516 return True
517 return False
518
519
rmistryd223fb22015-02-26 10:16:13 -0800520def PostUploadHook(cl, change, output_api):
521 """git cl upload will call this hook after the issue is created/modified.
522
523 This hook does the following:
524 * Adds a link to preview docs changes if there are any docs changes in the CL.
Ravi Mistry355feab2017-05-23 14:24:08 -0400525 * Adds 'No-Try: true' if the CL contains only docs changes.
rmistryd223fb22015-02-26 10:16:13 -0800526 """
527
528 results = []
529 atleast_one_docs_change = False
530 all_docs_changes = True
531 for affected_file in change.AffectedFiles():
532 affected_file_path = affected_file.LocalPath()
533 file_path, _ = os.path.splitext(affected_file_path)
534 if 'site' == file_path.split(os.path.sep)[0]:
535 atleast_one_docs_change = True
536 else:
537 all_docs_changes = False
538 if atleast_one_docs_change and not all_docs_changes:
539 break
540
541 issue = cl.issue
rmistryb9a9e872016-09-01 09:52:32 -0700542 if issue:
Ravi Mistry4722a412018-05-03 08:02:03 -0400543 # Skip PostUploadHooks for all auto-commit service account bots. New
544 # patchsets (caused due to PostUploadHooks) invalidates the CQ+2 vote from
545 # the "--use-commit-queue" flag to "git cl upload".
546 for suffix in SERVICE_ACCOUNT_SUFFIX:
547 if cl.GetIssueOwner().endswith(suffix):
548 return results
Ravi Mistryb5e2acc2017-12-07 11:10:11 -0500549
Eric Borenbf17eec2017-04-03 08:30:35 -0400550 original_description_lines, footers = cl.GetDescriptionFooters()
551 new_description_lines = list(original_description_lines)
rmistryd223fb22015-02-26 10:16:13 -0800552
Ravi Mistry355feab2017-05-23 14:24:08 -0400553 # If the change includes only doc changes then add No-Try: true in the
Ravi Mistryb5e2acc2017-12-07 11:10:11 -0500554 # CL's description if it does not exist yet.
555 if all_docs_changes and not _FooterExists(footers, 'No-Try', 'true'):
Ravi Mistry355feab2017-05-23 14:24:08 -0400556 new_description_lines.append('No-Try: true')
rmistryd223fb22015-02-26 10:16:13 -0800557 results.append(
558 output_api.PresubmitNotifyResult(
559 'This change has only doc changes. Automatically added '
Ravi Mistry355feab2017-05-23 14:24:08 -0400560 '\'No-Try: true\' to the CL\'s description'))
rmistryd223fb22015-02-26 10:16:13 -0800561
562 # If there is atleast one docs change then add preview link in the CL's
563 # description if it does not already exist there.
Ravi Mistry355feab2017-05-23 14:24:08 -0400564 docs_preview_link = '%s%s' % (DOCS_PREVIEW_URL, issue)
565 docs_preview_line = 'Docs-Preview: %s' % docs_preview_link
Eric Borenbf17eec2017-04-03 08:30:35 -0400566 if (atleast_one_docs_change and
Ravi Mistry355feab2017-05-23 14:24:08 -0400567 not _FooterExists(footers, 'Docs-Preview', docs_preview_link)):
rmistryd223fb22015-02-26 10:16:13 -0800568 # Automatically add a link to where the docs can be previewed.
Eric Borenbf17eec2017-04-03 08:30:35 -0400569 new_description_lines.append(docs_preview_line)
rmistryd223fb22015-02-26 10:16:13 -0800570 results.append(
571 output_api.PresubmitNotifyResult(
572 'Automatically added a link to preview the docs changes to the '
573 'CL\'s description'))
574
575 # If the description has changed update it.
Eric Borenbf17eec2017-04-03 08:30:35 -0400576 if new_description_lines != original_description_lines:
Ravi Mistryee06ffe2017-05-08 12:59:56 -0400577 # Add a new line separating the new contents from the old contents.
578 new_description_lines.insert(len(original_description_lines), '')
Eric Borenbf17eec2017-04-03 08:30:35 -0400579 cl.UpdateDescriptionFooters(new_description_lines, footers)
rmistryd223fb22015-02-26 10:16:13 -0800580
581 return results
582
583
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000584def CheckChangeOnCommit(input_api, output_api):
585 """Presubmit checks for the change on commit.
586
587 The following are the presubmit checks:
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000588 * Check change has one and only one EOL.
rmistry@google.comc2993442013-01-23 14:35:58 +0000589 * Ensures that the Skia tree is open in
590 http://skia-tree-status.appspot.com/. Shows a warning if it is in 'Caution'
591 state and an error if it is in 'Closed' state.
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000592 """
593 results = []
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000594 results.extend(_CommonChecks(input_api, output_api))
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000595 results.extend(
rmistry@google.comc2993442013-01-23 14:35:58 +0000596 _CheckTreeStatus(input_api, output_api, json_url=(
rmistry@google.com547012d2013-04-12 19:45:46 +0000597 SKIA_TREE_STATUS_URL + '/banner-status?format=json')))
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000598 results.extend(_CheckLGTMsForPublicAPI(input_api, output_api))
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000599 results.extend(_CheckOwnerIsInAuthorsFile(input_api, output_api))
Ravi Mistrya70cb8a2017-09-12 13:52:05 -0400600 # Checks for the presence of 'DO NOT''SUBMIT' in CL description and in
601 # content of files.
602 results.extend(
603 input_api.canned_checks.CheckDoNotSubmit(input_api, output_api))
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000604 return results