blob: 0b7e31f786235937eeaa7b0cad11c61c3a7d033b [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
Hal Canary4df3d532019-07-30 13:49:45 -0400201 if 0 != subprocess.call(cmd):
Mike Kleinbb413432019-07-26 11:55:40 -0500202 return [output_api.PresubmitError('`%s` failed' % ' '.join(cmd))]
203 return []
borenet1ed2ae42016-07-26 11:52:17 -0700204
Eric Boren58d1f762019-07-19 08:07:44 -0400205def _CheckCompileIsolate(input_api, output_api):
206 """Ensure that gen_compile_isolate.py does not change compile.isolate."""
207 # Only run the check if files were added or removed.
208 results = []
209 script = os.path.join('infra', 'bots', 'gen_compile_isolate.py')
210 isolate = os.path.join('infra', 'bots', 'compile.isolated')
211 for f in input_api.AffectedFiles():
212 if f.Action() in ('A', 'D', 'R'):
213 break
214 if f.LocalPath() in (script, isolate):
215 break
216 else:
217 return results
218
219 cmd = ['python', script, 'test']
220 try:
221 subprocess.check_output(cmd, stderr=subprocess.STDOUT)
222 except subprocess.CalledProcessError as e:
223 results.append(output_api.PresubmitError(e.output))
224 return results
225
226
Ben Wagner88855502017-10-12 17:55:19 -0400227class _WarningsAsErrors():
228 def __init__(self, output_api):
229 self.output_api = output_api
230 self.old_warning = None
231 def __enter__(self):
232 self.old_warning = self.output_api.PresubmitPromptWarning
233 self.output_api.PresubmitPromptWarning = self.output_api.PresubmitError
234 return self.output_api
235 def __exit__(self, ex_type, ex_value, ex_traceback):
236 self.output_api.PresubmitPromptWarning = self.old_warning
237
238
Eric Boren6dc00212019-07-24 15:15:43 -0400239def _CheckDEPSValid(input_api, output_api):
240 """Ensure that DEPS contains valid entries."""
241 results = []
242 script = os.path.join('infra', 'bots', 'check_deps.py')
243 relevant_files = ('DEPS', script)
244 for f in input_api.AffectedFiles():
245 if f.LocalPath() in relevant_files:
246 break
247 else:
248 return results
249 cmd = ['python', script]
250 try:
251 subprocess.check_output(cmd, stderr=subprocess.STDOUT)
252 except subprocess.CalledProcessError as e:
253 results.append(output_api.PresubmitError(e.output))
254 return results
255
256
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000257def _CommonChecks(input_api, output_api):
258 """Presubmit checks common to upload and commit."""
259 results = []
260 sources = lambda x: (x.LocalPath().endswith('.h') or
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000261 x.LocalPath().endswith('.py') or
262 x.LocalPath().endswith('.sh') or
mtklein18e55802015-03-25 07:21:20 -0700263 x.LocalPath().endswith('.m') or
264 x.LocalPath().endswith('.mm') or
265 x.LocalPath().endswith('.go') or
266 x.LocalPath().endswith('.c') or
267 x.LocalPath().endswith('.cc') or
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000268 x.LocalPath().endswith('.cpp'))
Ben Wagner88855502017-10-12 17:55:19 -0400269 results.extend(_CheckChangeHasEol(
270 input_api, output_api, source_file_filter=sources))
271 with _WarningsAsErrors(output_api):
272 results.extend(input_api.canned_checks.CheckChangeHasNoCR(
273 input_api, output_api, source_file_filter=sources))
274 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
275 input_api, output_api, source_file_filter=sources))
Ben Wagnercf42e982018-02-09 17:41:20 -0500276 results.extend(_JsonChecks(input_api, output_api))
rmistry01cbf6c2015-03-12 07:48:40 -0700277 results.extend(_IfDefChecks(input_api, output_api))
borenetc7c91802015-03-25 04:47:02 -0700278 results.extend(_CopyrightChecks(input_api, output_api,
279 source_file_filter=sources))
mtkleine4b19c42015-05-05 10:28:44 -0700280 results.extend(_ToolFlags(input_api, output_api))
Eric Boren58d1f762019-07-19 08:07:44 -0400281 results.extend(_CheckCompileIsolate(input_api, output_api))
Eric Boren6dc00212019-07-24 15:15:43 -0400282 results.extend(_CheckDEPSValid(input_api, output_api))
Mike Kleinbb413432019-07-26 11:55:40 -0500283 results.extend(_CheckIncludesFormatted(input_api, output_api))
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000284 return results
285
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000286
287def CheckChangeOnUpload(input_api, output_api):
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000288 """Presubmit checks for the change on upload.
289
290 The following are the presubmit checks:
291 * Check change has one and only one EOL.
292 """
293 results = []
294 results.extend(_CommonChecks(input_api, output_api))
borenet1ed2ae42016-07-26 11:52:17 -0700295 # Run on upload, not commit, since the presubmit bot apparently doesn't have
borenet60b0a2d2016-10-04 12:45:41 -0700296 # coverage or Go installed.
borenet2dbbfa52016-10-14 06:32:09 -0700297 results.extend(_InfraTests(input_api, output_api))
borenet60b0a2d2016-10-04 12:45:41 -0700298
mtklein4db3b792016-08-03 14:18:22 -0700299 results.extend(_CheckGNFormatted(input_api, output_api))
Ravi Mistry57735162019-07-25 13:45:15 -0400300 results.extend(_CheckReleaseNotesForPublicAPI(input_api, output_api))
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000301 return results
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000302
303
rmistry@google.comc2993442013-01-23 14:35:58 +0000304def _CheckTreeStatus(input_api, output_api, json_url):
305 """Check whether to allow commit.
306
307 Args:
308 input_api: input related apis.
309 output_api: output related apis.
310 json_url: url to download json style status.
311 """
312 tree_status_results = input_api.canned_checks.CheckTreeIsOpen(
313 input_api, output_api, json_url=json_url)
314 if not tree_status_results:
315 # Check for caution state only if tree is not closed.
Eric Borena715ff52019-10-22 14:35:21 -0400316 connection = input_api.urllib_request.urlopen(json_url)
rmistry@google.comc2993442013-01-23 14:35:58 +0000317 status = input_api.json.loads(connection.read())
318 connection.close()
rmistry@google.comf6c5f752013-03-29 17:26:00 +0000319 if ('caution' in status['message'].lower() and
320 os.isatty(sys.stdout.fileno())):
321 # Display a prompt only if we are in an interactive shell. Without this
322 # check the commit queue behaves incorrectly because it considers
323 # prompts to be failures.
rmistry@google.comc2993442013-01-23 14:35:58 +0000324 short_text = 'Tree state is: ' + status['general_state']
325 long_text = status['message'] + '\n' + json_url
326 tree_status_results.append(
327 output_api.PresubmitPromptWarning(
328 message=short_text, long_text=long_text))
rmistry@google.com547012d2013-04-12 19:45:46 +0000329 else:
330 # Tree status is closed. Put in message about contacting sheriff.
Eric Borena715ff52019-10-22 14:35:21 -0400331 connection = input_api.urllib_request.urlopen(
rmistry@google.com547012d2013-04-12 19:45:46 +0000332 SKIA_TREE_STATUS_URL + '/current-sheriff')
333 sheriff_details = input_api.json.loads(connection.read())
334 if sheriff_details:
335 tree_status_results[0]._message += (
336 '\n\nPlease contact the current Skia sheriff (%s) if you are trying '
337 'to submit a build fix\nand do not know how to submit because the '
338 'tree is closed') % sheriff_details['username']
rmistry@google.comc2993442013-01-23 14:35:58 +0000339 return tree_status_results
340
341
rmistryb398ecc2016-08-29 08:13:29 -0700342class CodeReview(object):
343 """Abstracts which codereview tool is used for the specified issue."""
344
345 def __init__(self, input_api):
346 self._issue = input_api.change.issue
347 self._gerrit = input_api.gerrit
rmistryb398ecc2016-08-29 08:13:29 -0700348
349 def GetOwnerEmail(self):
Aaron Gablea49909a2017-10-09 12:50:52 -0700350 return self._gerrit.GetChangeOwner(self._issue)
rmistryb398ecc2016-08-29 08:13:29 -0700351
352 def GetSubject(self):
Aaron Gablea49909a2017-10-09 12:50:52 -0700353 return self._gerrit.GetChangeInfo(self._issue)['subject']
rmistryb398ecc2016-08-29 08:13:29 -0700354
355 def GetDescription(self):
Aaron Gablea49909a2017-10-09 12:50:52 -0700356 return self._gerrit.GetChangeDescription(self._issue)
rmistryb398ecc2016-08-29 08:13:29 -0700357
358 def IsDryRun(self):
Aaron Gablea49909a2017-10-09 12:50:52 -0700359 return self._gerrit.GetChangeInfo(
360 self._issue)['labels']['Commit-Queue'].get('value', 0) == 1
rmistryb398ecc2016-08-29 08:13:29 -0700361
Ravi Mistry39eabb62016-10-05 08:41:12 -0400362 def GetReviewers(self):
Aaron Gablea49909a2017-10-09 12:50:52 -0700363 code_review_label = (
364 self._gerrit.GetChangeInfo(self._issue)['labels']['Code-Review'])
365 return [r['email'] for r in code_review_label.get('all', [])]
Ravi Mistry39eabb62016-10-05 08:41:12 -0400366
rmistryb398ecc2016-08-29 08:13:29 -0700367 def GetApprovers(self):
368 approvers = []
Aaron Gablea49909a2017-10-09 12:50:52 -0700369 code_review_label = (
370 self._gerrit.GetChangeInfo(self._issue)['labels']['Code-Review'])
371 for m in code_review_label.get('all', []):
372 if m.get("value") == 1:
373 approvers.append(m["email"])
rmistryb398ecc2016-08-29 08:13:29 -0700374 return approvers
375
376
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000377def _CheckOwnerIsInAuthorsFile(input_api, output_api):
378 results = []
rmistryb398ecc2016-08-29 08:13:29 -0700379 if input_api.change.issue:
380 cr = CodeReview(input_api)
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000381
rmistryb398ecc2016-08-29 08:13:29 -0700382 owner_email = cr.GetOwnerEmail()
Eric Borendd988292018-01-02 13:29:21 -0500383
384 # Service accounts don't need to be in AUTHORS.
Eric Boren1eec99c2018-04-26 13:09:48 -0400385 for suffix in SERVICE_ACCOUNT_SUFFIX:
386 if owner_email.endswith(suffix):
387 return results
Eric Borendd988292018-01-02 13:29:21 -0500388
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000389 try:
390 authors_content = ''
391 for line in open(AUTHORS_FILE_NAME):
392 if not line.startswith('#'):
393 authors_content += line
394 email_fnmatches = re.findall('<(.*)>', authors_content)
395 for email_fnmatch in email_fnmatches:
396 if fnmatch.fnmatch(owner_email, email_fnmatch):
397 # Found a match, the user is in the AUTHORS file break out of the loop
398 break
399 else:
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000400 results.append(
401 output_api.PresubmitError(
402 'The email %s is not in Skia\'s AUTHORS file.\n'
403 'Issue owner, this CL must include an addition to the Skia AUTHORS '
rmistry9806d4d2015-10-01 08:10:54 -0700404 'file.'
rmistry83fab472014-07-18 05:25:56 -0700405 % owner_email))
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000406 except IOError:
407 # Do not fail if authors file cannot be found.
408 traceback.print_exc()
409 input_api.logging.error('AUTHORS file not found!')
410
411 return results
412
413
Ravi Mistry57735162019-07-25 13:45:15 -0400414def _CheckReleaseNotesForPublicAPI(input_api, output_api):
415 """Checks to see if release notes file is updated with public API changes."""
416 results = []
417 public_api_changed = False
418 release_file_changed = False
419 for affected_file in input_api.AffectedFiles():
420 affected_file_path = affected_file.LocalPath()
421 file_path, file_ext = os.path.splitext(affected_file_path)
422 # We only care about files that end in .h and are under the top-level
423 # include dir, but not include/private.
424 if (file_ext == '.h' and
425 file_path.split(os.path.sep)[0] == 'include' and
426 'private' not in file_path):
427 public_api_changed = True
428 elif affected_file_path == RELEASE_NOTES_FILE_NAME:
429 release_file_changed = True
430
431 if public_api_changed and not release_file_changed:
432 results.append(output_api.PresubmitPromptWarning(
433 'If this change affects a client API, please add a summary line '
434 'to the %s file.' % RELEASE_NOTES_FILE_NAME))
435 return results
436
437
438
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000439def _CheckLGTMsForPublicAPI(input_api, output_api):
440 """Check LGTMs for public API changes.
441
442 For public API files make sure there is an LGTM from the list of owners in
443 PUBLIC_API_OWNERS.
444 """
445 results = []
446 requires_owner_check = False
rmistry9407ece2014-08-26 14:00:54 -0700447 for affected_file in input_api.AffectedFiles():
448 affected_file_path = affected_file.LocalPath()
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000449 file_path, file_ext = os.path.splitext(affected_file_path)
rmistry9407ece2014-08-26 14:00:54 -0700450 # We only care about files that end in .h and are under the top-level
mtkleinbda12672015-07-28 08:54:12 -0700451 # include dir, but not include/private.
452 if (file_ext == '.h' and
453 'include' == file_path.split(os.path.sep)[0] and
454 'private' not in file_path):
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000455 requires_owner_check = True
456
457 if not requires_owner_check:
458 return results
459
460 lgtm_from_owner = False
rmistryb398ecc2016-08-29 08:13:29 -0700461 if input_api.change.issue:
462 cr = CodeReview(input_api)
463
464 if re.match(REVERT_CL_SUBJECT_PREFIX, cr.GetSubject(), re.I):
commit-bot@chromium.orgcfdc5962014-01-31 17:33:04 +0000465 # It is a revert CL, ignore the public api owners check.
466 return results
rmistryf2d83ca2014-08-26 10:30:29 -0700467
rmistryb398ecc2016-08-29 08:13:29 -0700468 if cr.IsDryRun():
rmistry04abf132015-04-07 07:41:51 -0700469 # Ignore public api owners check for dry run CLs since they are not
rmistryf91b7172015-03-12 09:48:10 -0700470 # going to be committed.
471 return results
472
Ravi Mistry39eabb62016-10-05 08:41:12 -0400473 if input_api.gerrit:
474 for reviewer in cr.GetReviewers():
475 if reviewer in PUBLIC_API_OWNERS:
476 # If an owner is specified as an reviewer in Gerrit then ignore the
477 # public api owners check.
rmistryf2d83ca2014-08-26 10:30:29 -0700478 return results
Ravi Mistry39eabb62016-10-05 08:41:12 -0400479 else:
480 match = re.search(r'^TBR=(.*)$', cr.GetDescription(), re.M)
481 if match:
482 tbr_section = match.group(1).strip().split(' ')[0]
483 tbr_entries = tbr_section.split(',')
484 for owner in PUBLIC_API_OWNERS:
485 if owner in tbr_entries or owner.split('@')[0] in tbr_entries:
486 # If an owner is specified in the TBR= line then ignore the public
487 # api owners check.
488 return results
rmistryf2d83ca2014-08-26 10:30:29 -0700489
rmistryb398ecc2016-08-29 08:13:29 -0700490 if cr.GetOwnerEmail() in PUBLIC_API_OWNERS:
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000491 # An owner created the CL that is an automatic LGTM.
492 lgtm_from_owner = True
493
rmistryb398ecc2016-08-29 08:13:29 -0700494 for approver in cr.GetApprovers():
495 if approver in PUBLIC_API_OWNERS:
496 # Found an lgtm in a message from an owner.
497 lgtm_from_owner = True
498 break
commit-bot@chromium.orgcfdc5962014-01-31 17:33:04 +0000499
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000500 if not lgtm_from_owner:
501 results.append(
502 output_api.PresubmitError(
mtkleinbda12672015-07-28 08:54:12 -0700503 "If this CL adds to or changes Skia's public API, you need an LGTM "
504 "from any of %s. If this CL only removes from or doesn't change "
Ravi Mistrydbb84c22016-10-05 12:47:44 -0400505 "Skia's public API, please add a short note to the CL saying so. "
Aaron Gablea49909a2017-10-09 12:50:52 -0700506 "Add one of the owners as a reviewer to your CL as well as to the "
507 "TBR= line. If you don't know if this CL affects Skia's public "
508 "API, treat it like it does." % str(PUBLIC_API_OWNERS)))
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000509 return results
510
511
Ravi Mistry355feab2017-05-23 14:24:08 -0400512def _FooterExists(footers, key, value):
513 for k, v in footers:
514 if k == key and v == value:
515 return True
516 return False
517
518
rmistryd223fb22015-02-26 10:16:13 -0800519def PostUploadHook(cl, change, output_api):
520 """git cl upload will call this hook after the issue is created/modified.
521
522 This hook does the following:
523 * Adds a link to preview docs changes if there are any docs changes in the CL.
Ravi Mistry355feab2017-05-23 14:24:08 -0400524 * Adds 'No-Try: true' if the CL contains only docs changes.
rmistryd223fb22015-02-26 10:16:13 -0800525 """
526
527 results = []
528 atleast_one_docs_change = False
529 all_docs_changes = True
530 for affected_file in change.AffectedFiles():
531 affected_file_path = affected_file.LocalPath()
532 file_path, _ = os.path.splitext(affected_file_path)
533 if 'site' == file_path.split(os.path.sep)[0]:
534 atleast_one_docs_change = True
535 else:
536 all_docs_changes = False
537 if atleast_one_docs_change and not all_docs_changes:
538 break
539
540 issue = cl.issue
rmistryb9a9e872016-09-01 09:52:32 -0700541 if issue:
Ravi Mistry4722a412018-05-03 08:02:03 -0400542 # Skip PostUploadHooks for all auto-commit service account bots. New
543 # patchsets (caused due to PostUploadHooks) invalidates the CQ+2 vote from
544 # the "--use-commit-queue" flag to "git cl upload".
545 for suffix in SERVICE_ACCOUNT_SUFFIX:
546 if cl.GetIssueOwner().endswith(suffix):
547 return results
Ravi Mistryb5e2acc2017-12-07 11:10:11 -0500548
Eric Borenbf17eec2017-04-03 08:30:35 -0400549 original_description_lines, footers = cl.GetDescriptionFooters()
550 new_description_lines = list(original_description_lines)
rmistryd223fb22015-02-26 10:16:13 -0800551
Ravi Mistry355feab2017-05-23 14:24:08 -0400552 # If the change includes only doc changes then add No-Try: true in the
Ravi Mistryb5e2acc2017-12-07 11:10:11 -0500553 # CL's description if it does not exist yet.
554 if all_docs_changes and not _FooterExists(footers, 'No-Try', 'true'):
Ravi Mistry355feab2017-05-23 14:24:08 -0400555 new_description_lines.append('No-Try: true')
rmistryd223fb22015-02-26 10:16:13 -0800556 results.append(
557 output_api.PresubmitNotifyResult(
558 'This change has only doc changes. Automatically added '
Ravi Mistry355feab2017-05-23 14:24:08 -0400559 '\'No-Try: true\' to the CL\'s description'))
rmistryd223fb22015-02-26 10:16:13 -0800560
561 # If there is atleast one docs change then add preview link in the CL's
562 # description if it does not already exist there.
Ravi Mistry355feab2017-05-23 14:24:08 -0400563 docs_preview_link = '%s%s' % (DOCS_PREVIEW_URL, issue)
564 docs_preview_line = 'Docs-Preview: %s' % docs_preview_link
Eric Borenbf17eec2017-04-03 08:30:35 -0400565 if (atleast_one_docs_change and
Ravi Mistry355feab2017-05-23 14:24:08 -0400566 not _FooterExists(footers, 'Docs-Preview', docs_preview_link)):
rmistryd223fb22015-02-26 10:16:13 -0800567 # Automatically add a link to where the docs can be previewed.
Eric Borenbf17eec2017-04-03 08:30:35 -0400568 new_description_lines.append(docs_preview_line)
rmistryd223fb22015-02-26 10:16:13 -0800569 results.append(
570 output_api.PresubmitNotifyResult(
571 'Automatically added a link to preview the docs changes to the '
572 'CL\'s description'))
573
574 # If the description has changed update it.
Eric Borenbf17eec2017-04-03 08:30:35 -0400575 if new_description_lines != original_description_lines:
Ravi Mistryee06ffe2017-05-08 12:59:56 -0400576 # Add a new line separating the new contents from the old contents.
577 new_description_lines.insert(len(original_description_lines), '')
Eric Borenbf17eec2017-04-03 08:30:35 -0400578 cl.UpdateDescriptionFooters(new_description_lines, footers)
rmistryd223fb22015-02-26 10:16:13 -0800579
580 return results
581
582
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000583def CheckChangeOnCommit(input_api, output_api):
584 """Presubmit checks for the change on commit.
585
586 The following are the presubmit checks:
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000587 * Check change has one and only one EOL.
rmistry@google.comc2993442013-01-23 14:35:58 +0000588 * Ensures that the Skia tree is open in
589 http://skia-tree-status.appspot.com/. Shows a warning if it is in 'Caution'
590 state and an error if it is in 'Closed' state.
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000591 """
592 results = []
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000593 results.extend(_CommonChecks(input_api, output_api))
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000594 results.extend(
rmistry@google.comc2993442013-01-23 14:35:58 +0000595 _CheckTreeStatus(input_api, output_api, json_url=(
rmistry@google.com547012d2013-04-12 19:45:46 +0000596 SKIA_TREE_STATUS_URL + '/banner-status?format=json')))
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000597 results.extend(_CheckLGTMsForPublicAPI(input_api, output_api))
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000598 results.extend(_CheckOwnerIsInAuthorsFile(input_api, output_api))
Ravi Mistrya70cb8a2017-09-12 13:52:05 -0400599 # Checks for the presence of 'DO NOT''SUBMIT' in CL description and in
600 # content of files.
601 results.extend(
602 input_api.canned_checks.CheckDoNotSubmit(input_api, output_api))
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000603 return results