blob: a1adff36118264b0a24740e22633514d03761a10 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371# Copyright (C) 2014 Google Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7# * Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9# * Redistributions in binary form must reproduce the above
10# copyright notice, this list of conditions and the following disclaimer
11# in the documentation and/or other materials provided with the
12# distribution.
13# * Neither the name of Google Inc. nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Yang Guo75beda92019-10-28 07:29:2528"""
29DevTools presubmit script
Blink Reformat4c46d092018-04-07 15:32:3730
31See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
32for more details about the presubmit API built into gcl.
33"""
34
35import sys
Tim van der Lippef515fdc2020-03-06 16:18:2536import six
Tim van der Lippefb023462020-08-21 13:10:0637import time
Blink Reformat4c46d092018-04-07 15:32:3738
Yang Guoa7845d52019-10-31 10:30:2339EXCLUSIVE_CHANGE_DIRECTORIES = [
Sigurd Schneiderf3a1ecd2021-03-02 14:46:0340 ['third_party', 'v8'],
Tim van der Lippe2b488032021-04-08 16:23:5541 [
42 'node_modules', 'package.json', 'package-lock.json',
43 'scripts/deps/manage_node_deps.py'
44 ],
Tim van der Lippe2e143872021-04-08 11:56:4045 ['OWNERS', 'config/owner'],
Yang Guoa7845d52019-10-31 10:30:2346]
47
Liviu Raufd2e3212019-12-18 15:38:2048AUTOROLL_ACCOUNT = "devtools-ci-autoroll-builder@chops-service-accounts.iam.gserviceaccount.com"
Mathias Bynensa0a6e292019-12-17 12:24:0849
Tim van der Lippe4d004ec2020-03-03 18:32:0150
51def _ExecuteSubProcess(input_api, output_api, script_path, args, results):
Tim van der Lippef515fdc2020-03-06 16:18:2552 if isinstance(script_path, six.string_types):
53 script_path = [input_api.python_executable, script_path]
54
Tim van der Lippefb023462020-08-21 13:10:0655 start_time = time.time()
Sigurd Schneiderf3a1ecd2021-03-02 14:46:0356 process = input_api.subprocess.Popen(script_path + args,
57 stdout=input_api.subprocess.PIPE,
58 stderr=input_api.subprocess.STDOUT)
Tim van der Lippe4d004ec2020-03-03 18:32:0159 out, _ = process.communicate()
Tim van der Lippefb023462020-08-21 13:10:0660 end_time = time.time()
61
62 time_difference = end_time - start_time
63 time_info = "Script execution time was %.1fs seconds\n" % (time_difference)
Tim van der Lippe4d004ec2020-03-03 18:32:0164 if process.returncode != 0:
Tim van der Lippefb023462020-08-21 13:10:0665 results.append(output_api.PresubmitError(time_info + out))
Tim van der Lippe4d004ec2020-03-03 18:32:0166 else:
Tim van der Lippefb023462020-08-21 13:10:0667 results.append(output_api.PresubmitNotifyResult(time_info + out))
Tim van der Lippe4d004ec2020-03-03 18:32:0168 return results
69
70
Yang Guoa7845d52019-10-31 10:30:2371def _CheckChangesAreExclusiveToDirectory(input_api, output_api):
Tim van der Lippebc42a632019-11-28 14:22:5572 if input_api.change.DISABLE_THIRD_PARTY_CHECK != None:
73 return []
Brandon Goddarde7028672020-01-30 17:31:0474 results = [output_api.PresubmitNotifyResult('Directory Exclusivity Check:')]
Sigurd Schneiderf3a1ecd2021-03-02 14:46:0375
Yang Guoa7845d52019-10-31 10:30:2376 def IsParentDir(file, dir):
77 while file != '':
78 if file == dir:
79 return True
80 file = input_api.os_path.dirname(file)
Yang Guoa7845d52019-10-31 10:30:2381 return False
82
83 def FileIsInDir(file, dirs):
84 for dir in dirs:
85 if IsParentDir(file, dir):
86 return True
87
88 affected_files = input_api.LocalPaths()
Yang Guoa7845d52019-10-31 10:30:2389 num_affected = len(affected_files)
90 for dirs in EXCLUSIVE_CHANGE_DIRECTORIES:
Paul Lewis14effba2019-12-02 14:56:4091 dir_list = ', '.join(dirs)
Yang Guoa7845d52019-10-31 10:30:2392 affected_in_dir = filter(lambda f: FileIsInDir(f, dirs), affected_files)
93 num_in_dir = len(affected_in_dir)
94 if num_in_dir == 0:
95 continue
Tim van der Lippeebb94a92019-11-19 17:07:5396 # Addition of new third_party folders must have a new entry in `.gitignore`
97 if '.gitignore' in affected_files:
98 num_in_dir = num_in_dir + 1
Yang Guoa7845d52019-10-31 10:30:2399 if num_in_dir < num_affected:
Brandon Goddarde7028672020-01-30 17:31:04100 results.append(output_api
Paul Lewis14effba2019-12-02 14:56:40101 .PresubmitError(('CLs that affect files in "%s" should be limited to these files/directories.' % dir_list) +
Brandon Goddarde7028672020-01-30 17:31:04102 ' You can disable this check by adding DISABLE_THIRD_PARTY_CHECK=<reason> to your commit message'))
103 break
104
105 return results
Yang Guoa7845d52019-10-31 10:30:23106
Blink Reformat4c46d092018-04-07 15:32:37107
Sigurd Schneider5c9b4f92021-01-22 10:09:55108def _CheckBugAssociation(input_api, output_api, is_committing):
109 results = [output_api.PresubmitNotifyResult('Bug Association Check:')]
110 bugs = input_api.change.BugsFromDescription()
111 message = (
112 "Each CL should be associated with a bug, use \'Bug:\' or \'Fixed:\' lines in\n"
113 "the footer of the commit description. If you explicitly don\'t want to\n"
114 "set a bug, use \'Bug: none\' in the footer of the commit description.\n\n"
115 "Note: The footer of the commit description is the last block of lines in\n"
116 "the commit description that doesn't contain empty lines. This means that\n"
117 "any \'Bug:\' or \'Fixed:\' lines that are eventually followed by an empty\n"
118 "line are not detected by this presubmit check.")
119
120 if not bugs:
121 if is_committing:
122 results.append(output_api.PresubmitError(message))
123 else:
124 results.append(output_api.PresubmitNotifyResult(message))
125
126 for bug in bugs:
127 results.append(output_api.PresubmitNotifyResult(('%s') % bug))
128
129 return results
130
131
Blink Reformat4c46d092018-04-07 15:32:37132def _CheckBuildGN(input_api, output_api):
Brandon Goddarde7028672020-01-30 17:31:04133 results = [output_api.PresubmitNotifyResult('Running BUILD.GN check:')]
Sigurd Schneiderf3a1ecd2021-03-02 14:46:03134 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
135 'scripts', 'check_gn.js')
Brandon Goddarde7028672020-01-30 17:31:04136 results.extend(_checkWithNodeScript(input_api, output_api, script_path))
137 return results
Blink Reformat4c46d092018-04-07 15:32:37138
139
Brandon Goddard33104372020-08-13 15:49:23140def _CheckExperimentTelemetry(input_api, output_api):
Brandon Goddard33104372020-08-13 15:49:23141 experiment_telemetry_files = [
142 input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end',
Jan Schefflerb4eb22d2021-04-05 20:38:36143 'main', 'MainImpl.ts'),
Brandon Goddard33104372020-08-13 15:49:23144 input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end',
Tim van der Lippee0247312021-04-01 14:25:30145 'core', 'host', 'UserMetrics.ts')
Brandon Goddard33104372020-08-13 15:49:23146 ]
147 affected_main_files = _getAffectedFiles(input_api,
148 experiment_telemetry_files, [],
149 ['.js'])
150 if len(affected_main_files) == 0:
Tim van der Lippefb023462020-08-21 13:10:06151 return [
152 output_api.PresubmitNotifyResult(
153 'No affected files for telemetry check')
154 ]
Brandon Goddard33104372020-08-13 15:49:23155
Tim van der Lippefb023462020-08-21 13:10:06156 results = [
157 output_api.PresubmitNotifyResult('Running Experiment Telemetry check:')
158 ]
Brandon Goddard33104372020-08-13 15:49:23159 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
160 'scripts', 'check_experiments.js')
161 results.extend(_checkWithNodeScript(input_api, output_api, script_path))
162 return results
163
164
Tim van der Lippee4bdd742019-12-17 15:40:16165def _CheckJSON(input_api, output_api):
Brandon Goddarde7028672020-01-30 17:31:04166 results = [output_api.PresubmitNotifyResult('Running JSON Validator:')]
Sigurd Schneiderf3a1ecd2021-03-02 14:46:03167 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
168 'scripts', 'json_validator',
169 'validate_module_json.js')
Brandon Goddarde7028672020-01-30 17:31:04170 results.extend(_checkWithNodeScript(input_api, output_api, script_path))
171 return results
Tim van der Lippee4bdd742019-12-17 15:40:16172
173
Blink Reformat4c46d092018-04-07 15:32:37174def _CheckFormat(input_api, output_api):
Sigurd Schneiderf3a1ecd2021-03-02 14:46:03175 node_modules_affected_files = _getAffectedFiles(input_api, [
176 input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules')
177 ], [], [])
Tim van der Lippefdbd42e2020-04-07 14:14:36178
179 # TODO(crbug.com/1068198): Remove once `git cl format --js` can handle large CLs.
180 if (len(node_modules_affected_files) > 0):
181 return [output_api.PresubmitNotifyResult('Skipping Format Checks because `node_modules` files are affected.')]
182
Brandon Goddarde7028672020-01-30 17:31:04183 results = [output_api.PresubmitNotifyResult('Running Format Checks:')]
Blink Reformat4c46d092018-04-07 15:32:37184
Tim van der Lippef515fdc2020-03-06 16:18:25185 return _ExecuteSubProcess(input_api, output_api, ['git', 'cl', 'format', '--js'], [], results)
Blink Reformat4c46d092018-04-07 15:32:37186
Mathias Bynens1b2c5e42020-06-18 06:29:21187def _CheckDevToolsStyleJS(input_api, output_api):
Tim van der Lippefb023462020-08-21 13:10:06188 results = [output_api.PresubmitNotifyResult('JS style check:')]
Mathias Bynens1b2c5e42020-06-18 06:29:21189 lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
190 'scripts', 'test',
191 'run_lint_check_js.js')
Tim van der Lippe4d004ec2020-03-03 18:32:01192
Mathias Bynens1b2c5e42020-06-18 06:29:21193 front_end_directory = input_api.os_path.join(
194 input_api.PresubmitLocalPath(), 'front_end')
Jack Franklinbcfd6ad2021-02-17 10:12:50195 component_docs_directory = input_api.os_path.join(front_end_directory,
196 'component_docs')
Alex Rudenko5556a902020-09-29 09:37:23197 inspector_overlay_directory = input_api.os_path.join(
198 input_api.PresubmitLocalPath(), 'inspector_overlay')
Mathias Bynens1b2c5e42020-06-18 06:29:21199 test_directory = input_api.os_path.join(input_api.PresubmitLocalPath(),
200 'test')
201 scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(),
202 'scripts')
Tim van der Lippe2a4ae2b2020-03-11 17:28:06203
Mathias Bynens1b2c5e42020-06-18 06:29:21204 default_linted_directories = [
Alex Rudenko5556a902020-09-29 09:37:23205 front_end_directory, test_directory, scripts_directory,
206 inspector_overlay_directory
Mathias Bynens1b2c5e42020-06-18 06:29:21207 ]
Tim van der Lippe2a4ae2b2020-03-11 17:28:06208
209 eslint_related_files = [
Mathias Bynens1b2c5e42020-06-18 06:29:21210 input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules',
211 'eslint'),
Tim van der Lippecf4ab402021-02-12 14:30:58212 input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules',
213 '@typescript-eslint'),
Tim van der Lippe2a4ae2b2020-03-11 17:28:06214 input_api.os_path.join(input_api.PresubmitLocalPath(), '.eslintrc.js'),
Mathias Bynens1b2c5e42020-06-18 06:29:21215 input_api.os_path.join(input_api.PresubmitLocalPath(),
216 '.eslintignore'),
Tim van der Lippe33543ac2020-12-14 14:37:45217 input_api.os_path.join(front_end_directory, '.eslintrc.js'),
Jack Franklinbcfd6ad2021-02-17 10:12:50218 input_api.os_path.join(component_docs_directory, '.eslintrc.js'),
Tim van der Lippe406249f2020-12-14 14:59:10219 input_api.os_path.join(test_directory, '.eslintrc.js'),
Mathias Bynens1b2c5e42020-06-18 06:29:21220 input_api.os_path.join(scripts_directory, 'test',
221 'run_lint_check_js.py'),
222 input_api.os_path.join(scripts_directory, 'test',
223 'run_lint_check_js.js'),
Tim van der Lippe2a4ae2b2020-03-11 17:28:06224 input_api.os_path.join(scripts_directory, '.eslintrc.js'),
225 input_api.os_path.join(scripts_directory, 'eslint_rules'),
226 ]
227
Mathias Bynens1b2c5e42020-06-18 06:29:21228 lint_config_files = _getAffectedFiles(input_api, eslint_related_files, [],
229 ['.js', '.py', '.eslintignore'])
Tim van der Lippe2a4ae2b2020-03-11 17:28:06230
Mathias Bynens0ec56612020-06-19 07:14:03231 should_bail_out, files_to_lint = _getFilesToLint(
232 input_api, output_api, lint_config_files, default_linted_directories,
233 ['.js', '.ts'], results)
234 if should_bail_out:
Mathias Bynens1b2c5e42020-06-18 06:29:21235 return results
Tim van der Lippe2a4ae2b2020-03-11 17:28:06236
Mathias Bynens1b2c5e42020-06-18 06:29:21237 results.extend(
238 _checkWithNodeScript(input_api, output_api, lint_path, files_to_lint))
Tim van der Lippe98132242020-04-14 16:16:54239 return results
Blink Reformat4c46d092018-04-07 15:32:37240
241
Mathias Bynens1b2c5e42020-06-18 06:29:21242def _CheckDevToolsStyleCSS(input_api, output_api):
Tim van der Lippefb023462020-08-21 13:10:06243 results = [output_api.PresubmitNotifyResult('CSS style check:')]
Mathias Bynens1b2c5e42020-06-18 06:29:21244 lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
245 'scripts', 'test',
Jack Franklinbc302342021-01-18 10:03:30246 'run_lint_check_css.js')
Mathias Bynens1b2c5e42020-06-18 06:29:21247
248 front_end_directory = input_api.os_path.join(
249 input_api.PresubmitLocalPath(), 'front_end')
Alex Rudenko5556a902020-09-29 09:37:23250 inspector_overlay_directory = input_api.os_path.join(
251 input_api.PresubmitLocalPath(), 'inspector_overlay')
252 default_linted_directories = [
253 front_end_directory, inspector_overlay_directory
254 ]
Mathias Bynens1b2c5e42020-06-18 06:29:21255
256 scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(),
257 'scripts')
258
259 stylelint_related_files = [
260 input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules',
261 'stylelint'),
262 input_api.os_path.join(input_api.PresubmitLocalPath(),
263 '.stylelintrc.json'),
264 input_api.os_path.join(input_api.PresubmitLocalPath(),
265 '.stylelintignore'),
266 input_api.os_path.join(scripts_directory, 'test',
Sigurd Schneider6523c512021-02-12 09:44:28267 'run_lint_check_css.js'),
Mathias Bynens1b2c5e42020-06-18 06:29:21268 ]
269
270 lint_config_files = _getAffectedFiles(input_api, stylelint_related_files,
Sigurd Schneider6523c512021-02-12 09:44:28271 [], [])
Mathias Bynens1b2c5e42020-06-18 06:29:21272
Sigurd Schneidere3bf6c22021-02-11 14:35:23273 css_should_bail_out, css_files_to_lint = _getFilesToLint(
Mathias Bynens0ec56612020-06-19 07:14:03274 input_api, output_api, lint_config_files, default_linted_directories,
275 ['.css'], results)
Mathias Bynens1b2c5e42020-06-18 06:29:21276
Sigurd Schneidere3bf6c22021-02-11 14:35:23277 ts_should_bail_out, ts_files_to_lint = _getFilesToLint(
278 input_api, output_api, lint_config_files, default_linted_directories,
279 ['.ts'], results)
280
Sigurd Schneiderf3a1ecd2021-03-02 14:46:03281 # If there are more than 50 files to check, don't bother and check
282 # everything, so as to not run into command line length limits on Windows.
283 if not css_should_bail_out:
284 if len(css_files_to_lint) < 50:
285 script_args = ["--files"] + css_files_to_lint
286 else:
287 script_args = [] # The defaults check all CSS files.
288 results.extend(
289 _checkWithNodeScript(input_api, output_api, lint_path,
290 script_args))
291
Sigurd Schneidere3bf6c22021-02-11 14:35:23292 if not ts_should_bail_out:
Sigurd Schneiderf3a1ecd2021-03-02 14:46:03293 script_args = ["--syntax", "html"]
294 if len(ts_files_to_lint) < 50:
295 script_args += ["--files"] + ts_files_to_lint
296 else:
297 script_args += ["--glob", "front_end/**/*.ts"]
Sigurd Schneidere3bf6c22021-02-11 14:35:23298 results.extend(
299 _checkWithNodeScript(input_api, output_api, lint_path,
300 script_args))
301
Jack Franklinbc302342021-01-18 10:03:30302 return results
Mathias Bynens1b2c5e42020-06-18 06:29:21303
304
Jack Franklin13812f62021-02-01 15:51:12305def _CheckDarkModeStyleSheetsUpToDate(input_api, output_api):
306 devtools_root = input_api.PresubmitLocalPath()
307 devtools_front_end = input_api.os_path.join(devtools_root, 'front_end')
308 affected_css_files = _getAffectedFiles(input_api, [devtools_front_end], [],
309 ['.css'])
310 results = [output_api.PresubmitNotifyResult('Dark Mode CSS check:')]
311 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
312 'scripts', 'dark_mode',
313 'check_darkmode_css_up_to_date.js')
314 results.extend(
315 _checkWithNodeScript(input_api, output_api, script_path,
316 affected_css_files))
317 return results
318
319
Joel Einbinderf6f86b62019-06-10 23:19:12320def _CheckOptimizeSVGHashes(input_api, output_api):
Tim van der Lippefb023462020-08-21 13:10:06321 if not input_api.platform.startswith('linux'):
322 return [output_api.PresubmitNotifyResult('Skipping SVG hash check')]
323
Mathias Bynens1b2c5e42020-06-18 06:29:21324 results = [
325 output_api.PresubmitNotifyResult('Running SVG optimization check:')
326 ]
Blink Reformat4c46d092018-04-07 15:32:37327
328 original_sys_path = sys.path
329 try:
330 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build')]
331 import devtools_file_hashes
332 finally:
333 sys.path = original_sys_path
334
335 absolute_local_paths = [af.AbsoluteLocalPath() for af in input_api.AffectedFiles(include_deletes=False)]
Yang Guo75beda92019-10-28 07:29:25336 images_src_path = input_api.os_path.join('devtools', 'front_end', 'Images', 'src')
337 image_source_file_paths = [path for path in absolute_local_paths if images_src_path in path and path.endswith('.svg')]
338 image_sources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', 'Images', 'src')
339 hashes_file_name = 'optimize_svg.hashes'
Blink Reformat4c46d092018-04-07 15:32:37340 hashes_file_path = input_api.os_path.join(image_sources_path, hashes_file_name)
341 invalid_hash_file_paths = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, image_source_file_paths)
342 if len(invalid_hash_file_paths) == 0:
Brandon Goddarde7028672020-01-30 17:31:04343 return results
Blink Reformat4c46d092018-04-07 15:32:37344 invalid_hash_file_names = [input_api.os_path.basename(file_path) for file_path in invalid_hash_file_paths]
Yang Guo75beda92019-10-28 07:29:25345 file_paths_str = ', '.join(invalid_hash_file_names)
346 error_message = 'The following SVG files should be optimized using optimize_svg_images script before uploading: \n - %s' % file_paths_str
Brandon Goddarde7028672020-01-30 17:31:04347 results.append(output_api.PresubmitError(error_message))
348 return results
Blink Reformat4c46d092018-04-07 15:32:37349
350
Mathias Bynens032591d2019-10-21 09:51:31351
Tim van der Lippe4d004ec2020-03-03 18:32:01352def _CheckGeneratedFiles(input_api, output_api):
Tim van der Lippeb3b90762020-03-04 15:21:52353 v8_directory_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'v8')
354 blink_directory_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'blink')
355 protocol_location = input_api.os_path.join(blink_directory_path, 'public', 'devtools_protocol')
356 scripts_build_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build')
Tim van der Lippe5d2d79b2020-03-23 11:45:04357 scripts_generated_output_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', 'generated')
Tim van der Lippeb3b90762020-03-04 15:21:52358
359 generated_aria_path = input_api.os_path.join(scripts_build_path, 'generate_aria.py')
360 generated_supported_css_path = input_api.os_path.join(scripts_build_path, 'generate_supported_css.py')
361 generated_protocol_path = input_api.os_path.join(scripts_build_path, 'code_generator_frontend.py')
362 concatenate_protocols_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'inspector_protocol',
363 'concatenate_protocols.py')
364
365 affected_files = _getAffectedFiles(input_api, [
366 v8_directory_path,
367 blink_directory_path,
368 input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'pyjson5'),
369 generated_aria_path,
370 generated_supported_css_path,
371 concatenate_protocols_path,
372 generated_protocol_path,
Tim van der Lippe5d2d79b2020-03-23 11:45:04373 scripts_generated_output_path,
374 ], [], ['.pdl', '.json5', '.py', '.js'])
Tim van der Lippeb3b90762020-03-04 15:21:52375
376 if len(affected_files) == 0:
Tim van der Lippefb023462020-08-21 13:10:06377 return [
378 output_api.PresubmitNotifyResult(
379 'No affected files for generated files check')
380 ]
Tim van der Lippeb3b90762020-03-04 15:21:52381
Tim van der Lippe4d004ec2020-03-03 18:32:01382 results = [output_api.PresubmitNotifyResult('Running Generated Files Check:')]
Tim van der Lippeb0d65f12020-03-05 12:15:24383 generate_protocol_resources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'deps',
384 'generate_protocol_resources.py')
Tim van der Lippe4d004ec2020-03-03 18:32:01385
Tim van der Lippeb0d65f12020-03-05 12:15:24386 return _ExecuteSubProcess(input_api, output_api, generate_protocol_resources_path, [], results)
Tim van der Lippe4d004ec2020-03-03 18:32:01387
388
Christy Chen2d6d9a62020-09-22 16:04:09389def _CollectStrings(input_api, output_api):
390 devtools_root = input_api.PresubmitLocalPath()
391 devtools_front_end = input_api.os_path.join(devtools_root, 'front_end')
392 affected_front_end_files = _getAffectedFiles(input_api,
Peter Marshall1d952dc2021-02-10 12:49:32393 [devtools_front_end], [],
Tim van der Lippec50df852021-01-19 15:15:52394 ['.js', '.ts'])
Christy Chen2d6d9a62020-09-22 16:04:09395 if len(affected_front_end_files) == 0:
396 return [
397 output_api.PresubmitNotifyResult(
398 'No affected files to run collect-strings')
399 ]
400
401 results = [
402 output_api.PresubmitNotifyResult('Collecting strings from front_end:')
403 ]
404 script_path = input_api.os_path.join(devtools_root, 'third_party', 'i18n',
405 'collect-strings.js')
406 results.extend(_checkWithNodeScript(input_api, output_api, script_path))
407 results.append(
408 output_api.PresubmitNotifyResult(
Peter Marshalld67e9f12021-02-08 09:34:35409 'Please commit en-US.json/en-XL.json if changes are generated.'))
Christy Chen2d6d9a62020-09-22 16:04:09410 return results
411
412
Tim van der Lippe5279f842020-01-14 16:26:38413def _CheckNoUncheckedFiles(input_api, output_api):
414 results = []
415 process = input_api.subprocess.Popen(['git', 'diff', '--exit-code'],
416 stdout=input_api.subprocess.PIPE,
417 stderr=input_api.subprocess.STDOUT)
418 out, _ = process.communicate()
419 if process.returncode != 0:
Jack Franklin324f3042020-09-03 10:28:29420 files_changed_process = input_api.subprocess.Popen(
421 ['git', 'diff', '--name-only'],
422 stdout=input_api.subprocess.PIPE,
423 stderr=input_api.subprocess.STDOUT)
Tim van der Lippe9bb1cf62020-03-06 16:17:02424 files_changed, _ = files_changed_process.communicate()
425
426 return [
427 output_api.PresubmitError('You have changed files that need to be committed:'),
428 output_api.PresubmitError(files_changed)
429 ]
Tim van der Lippe5279f842020-01-14 16:26:38430 return []
431
Tim van der Lippe8fdda112020-01-27 11:27:06432def _CheckForTooLargeFiles(input_api, output_api):
Christy Chen1ab87e02020-01-31 00:32:16433 """Avoid large files, especially binary files, in the repository since
Tim van der Lippe8fdda112020-01-27 11:27:06434 git doesn't scale well for those. They will be in everyone's repo
435 clones forever, forever making Chromium slower to clone and work
436 with."""
Christy Chen1ab87e02020-01-31 00:32:16437 # Uploading files to cloud storage is not trivial so we don't want
438 # to set the limit too low, but the upper limit for "normal" large
439 # files seems to be 1-2 MB, with a handful around 5-8 MB, so
440 # anything over 20 MB is exceptional.
441 TOO_LARGE_FILE_SIZE_LIMIT = 20 * 1024 * 1024 # 10 MB
442 too_large_files = []
443 for f in input_api.AffectedFiles():
444 # Check both added and modified files (but not deleted files).
445 if f.Action() in ('A', 'M'):
446 size = input_api.os_path.getsize(f.AbsoluteLocalPath())
447 if size > TOO_LARGE_FILE_SIZE_LIMIT:
448 too_large_files.append("%s: %d bytes" % (f.LocalPath(), size))
449 if too_large_files:
450 message = (
451 'Do not commit large files to git since git scales badly for those.\n' +
452 'Instead put the large files in cloud storage and use DEPS to\n' +
453 'fetch them.\n' + '\n'.join(too_large_files)
454 )
455 return [output_api.PresubmitError(
456 'Too large files found in commit', long_text=message + '\n')]
457 else:
458 return []
Tim van der Lippe8fdda112020-01-27 11:27:06459
Tim van der Lippe5279f842020-01-14 16:26:38460
Tim van der Lippef8a87092020-09-14 12:01:18461def _RunCannedChecks(input_api, output_api):
462 results = []
463 results.extend(
464 input_api.canned_checks.CheckOwnersFormat(input_api, output_api))
465 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
466 results.extend(
467 input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(
468 input_api, output_api))
469 results.extend(
470 input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
471 input_api, output_api))
472 results.extend(
473 input_api.canned_checks.CheckGenderNeutral(input_api, output_api))
474 return results
475
476
Yang Guo4fd355c2019-09-19 08:59:03477def _CommonChecks(input_api, output_api):
Mathias Bynens032591d2019-10-21 09:51:31478 """Checks common to both upload and commit."""
479 results = []
Mathias Bynens011b0072020-08-05 08:17:35480 results.extend(
481 input_api.canned_checks.CheckAuthorizedAuthor(
482 input_api, output_api, bot_allowlist=[AUTOROLL_ACCOUNT]))
Blink Reformat4c46d092018-04-07 15:32:37483 results.extend(_CheckBuildGN(input_api, output_api))
Brandon Goddard33104372020-08-13 15:49:23484 results.extend(_CheckExperimentTelemetry(input_api, output_api))
Tim van der Lippe4d004ec2020-03-03 18:32:01485 results.extend(_CheckGeneratedFiles(input_api, output_api))
Tim van der Lippee4bdd742019-12-17 15:40:16486 results.extend(_CheckJSON(input_api, output_api))
Mathias Bynens1b2c5e42020-06-18 06:29:21487 results.extend(_CheckDevToolsStyleJS(input_api, output_api))
488 results.extend(_CheckDevToolsStyleCSS(input_api, output_api))
Jack Franklinb10193f2021-03-19 10:25:08489
Jack Franklind34cf332021-03-24 10:27:20490 results.extend(_CheckDarkModeStyleSheetsUpToDate(input_api, output_api))
Tim van der Lippe5497d482020-01-14 15:27:30491 results.extend(_CheckFormat(input_api, output_api))
Joel Einbinderf6f86b62019-06-10 23:19:12492 results.extend(_CheckOptimizeSVGHashes(input_api, output_api))
Yang Guoa7845d52019-10-31 10:30:23493 results.extend(_CheckChangesAreExclusiveToDirectory(input_api, output_api))
Peter Marshallcd845512021-01-28 14:29:21494 results.extend(_CheckI18nWasBundled(input_api, output_api))
Tim van der Lippef8a87092020-09-14 12:01:18495 # Run the canned checks from `depot_tools` after the custom DevTools checks.
496 # The canned checks for example check that lines have line endings. The
497 # DevTools presubmit checks automatically fix these issues. If we would run
498 # the canned checks before the DevTools checks, they would erroneously conclude
499 # that there are issues in the code. Since the canned checks are allowed to be
500 # ignored, a confusing message is shown that asks if the failed presubmit can
501 # be continued regardless. By fixing the issues before we reach the canned checks,
502 # we don't show the message to suppress these errors, which would otherwise be
503 # causing CQ to fail.
504 results.extend(_RunCannedChecks(input_api, output_api))
Kalon Hindsd44fddf2020-12-10 13:43:25505 return results
506
507
508def _SideEffectChecks(input_api, output_api):
509 """Check side effects caused by other checks"""
510 results = []
Tim van der Lippe5279f842020-01-14 16:26:38511 results.extend(_CheckNoUncheckedFiles(input_api, output_api))
Tim van der Lippe8fdda112020-01-27 11:27:06512 results.extend(_CheckForTooLargeFiles(input_api, output_api))
Blink Reformat4c46d092018-04-07 15:32:37513 return results
514
515
Liviu Raud614e092020-01-08 09:56:33516def CheckChangeOnUpload(input_api, output_api):
517 results = []
518 results.extend(_CommonChecks(input_api, output_api))
Christy Chen2d6d9a62020-09-22 16:04:09519 results.extend(_CollectStrings(input_api, output_api))
Kalon Hindsd44fddf2020-12-10 13:43:25520 # Run checks that rely on output from other DevTool checks
521 results.extend(_SideEffectChecks(input_api, output_api))
Sigurd Schneider5c9b4f92021-01-22 10:09:55522 results.extend(_CheckBugAssociation(input_api, output_api, False))
Liviu Raud614e092020-01-08 09:56:33523 return results
524
525
Blink Reformat4c46d092018-04-07 15:32:37526def CheckChangeOnCommit(input_api, output_api):
Mandy Chenf0fbdbe2019-08-22 23:58:37527 results = []
Yang Guo4fd355c2019-09-19 08:59:03528 results.extend(_CommonChecks(input_api, output_api))
Christy Chen2d6d9a62020-09-22 16:04:09529 results.extend(_CollectStrings(input_api, output_api))
Kalon Hindsd44fddf2020-12-10 13:43:25530 # Run checks that rely on output from other DevTool checks
531 results.extend(_SideEffectChecks(input_api, output_api))
Mathias Bynens032591d2019-10-21 09:51:31532 results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api))
Sigurd Schneider5c9b4f92021-01-22 10:09:55533 results.extend(_CheckBugAssociation(input_api, output_api, True))
Mandy Chenf0fbdbe2019-08-22 23:58:37534 return results
Blink Reformat4c46d092018-04-07 15:32:37535
536
Mandy Chena6be46a2019-07-09 17:06:27537def _getAffectedFiles(input_api, parent_directories, excluded_actions, accepted_endings): # pylint: disable=invalid-name
Yang Guo75beda92019-10-28 07:29:25538 """Return absolute file paths of affected files (not due to an excluded action)
Mandy Chena6be46a2019-07-09 17:06:27539 under a parent directory with an accepted file ending.
Yang Guo75beda92019-10-28 07:29:25540 """
Mandy Chena6be46a2019-07-09 17:06:27541 local_paths = [
542 f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if all(f.Action() != action for action in excluded_actions)
543 ]
544 affected_files = [
Tim van der Lippefdbd42e2020-04-07 14:14:36545 file_name for file_name in local_paths if any(parent_directory in file_name for parent_directory in parent_directories) and
546 (len(accepted_endings) is 0 or any(file_name.endswith(accepted_ending) for accepted_ending in accepted_endings))
Mandy Chena6be46a2019-07-09 17:06:27547 ]
548 return affected_files
549
550
Tim van der Lippec4617122020-03-06 16:24:19551def _checkWithNodeScript(input_api, output_api, script_path, script_arguments=[]): # pylint: disable=invalid-name
Blink Reformat4c46d092018-04-07 15:32:37552 original_sys_path = sys.path
553 try:
Yang Guo75beda92019-10-28 07:29:25554 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')]
Yang Guod8176982019-10-04 20:30:35555 import devtools_paths
Blink Reformat4c46d092018-04-07 15:32:37556 finally:
557 sys.path = original_sys_path
558
Tim van der Lippec4617122020-03-06 16:24:19559 return _ExecuteSubProcess(input_api, output_api, [devtools_paths.node_path(), script_path], script_arguments, [])
Mathias Bynens1b2c5e42020-06-18 06:29:21560
561
Jack Franklin324f3042020-09-03 10:28:29562def _checkWithTypeScript(input_api,
563 output_api,
564 tsc_arguments,
565 script_path,
566 script_arguments=[]): # pylint: disable=invalid-name
567 original_sys_path = sys.path
568 try:
569 sys.path = sys.path + [
570 input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')
571 ]
572 import devtools_paths
573 finally:
574 sys.path = original_sys_path
575
576 # First run tsc to compile the TS script that we then run in the _ExecuteSubProcess call
577 tsc_compiler_process = input_api.subprocess.Popen(
578 [
579 devtools_paths.node_path(),
580 devtools_paths.typescript_compiler_path()
581 ] + tsc_arguments,
582 stdout=input_api.subprocess.PIPE,
583 stderr=input_api.subprocess.STDOUT)
584
585 out, _ = tsc_compiler_process.communicate()
586 if tsc_compiler_process.returncode != 0:
587 return [
588 output_api.PresubmitError('Error compiling briges regenerator:\n' +
589 str(out))
590 ]
591
592 return _checkWithNodeScript(input_api, output_api, script_path,
593 script_arguments)
594
595
Mathias Bynens1b2c5e42020-06-18 06:29:21596def _getFilesToLint(input_api, output_api, lint_config_files,
597 default_linted_directories, accepted_endings, results):
Mathias Bynens0ec56612020-06-19 07:14:03598 run_full_check = False
Mathias Bynens1b2c5e42020-06-18 06:29:21599 files_to_lint = []
600
601 # We are changing the lint configuration; run the full check.
602 if len(lint_config_files) is not 0:
603 results.append(
604 output_api.PresubmitNotifyResult('Running full lint check'))
Mathias Bynens0ec56612020-06-19 07:14:03605 run_full_check = True
Mathias Bynens1b2c5e42020-06-18 06:29:21606 else:
607 # Only run the linter on files that are relevant, to save PRESUBMIT time.
608 files_to_lint = _getAffectedFiles(input_api,
609 default_linted_directories, ['D'],
610 accepted_endings)
611
Paul Lewis2b9224f2020-09-08 17:13:10612 # Exclude front_end/third_party files.
613 files_to_lint = filter(lambda path: "third_party" not in path,
614 files_to_lint)
615
Mathias Bynens1b2c5e42020-06-18 06:29:21616 if len(files_to_lint) is 0:
617 results.append(
618 output_api.PresubmitNotifyResult(
619 'No affected files for lint check'))
620
Mathias Bynens0ec56612020-06-19 07:14:03621 should_bail_out = len(files_to_lint) is 0 and not run_full_check
622 return should_bail_out, files_to_lint
Peter Marshallcd845512021-01-28 14:29:21623
624
625def _CheckI18nWasBundled(input_api, output_api):
626 affected_files = _getAffectedFiles(input_api, [
627 input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end',
628 'third_party', 'i18n', 'lib')
629 ], [], ['.js'])
630
631 if len(affected_files) == 0:
632 return [
633 output_api.PresubmitNotifyResult(
634 'No affected files for i18n bundle check')
635 ]
636
637 results = [output_api.PresubmitNotifyResult('Running buildi18nBundle.js:')]
638 script_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
639 'scripts', 'localizationV2',
640 'buildi18nBundle.js')
641 results.extend(_checkWithNodeScript(input_api, output_api, script_path))
642 return results