Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | # 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 Guo | 75beda9 | 2019-10-28 07:29:25 | [diff] [blame] | 28 | """ |
| 29 | DevTools presubmit script |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 30 | |
| 31 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 32 | for more details about the presubmit API built into gcl. |
| 33 | """ |
| 34 | |
| 35 | import sys |
Tim van der Lippe | f515fdc | 2020-03-06 16:18:25 | [diff] [blame] | 36 | import six |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 37 | import time |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 38 | |
Liviu Rau | fd2e321 | 2019-12-18 15:38:20 | [diff] [blame] | 39 | AUTOROLL_ACCOUNT = "devtools-ci-autoroll-builder@chops-service-accounts.iam.gserviceaccount.com" |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 40 | USE_PYTHON3 = True |
Mathias Bynens | a0a6e29 | 2019-12-17 12:24:08 | [diff] [blame] | 41 | |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 42 | |
| 43 | def _ExecuteSubProcess(input_api, output_api, script_path, args, results): |
Tim van der Lippe | f515fdc | 2020-03-06 16:18:25 | [diff] [blame] | 44 | if isinstance(script_path, six.string_types): |
Philip Pfaffe | f4320aa | 2022-07-21 11:33:24 | [diff] [blame] | 45 | script_path = [input_api.python3_executable, script_path] |
Tim van der Lippe | f515fdc | 2020-03-06 16:18:25 | [diff] [blame] | 46 | |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 47 | start_time = time.time() |
Sigurd Schneider | f3a1ecd | 2021-03-02 14:46:03 | [diff] [blame] | 48 | process = input_api.subprocess.Popen(script_path + args, |
| 49 | stdout=input_api.subprocess.PIPE, |
| 50 | stderr=input_api.subprocess.STDOUT) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 51 | out, _ = process.communicate() |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 52 | end_time = time.time() |
| 53 | |
| 54 | time_difference = end_time - start_time |
| 55 | time_info = "Script execution time was %.1fs seconds\n" % (time_difference) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 56 | if process.returncode != 0: |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 57 | results.append( |
| 58 | output_api.PresubmitError(time_info + out.decode('utf-8'))) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 59 | else: |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 60 | results.append( |
| 61 | output_api.PresubmitNotifyResult(time_info + out.decode('utf-8'))) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 62 | return results |
| 63 | |
| 64 | |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 65 | def _CheckChangesAreExclusiveToDirectory(input_api, output_api): |
Tim van der Lippe | bc42a63 | 2019-11-28 14:22:55 | [diff] [blame] | 66 | if input_api.change.DISABLE_THIRD_PARTY_CHECK != None: |
| 67 | return [] |
Saba Khukhunashvili | 0cdc5a9 | 2022-07-15 19:10:37 | [diff] [blame] | 68 | |
Brandon Goddard | e702867 | 2020-01-30 17:31:04 | [diff] [blame] | 69 | results = [output_api.PresubmitNotifyResult('Directory Exclusivity Check:')] |
Sigurd Schneider | f3a1ecd | 2021-03-02 14:46:03 | [diff] [blame] | 70 | |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 71 | def IsParentDir(file, dir): |
| 72 | while file != '': |
| 73 | if file == dir: |
| 74 | return True |
| 75 | file = input_api.os_path.dirname(file) |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 76 | return False |
| 77 | |
| 78 | def FileIsInDir(file, dirs): |
Tim van der Lippe | c2756dc | 2021-12-08 12:17:32 | [diff] [blame] | 79 | if file.endswith('OWNERS') and 'OWNERS' in dirs: |
| 80 | return True |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 81 | for dir in dirs: |
| 82 | if IsParentDir(file, dir): |
| 83 | return True |
| 84 | |
Tim van der Lippe | 4050a30 | 2021-04-13 09:21:21 | [diff] [blame] | 85 | EXCLUSIVE_CHANGE_DIRECTORIES = [ |
| 86 | [ |
| 87 | 'third_party', 'v8', |
Saba Khukhunashvili | 0cdc5a9 | 2022-07-15 19:10:37 | [diff] [blame] | 88 | input_api.os_path.join('front_end', 'models', |
| 89 | 'javascript_metadata'), |
Tim van der Lippe | 4050a30 | 2021-04-13 09:21:21 | [diff] [blame] | 90 | input_api.os_path.join('front_end', 'generated') |
| 91 | ], |
| 92 | [ |
| 93 | 'node_modules', |
Tim van der Lippe | 4050a30 | 2021-04-13 09:21:21 | [diff] [blame] | 94 | 'package-lock.json', |
| 95 | input_api.os_path.join('scripts', 'deps', 'manage_node_deps.py'), |
| 96 | ], |
Tim van der Lippe | c2756dc | 2021-12-08 12:17:32 | [diff] [blame] | 97 | ['OWNERS'], |
Tim van der Lippe | 4050a30 | 2021-04-13 09:21:21 | [diff] [blame] | 98 | ] |
| 99 | |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 100 | affected_files = input_api.LocalPaths() |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 101 | num_affected = len(affected_files) |
| 102 | for dirs in EXCLUSIVE_CHANGE_DIRECTORIES: |
Paul Lewis | 14effba | 2019-12-02 14:56:40 | [diff] [blame] | 103 | dir_list = ', '.join(dirs) |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 104 | affected_in_dir = [ |
| 105 | file for file in affected_files if FileIsInDir(file, dirs) |
| 106 | ] |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 107 | num_in_dir = len(affected_in_dir) |
| 108 | if num_in_dir == 0: |
| 109 | continue |
Tim van der Lippe | ebb94a9 | 2019-11-19 17:07:53 | [diff] [blame] | 110 | # Addition of new third_party folders must have a new entry in `.gitignore` |
| 111 | if '.gitignore' in affected_files: |
| 112 | num_in_dir = num_in_dir + 1 |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 113 | if num_in_dir < num_affected: |
Tim van der Lippe | 239963b | 2021-04-09 09:43:38 | [diff] [blame] | 114 | unexpected_files = [ |
| 115 | file for file in affected_files if file not in affected_in_dir |
| 116 | ] |
| 117 | results.append( |
| 118 | output_api.PresubmitError( |
| 119 | ('CLs that affect files in "%s" should be limited to these files/directories.' |
| 120 | % dir_list) + |
| 121 | ('\nUnexpected files: %s.' % unexpected_files) + |
| 122 | '\nYou can disable this check by adding DISABLE_THIRD_PARTY_CHECK=<reason> to your commit message' |
| 123 | )) |
Brandon Goddard | e702867 | 2020-01-30 17:31:04 | [diff] [blame] | 124 | break |
| 125 | |
| 126 | return results |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 127 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 128 | |
Sigurd Schneider | 5c9b4f9 | 2021-01-22 10:09:55 | [diff] [blame] | 129 | def _CheckBugAssociation(input_api, output_api, is_committing): |
| 130 | results = [output_api.PresubmitNotifyResult('Bug Association Check:')] |
| 131 | bugs = input_api.change.BugsFromDescription() |
| 132 | message = ( |
| 133 | "Each CL should be associated with a bug, use \'Bug:\' or \'Fixed:\' lines in\n" |
| 134 | "the footer of the commit description. If you explicitly don\'t want to\n" |
| 135 | "set a bug, use \'Bug: none\' in the footer of the commit description.\n\n" |
| 136 | "Note: The footer of the commit description is the last block of lines in\n" |
| 137 | "the commit description that doesn't contain empty lines. This means that\n" |
| 138 | "any \'Bug:\' or \'Fixed:\' lines that are eventually followed by an empty\n" |
| 139 | "line are not detected by this presubmit check.") |
| 140 | |
| 141 | if not bugs: |
| 142 | if is_committing: |
| 143 | results.append(output_api.PresubmitError(message)) |
| 144 | else: |
| 145 | results.append(output_api.PresubmitNotifyResult(message)) |
| 146 | |
| 147 | for bug in bugs: |
| 148 | results.append(output_api.PresubmitNotifyResult(('%s') % bug)) |
| 149 | |
| 150 | return results |
| 151 | |
| 152 | |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 153 | def _CheckExperimentTelemetry(input_api, output_api): |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 154 | experiment_telemetry_files = [ |
| 155 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', |
Christy Chen | ab9a44d | 2021-07-02 19:54:30 | [diff] [blame] | 156 | 'entrypoints', 'main', 'MainImpl.ts'), |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 157 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', |
Tim van der Lippe | e024731 | 2021-04-01 14:25:30 | [diff] [blame] | 158 | 'core', 'host', 'UserMetrics.ts') |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 159 | ] |
| 160 | affected_main_files = _getAffectedFiles(input_api, |
| 161 | experiment_telemetry_files, [], |
Christy Chen | ab9a44d | 2021-07-02 19:54:30 | [diff] [blame] | 162 | ['.ts']) |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 163 | if len(affected_main_files) == 0: |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 164 | return [ |
| 165 | output_api.PresubmitNotifyResult( |
| 166 | 'No affected files for telemetry check') |
| 167 | ] |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 168 | |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 169 | results = [ |
| 170 | output_api.PresubmitNotifyResult('Running Experiment Telemetry check:') |
| 171 | ] |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 172 | script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 173 | 'scripts', 'check_experiments.js') |
| 174 | results.extend(_checkWithNodeScript(input_api, output_api, script_path)) |
| 175 | return results |
| 176 | |
| 177 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 178 | def _CheckFormat(input_api, output_api): |
Sigurd Schneider | f3a1ecd | 2021-03-02 14:46:03 | [diff] [blame] | 179 | node_modules_affected_files = _getAffectedFiles(input_api, [ |
Tim van der Lippe | 6509dba | 2021-11-23 16:19:23 | [diff] [blame] | 180 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules'), |
| 181 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', |
| 182 | 'third_party') |
Sigurd Schneider | f3a1ecd | 2021-03-02 14:46:03 | [diff] [blame] | 183 | ], [], []) |
Tim van der Lippe | fdbd42e | 2020-04-07 14:14:36 | [diff] [blame] | 184 | |
| 185 | # TODO(crbug.com/1068198): Remove once `git cl format --js` can handle large CLs. |
| 186 | if (len(node_modules_affected_files) > 0): |
Tim van der Lippe | 6509dba | 2021-11-23 16:19:23 | [diff] [blame] | 187 | return [ |
| 188 | output_api.PresubmitNotifyResult( |
| 189 | 'Skipping Format Checks because `node_modules`/`front_end/third_party` files are affected.' |
| 190 | ) |
| 191 | ] |
Tim van der Lippe | fdbd42e | 2020-04-07 14:14:36 | [diff] [blame] | 192 | |
Brandon Goddard | e702867 | 2020-01-30 17:31:04 | [diff] [blame] | 193 | results = [output_api.PresubmitNotifyResult('Running Format Checks:')] |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 194 | |
Tim van der Lippe | f515fdc | 2020-03-06 16:18:25 | [diff] [blame] | 195 | return _ExecuteSubProcess(input_api, output_api, ['git', 'cl', 'format', '--js'], [], results) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 196 | |
Jack Franklin | 1aa212d | 2021-09-10 14:20:08 | [diff] [blame] | 197 | |
| 198 | def _CheckDevToolsRunESLintTests(input_api, output_api): |
| 199 | # Check for changes in the eslint_rules directory, and run the eslint rules |
| 200 | # tests if so. |
| 201 | # We don't do this on every CL as most do not touch the rules, but if we do |
| 202 | # change them we need to make sure all the tests are passing. |
Jack Franklin | 03db63a | 2021-09-16 13:40:56 | [diff] [blame] | 203 | original_sys_path = sys.path |
| 204 | try: |
| 205 | sys.path = sys.path + [ |
| 206 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts') |
| 207 | ] |
| 208 | import devtools_paths |
| 209 | finally: |
| 210 | sys.path = original_sys_path |
Jack Franklin | 1aa212d | 2021-09-10 14:20:08 | [diff] [blame] | 211 | eslint_rules_dir_path = input_api.os_path.join( |
| 212 | input_api.PresubmitLocalPath(), 'scripts', 'eslint_rules') |
| 213 | eslint_rules_affected_files = _getAffectedFiles(input_api, |
| 214 | [eslint_rules_dir_path], |
| 215 | [], []) |
| 216 | |
| 217 | if (len(eslint_rules_affected_files) == 0): |
| 218 | return [] |
| 219 | |
Jack Franklin | 03db63a | 2021-09-16 13:40:56 | [diff] [blame] | 220 | mocha_path = devtools_paths.mocha_path() |
Jack Franklin | 1aa212d | 2021-09-10 14:20:08 | [diff] [blame] | 221 | eslint_tests_path = input_api.os_path.join(eslint_rules_dir_path, 'tests', |
| 222 | '*_test.js') |
| 223 | |
| 224 | results = [output_api.PresubmitNotifyResult('ESLint rules unit tests')] |
| 225 | results.extend( |
| 226 | # The dot reporter is more concise which is useful to not get LOADS of |
| 227 | # output when just one test fails. |
| 228 | _checkWithNodeScript(input_api, output_api, mocha_path, |
| 229 | ['--reporter', 'dot', eslint_tests_path])) |
| 230 | return results |
| 231 | |
| 232 | |
Tim van der Lippe | 800d875 | 2022-02-04 12:49:56 | [diff] [blame] | 233 | def _CheckDevToolsRunBuildTests(input_api, output_api): |
| 234 | # Check for changes in the build/tests directory, and run the tests if so. |
| 235 | # We don't do this on every CL as most do not touch the rules, but if we do |
| 236 | # change them we need to make sure all the tests are passing. |
| 237 | original_sys_path = sys.path |
| 238 | try: |
| 239 | sys.path = sys.path + [ |
| 240 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts') |
| 241 | ] |
| 242 | import devtools_paths |
| 243 | finally: |
| 244 | sys.path = original_sys_path |
| 245 | scripts_build_dir_path = input_api.os_path.join( |
| 246 | input_api.PresubmitLocalPath(), 'scripts', 'build') |
| 247 | scripts_build_affected_files = _getAffectedFiles(input_api, |
| 248 | [scripts_build_dir_path], |
| 249 | [], []) |
| 250 | |
| 251 | if len(scripts_build_affected_files) == 0: |
| 252 | return [] |
| 253 | |
| 254 | mocha_path = devtools_paths.mocha_path() |
| 255 | build_tests_path = input_api.os_path.join(scripts_build_dir_path, 'tests', |
| 256 | '*_test.js') |
| 257 | |
| 258 | results = [output_api.PresubmitNotifyResult('Build plugins unit tests')] |
| 259 | results.extend( |
| 260 | # The dot reporter is more concise which is useful to not get LOADS of |
| 261 | # output when just one test fails. |
| 262 | _checkWithNodeScript(input_api, output_api, mocha_path, |
| 263 | ['--reporter', 'dot', build_tests_path])) |
| 264 | return results |
| 265 | |
| 266 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 267 | def _CheckDevToolsStyleJS(input_api, output_api): |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 268 | results = [output_api.PresubmitNotifyResult('JS style check:')] |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 269 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 270 | 'scripts', 'test', |
Tim van der Lippe | f9e565e | 2021-11-08 16:22:11 | [diff] [blame] | 271 | 'run_lint_check_js.mjs') |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 272 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 273 | front_end_directory = input_api.os_path.join( |
| 274 | input_api.PresubmitLocalPath(), 'front_end') |
Jack Franklin | bcfd6ad | 2021-02-17 10:12:50 | [diff] [blame] | 275 | component_docs_directory = input_api.os_path.join(front_end_directory, |
Tim van der Lippe | e622f55 | 2021-04-14 14:15:18 | [diff] [blame] | 276 | 'ui', 'components', |
| 277 | 'docs') |
Alex Rudenko | 5556a90 | 2020-09-29 09:37:23 | [diff] [blame] | 278 | inspector_overlay_directory = input_api.os_path.join( |
| 279 | input_api.PresubmitLocalPath(), 'inspector_overlay') |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 280 | test_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 281 | 'test') |
| 282 | scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 283 | 'scripts') |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 284 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 285 | default_linted_directories = [ |
Alex Rudenko | 5556a90 | 2020-09-29 09:37:23 | [diff] [blame] | 286 | front_end_directory, test_directory, scripts_directory, |
| 287 | inspector_overlay_directory |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 288 | ] |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 289 | |
| 290 | eslint_related_files = [ |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 291 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules', |
| 292 | 'eslint'), |
Tim van der Lippe | cf4ab40 | 2021-02-12 14:30:58 | [diff] [blame] | 293 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules', |
| 294 | '@typescript-eslint'), |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 295 | input_api.os_path.join(input_api.PresubmitLocalPath(), '.eslintrc.js'), |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 296 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 297 | '.eslintignore'), |
Tim van der Lippe | 33543ac | 2020-12-14 14:37:45 | [diff] [blame] | 298 | input_api.os_path.join(front_end_directory, '.eslintrc.js'), |
Jack Franklin | bcfd6ad | 2021-02-17 10:12:50 | [diff] [blame] | 299 | input_api.os_path.join(component_docs_directory, '.eslintrc.js'), |
Tim van der Lippe | 406249f | 2020-12-14 14:59:10 | [diff] [blame] | 300 | input_api.os_path.join(test_directory, '.eslintrc.js'), |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 301 | input_api.os_path.join(scripts_directory, 'test', |
| 302 | 'run_lint_check_js.py'), |
| 303 | input_api.os_path.join(scripts_directory, 'test', |
Tim van der Lippe | f9e565e | 2021-11-08 16:22:11 | [diff] [blame] | 304 | 'run_lint_check_js.mjs'), |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 305 | input_api.os_path.join(scripts_directory, '.eslintrc.js'), |
| 306 | input_api.os_path.join(scripts_directory, 'eslint_rules'), |
| 307 | ] |
| 308 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 309 | lint_config_files = _getAffectedFiles(input_api, eslint_related_files, [], |
| 310 | ['.js', '.py', '.eslintignore']) |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 311 | |
Mathias Bynens | 0ec5661 | 2020-06-19 07:14:03 | [diff] [blame] | 312 | should_bail_out, files_to_lint = _getFilesToLint( |
| 313 | input_api, output_api, lint_config_files, default_linted_directories, |
| 314 | ['.js', '.ts'], results) |
| 315 | if should_bail_out: |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 316 | return results |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 | [diff] [blame] | 317 | |
Brandon Goddard | e34e94f | 2021-04-12 17:58:26 | [diff] [blame] | 318 | # If there are more than 50 files to check, don't bother and check |
| 319 | # everything, so as to not run into command line length limits on Windows. |
| 320 | if len(files_to_lint) > 50: |
| 321 | files_to_lint = [] |
| 322 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 323 | results.extend( |
| 324 | _checkWithNodeScript(input_api, output_api, lint_path, files_to_lint)) |
Tim van der Lippe | 9813224 | 2020-04-14 16:16:54 | [diff] [blame] | 325 | return results |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 326 | |
| 327 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 328 | def _CheckDevToolsStyleCSS(input_api, output_api): |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 329 | results = [output_api.PresubmitNotifyResult('CSS style check:')] |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 330 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 331 | 'scripts', 'test', |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 332 | 'run_lint_check_css.js') |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 333 | |
| 334 | front_end_directory = input_api.os_path.join( |
| 335 | input_api.PresubmitLocalPath(), 'front_end') |
Alex Rudenko | 5556a90 | 2020-09-29 09:37:23 | [diff] [blame] | 336 | inspector_overlay_directory = input_api.os_path.join( |
| 337 | input_api.PresubmitLocalPath(), 'inspector_overlay') |
| 338 | default_linted_directories = [ |
| 339 | front_end_directory, inspector_overlay_directory |
| 340 | ] |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 341 | |
| 342 | scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 343 | 'scripts') |
| 344 | |
| 345 | stylelint_related_files = [ |
| 346 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules', |
| 347 | 'stylelint'), |
| 348 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 349 | '.stylelintrc.json'), |
| 350 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 351 | '.stylelintignore'), |
| 352 | input_api.os_path.join(scripts_directory, 'test', |
Sigurd Schneider | 6523c51 | 2021-02-12 09:44:28 | [diff] [blame] | 353 | 'run_lint_check_css.js'), |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 354 | ] |
| 355 | |
| 356 | lint_config_files = _getAffectedFiles(input_api, stylelint_related_files, |
Sigurd Schneider | 6523c51 | 2021-02-12 09:44:28 | [diff] [blame] | 357 | [], []) |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 358 | |
Sigurd Schneider | e3bf6c2 | 2021-02-11 14:35:23 | [diff] [blame] | 359 | css_should_bail_out, css_files_to_lint = _getFilesToLint( |
Mathias Bynens | 0ec5661 | 2020-06-19 07:14:03 | [diff] [blame] | 360 | input_api, output_api, lint_config_files, default_linted_directories, |
| 361 | ['.css'], results) |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 362 | |
Sigurd Schneider | f3a1ecd | 2021-03-02 14:46:03 | [diff] [blame] | 363 | # If there are more than 50 files to check, don't bother and check |
| 364 | # everything, so as to not run into command line length limits on Windows. |
| 365 | if not css_should_bail_out: |
| 366 | if len(css_files_to_lint) < 50: |
| 367 | script_args = ["--files"] + css_files_to_lint |
| 368 | else: |
| 369 | script_args = [] # The defaults check all CSS files. |
| 370 | results.extend( |
| 371 | _checkWithNodeScript(input_api, output_api, lint_path, |
| 372 | script_args)) |
| 373 | |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 | [diff] [blame] | 374 | return results |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 375 | |
| 376 | |
Tim van der Lippe | a53672d | 2021-07-08 14:52:35 | [diff] [blame] | 377 | def _CheckDevToolsNonJSFileLicenseHeaders(input_api, output_api): |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 378 | results = [ |
| 379 | output_api.PresubmitNotifyResult( |
| 380 | 'Python-like file license header check:') |
| 381 | ] |
Tim van der Lippe | a53672d | 2021-07-08 14:52:35 | [diff] [blame] | 382 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 383 | 'scripts', 'test', |
| 384 | 'run_header_check_non_js_files.js') |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 385 | |
| 386 | front_end_directory = input_api.os_path.join( |
| 387 | input_api.PresubmitLocalPath(), 'front_end') |
| 388 | inspector_overlay_directory = input_api.os_path.join( |
| 389 | input_api.PresubmitLocalPath(), 'inspector_overlay') |
| 390 | test_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 391 | 'test') |
| 392 | scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 393 | 'scripts') |
Tim van der Lippe | 8b92954 | 2021-05-26 14:54:20 | [diff] [blame] | 394 | config_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 395 | 'config') |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 396 | |
| 397 | default_linted_directories = [ |
| 398 | front_end_directory, test_directory, scripts_directory, |
Tim van der Lippe | 8b92954 | 2021-05-26 14:54:20 | [diff] [blame] | 399 | inspector_overlay_directory, config_directory |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 400 | ] |
| 401 | |
| 402 | check_related_files = [lint_path] |
| 403 | |
| 404 | lint_config_files = _getAffectedFiles(input_api, check_related_files, [], |
| 405 | ['.js']) |
| 406 | |
| 407 | should_bail_out, files_to_lint = _getFilesToLint( |
| 408 | input_api, output_api, lint_config_files, default_linted_directories, |
Tim van der Lippe | a53672d | 2021-07-08 14:52:35 | [diff] [blame] | 409 | ['BUILD.gn', '.gni', '.css'], results) |
Tim van der Lippe | 8175250 | 2021-05-26 14:38:12 | [diff] [blame] | 410 | if should_bail_out: |
| 411 | return results |
| 412 | |
| 413 | # If there are more than 50 files to check, don't bother and check |
| 414 | # everything, so as to not run into command line length limits on Windows. |
| 415 | if len(files_to_lint) > 50: |
| 416 | files_to_lint = [] |
| 417 | |
| 418 | results.extend( |
| 419 | _checkWithNodeScript(input_api, output_api, lint_path, files_to_lint)) |
| 420 | return results |
| 421 | |
| 422 | |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 423 | def _CheckGeneratedFiles(input_api, output_api): |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 424 | v8_directory_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'v8') |
| 425 | blink_directory_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'blink') |
| 426 | protocol_location = input_api.os_path.join(blink_directory_path, 'public', 'devtools_protocol') |
| 427 | scripts_build_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'build') |
Tim van der Lippe | 5d2d79b | 2020-03-23 11:45:04 | [diff] [blame] | 428 | scripts_generated_output_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', 'generated') |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 429 | |
| 430 | generated_aria_path = input_api.os_path.join(scripts_build_path, 'generate_aria.py') |
| 431 | generated_supported_css_path = input_api.os_path.join(scripts_build_path, 'generate_supported_css.py') |
| 432 | generated_protocol_path = input_api.os_path.join(scripts_build_path, 'code_generator_frontend.py') |
Tim van der Lippe | 2a1eac2 | 2021-05-13 15:19:29 | [diff] [blame] | 433 | generated_protocol_typescript_path = input_api.os_path.join( |
| 434 | input_api.PresubmitLocalPath(), 'scripts', 'protocol_typescript') |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 435 | concatenate_protocols_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'inspector_protocol', |
| 436 | 'concatenate_protocols.py') |
| 437 | |
| 438 | affected_files = _getAffectedFiles(input_api, [ |
| 439 | v8_directory_path, |
| 440 | blink_directory_path, |
Tim van der Lippe | 2a1eac2 | 2021-05-13 15:19:29 | [diff] [blame] | 441 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', |
| 442 | 'pyjson5'), |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 443 | generated_aria_path, |
| 444 | generated_supported_css_path, |
| 445 | concatenate_protocols_path, |
| 446 | generated_protocol_path, |
Tim van der Lippe | 5d2d79b | 2020-03-23 11:45:04 | [diff] [blame] | 447 | scripts_generated_output_path, |
Tim van der Lippe | 2a1eac2 | 2021-05-13 15:19:29 | [diff] [blame] | 448 | generated_protocol_typescript_path, |
| 449 | ], [], ['.pdl', '.json5', '.py', '.js', '.ts']) |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 450 | |
| 451 | if len(affected_files) == 0: |
Tim van der Lippe | fb02346 | 2020-08-21 13:10:06 | [diff] [blame] | 452 | return [ |
| 453 | output_api.PresubmitNotifyResult( |
| 454 | 'No affected files for generated files check') |
| 455 | ] |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 | [diff] [blame] | 456 | |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 457 | results = [output_api.PresubmitNotifyResult('Running Generated Files Check:')] |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 458 | generate_protocol_resources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'deps', |
| 459 | 'generate_protocol_resources.py') |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 460 | |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 | [diff] [blame] | 461 | return _ExecuteSubProcess(input_api, output_api, generate_protocol_resources_path, [], results) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 462 | |
| 463 | |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 464 | def _CollectStrings(input_api, output_api): |
| 465 | devtools_root = input_api.PresubmitLocalPath() |
| 466 | devtools_front_end = input_api.os_path.join(devtools_root, 'front_end') |
Tim van der Lippe | 25f1108 | 2021-06-24 15:28:08 | [diff] [blame] | 467 | script_path = input_api.os_path.join(devtools_root, 'third_party', 'i18n', |
| 468 | 'collect-strings.js') |
| 469 | affected_front_end_files = _getAffectedFiles( |
| 470 | input_api, [devtools_front_end, script_path], [], ['.js', '.ts']) |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 471 | if len(affected_front_end_files) == 0: |
| 472 | return [ |
| 473 | output_api.PresubmitNotifyResult( |
| 474 | 'No affected files to run collect-strings') |
| 475 | ] |
| 476 | |
| 477 | results = [ |
| 478 | output_api.PresubmitNotifyResult('Collecting strings from front_end:') |
| 479 | ] |
Tim van der Lippe | 25f1108 | 2021-06-24 15:28:08 | [diff] [blame] | 480 | results.extend( |
| 481 | _checkWithNodeScript(input_api, output_api, script_path, |
| 482 | [devtools_front_end])) |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 483 | results.append( |
| 484 | output_api.PresubmitNotifyResult( |
Peter Marshall | d67e9f1 | 2021-02-08 09:34:35 | [diff] [blame] | 485 | 'Please commit en-US.json/en-XL.json if changes are generated.')) |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 486 | return results |
| 487 | |
| 488 | |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 | [diff] [blame] | 489 | def _CheckNoUncheckedFiles(input_api, output_api): |
| 490 | results = [] |
| 491 | process = input_api.subprocess.Popen(['git', 'diff', '--exit-code'], |
| 492 | stdout=input_api.subprocess.PIPE, |
| 493 | stderr=input_api.subprocess.STDOUT) |
| 494 | out, _ = process.communicate() |
| 495 | if process.returncode != 0: |
Jack Franklin | 324f304 | 2020-09-03 10:28:29 | [diff] [blame] | 496 | files_changed_process = input_api.subprocess.Popen( |
Tim van der Lippe | 25f1108 | 2021-06-24 15:28:08 | [diff] [blame] | 497 | ['git', 'diff'], |
Jack Franklin | 324f304 | 2020-09-03 10:28:29 | [diff] [blame] | 498 | stdout=input_api.subprocess.PIPE, |
| 499 | stderr=input_api.subprocess.STDOUT) |
Tim van der Lippe | 9bb1cf6 | 2020-03-06 16:17:02 | [diff] [blame] | 500 | files_changed, _ = files_changed_process.communicate() |
| 501 | |
| 502 | return [ |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 503 | output_api.PresubmitError( |
| 504 | 'You have changed files that need to be committed:'), |
| 505 | output_api.PresubmitError(files_changed.decode('utf-8')) |
Tim van der Lippe | 9bb1cf6 | 2020-03-06 16:17:02 | [diff] [blame] | 506 | ] |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 | [diff] [blame] | 507 | return [] |
| 508 | |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 | [diff] [blame] | 509 | def _CheckForTooLargeFiles(input_api, output_api): |
Christy Chen | 1ab87e0 | 2020-01-31 00:32:16 | [diff] [blame] | 510 | """Avoid large files, especially binary files, in the repository since |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 | [diff] [blame] | 511 | git doesn't scale well for those. They will be in everyone's repo |
| 512 | clones forever, forever making Chromium slower to clone and work |
| 513 | with.""" |
Christy Chen | 1ab87e0 | 2020-01-31 00:32:16 | [diff] [blame] | 514 | # Uploading files to cloud storage is not trivial so we don't want |
| 515 | # to set the limit too low, but the upper limit for "normal" large |
| 516 | # files seems to be 1-2 MB, with a handful around 5-8 MB, so |
| 517 | # anything over 20 MB is exceptional. |
| 518 | TOO_LARGE_FILE_SIZE_LIMIT = 20 * 1024 * 1024 # 10 MB |
| 519 | too_large_files = [] |
| 520 | for f in input_api.AffectedFiles(): |
| 521 | # Check both added and modified files (but not deleted files). |
| 522 | if f.Action() in ('A', 'M'): |
| 523 | size = input_api.os_path.getsize(f.AbsoluteLocalPath()) |
| 524 | if size > TOO_LARGE_FILE_SIZE_LIMIT: |
| 525 | too_large_files.append("%s: %d bytes" % (f.LocalPath(), size)) |
| 526 | if too_large_files: |
| 527 | message = ( |
| 528 | 'Do not commit large files to git since git scales badly for those.\n' + |
| 529 | 'Instead put the large files in cloud storage and use DEPS to\n' + |
| 530 | 'fetch them.\n' + '\n'.join(too_large_files) |
| 531 | ) |
| 532 | return [output_api.PresubmitError( |
| 533 | 'Too large files found in commit', long_text=message + '\n')] |
| 534 | else: |
| 535 | return [] |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 | [diff] [blame] | 536 | |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 | [diff] [blame] | 537 | |
Tim van der Lippe | f8a8709 | 2020-09-14 12:01:18 | [diff] [blame] | 538 | def _RunCannedChecks(input_api, output_api): |
| 539 | results = [] |
| 540 | results.extend( |
| 541 | input_api.canned_checks.CheckOwnersFormat(input_api, output_api)) |
| 542 | results.extend(input_api.canned_checks.CheckOwners(input_api, output_api)) |
| 543 | results.extend( |
| 544 | input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol( |
| 545 | input_api, output_api)) |
| 546 | results.extend( |
| 547 | input_api.canned_checks.CheckChangeHasNoStrayWhitespace( |
Tim van der Lippe | 6977538 | 2021-05-27 16:06:12 | [diff] [blame] | 548 | input_api, |
| 549 | output_api, |
| 550 | source_file_filter=lambda file: not file.LocalPath().startswith( |
| 551 | 'node_modules'))) |
Tim van der Lippe | f8a8709 | 2020-09-14 12:01:18 | [diff] [blame] | 552 | results.extend( |
| 553 | input_api.canned_checks.CheckGenderNeutral(input_api, output_api)) |
| 554 | return results |
| 555 | |
| 556 | |
Yang Guo | 4fd355c | 2019-09-19 08:59:03 | [diff] [blame] | 557 | def _CommonChecks(input_api, output_api): |
Mathias Bynens | 032591d | 2019-10-21 09:51:31 | [diff] [blame] | 558 | """Checks common to both upload and commit.""" |
| 559 | results = [] |
Mathias Bynens | 011b007 | 2020-08-05 08:17:35 | [diff] [blame] | 560 | results.extend( |
| 561 | input_api.canned_checks.CheckAuthorizedAuthor( |
| 562 | input_api, output_api, bot_allowlist=[AUTOROLL_ACCOUNT])) |
Brandon Goddard | 3310437 | 2020-08-13 15:49:23 | [diff] [blame] | 563 | results.extend(_CheckExperimentTelemetry(input_api, output_api)) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 | [diff] [blame] | 564 | results.extend(_CheckGeneratedFiles(input_api, output_api)) |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 565 | results.extend(_CheckDevToolsStyleJS(input_api, output_api)) |
| 566 | results.extend(_CheckDevToolsStyleCSS(input_api, output_api)) |
Jack Franklin | 1aa212d | 2021-09-10 14:20:08 | [diff] [blame] | 567 | results.extend(_CheckDevToolsRunESLintTests(input_api, output_api)) |
Tim van der Lippe | 800d875 | 2022-02-04 12:49:56 | [diff] [blame] | 568 | results.extend(_CheckDevToolsRunBuildTests(input_api, output_api)) |
Tim van der Lippe | a53672d | 2021-07-08 14:52:35 | [diff] [blame] | 569 | results.extend(_CheckDevToolsNonJSFileLicenseHeaders( |
| 570 | input_api, output_api)) |
Jack Franklin | b10193f | 2021-03-19 10:25:08 | [diff] [blame] | 571 | |
Tim van der Lippe | 5497d48 | 2020-01-14 15:27:30 | [diff] [blame] | 572 | results.extend(_CheckFormat(input_api, output_api)) |
Yang Guo | a7845d5 | 2019-10-31 10:30:23 | [diff] [blame] | 573 | results.extend(_CheckChangesAreExclusiveToDirectory(input_api, output_api)) |
Tim van der Lippe | f8a8709 | 2020-09-14 12:01:18 | [diff] [blame] | 574 | # Run the canned checks from `depot_tools` after the custom DevTools checks. |
| 575 | # The canned checks for example check that lines have line endings. The |
| 576 | # DevTools presubmit checks automatically fix these issues. If we would run |
| 577 | # the canned checks before the DevTools checks, they would erroneously conclude |
| 578 | # that there are issues in the code. Since the canned checks are allowed to be |
| 579 | # ignored, a confusing message is shown that asks if the failed presubmit can |
| 580 | # be continued regardless. By fixing the issues before we reach the canned checks, |
| 581 | # we don't show the message to suppress these errors, which would otherwise be |
| 582 | # causing CQ to fail. |
| 583 | results.extend(_RunCannedChecks(input_api, output_api)) |
Kalon Hinds | d44fddf | 2020-12-10 13:43:25 | [diff] [blame] | 584 | return results |
| 585 | |
| 586 | |
| 587 | def _SideEffectChecks(input_api, output_api): |
| 588 | """Check side effects caused by other checks""" |
| 589 | results = [] |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 | [diff] [blame] | 590 | results.extend(_CheckNoUncheckedFiles(input_api, output_api)) |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 | [diff] [blame] | 591 | results.extend(_CheckForTooLargeFiles(input_api, output_api)) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 592 | return results |
| 593 | |
| 594 | |
Liviu Rau | d614e09 | 2020-01-08 09:56:33 | [diff] [blame] | 595 | def CheckChangeOnUpload(input_api, output_api): |
| 596 | results = [] |
| 597 | results.extend(_CommonChecks(input_api, output_api)) |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 598 | results.extend(_CollectStrings(input_api, output_api)) |
Kalon Hinds | d44fddf | 2020-12-10 13:43:25 | [diff] [blame] | 599 | # Run checks that rely on output from other DevTool checks |
| 600 | results.extend(_SideEffectChecks(input_api, output_api)) |
Sigurd Schneider | 5c9b4f9 | 2021-01-22 10:09:55 | [diff] [blame] | 601 | results.extend(_CheckBugAssociation(input_api, output_api, False)) |
Liviu Rau | d614e09 | 2020-01-08 09:56:33 | [diff] [blame] | 602 | return results |
| 603 | |
| 604 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 605 | def CheckChangeOnCommit(input_api, output_api): |
Mandy Chen | f0fbdbe | 2019-08-22 23:58:37 | [diff] [blame] | 606 | results = [] |
Yang Guo | 4fd355c | 2019-09-19 08:59:03 | [diff] [blame] | 607 | results.extend(_CommonChecks(input_api, output_api)) |
Christy Chen | 2d6d9a6 | 2020-09-22 16:04:09 | [diff] [blame] | 608 | results.extend(_CollectStrings(input_api, output_api)) |
Kalon Hinds | d44fddf | 2020-12-10 13:43:25 | [diff] [blame] | 609 | # Run checks that rely on output from other DevTool checks |
| 610 | results.extend(_SideEffectChecks(input_api, output_api)) |
Mathias Bynens | 032591d | 2019-10-21 09:51:31 | [diff] [blame] | 611 | results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api)) |
Sigurd Schneider | 5c9b4f9 | 2021-01-22 10:09:55 | [diff] [blame] | 612 | results.extend(_CheckBugAssociation(input_api, output_api, True)) |
Mandy Chen | f0fbdbe | 2019-08-22 23:58:37 | [diff] [blame] | 613 | return results |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 614 | |
| 615 | |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 | [diff] [blame] | 616 | def _getAffectedFiles(input_api, parent_directories, excluded_actions, accepted_endings): # pylint: disable=invalid-name |
Yang Guo | 75beda9 | 2019-10-28 07:29:25 | [diff] [blame] | 617 | """Return absolute file paths of affected files (not due to an excluded action) |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 | [diff] [blame] | 618 | under a parent directory with an accepted file ending. |
Yang Guo | 75beda9 | 2019-10-28 07:29:25 | [diff] [blame] | 619 | """ |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 | [diff] [blame] | 620 | local_paths = [ |
| 621 | f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if all(f.Action() != action for action in excluded_actions) |
| 622 | ] |
| 623 | affected_files = [ |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 624 | file_name for file_name in local_paths |
| 625 | if any(parent_directory in file_name |
| 626 | for parent_directory in parent_directories) and ( |
| 627 | len(accepted_endings) == 0 or any( |
| 628 | file_name.endswith(accepted_ending) |
| 629 | for accepted_ending in accepted_endings)) |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 | [diff] [blame] | 630 | ] |
| 631 | return affected_files |
| 632 | |
| 633 | |
Tim van der Lippe | c461712 | 2020-03-06 16:24:19 | [diff] [blame] | 634 | def _checkWithNodeScript(input_api, output_api, script_path, script_arguments=[]): # pylint: disable=invalid-name |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 635 | original_sys_path = sys.path |
| 636 | try: |
Yang Guo | 75beda9 | 2019-10-28 07:29:25 | [diff] [blame] | 637 | sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')] |
Yang Guo | d817698 | 2019-10-04 20:30:35 | [diff] [blame] | 638 | import devtools_paths |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 639 | finally: |
| 640 | sys.path = original_sys_path |
| 641 | |
Tim van der Lippe | c461712 | 2020-03-06 16:24:19 | [diff] [blame] | 642 | return _ExecuteSubProcess(input_api, output_api, [devtools_paths.node_path(), script_path], script_arguments, []) |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 643 | |
| 644 | |
| 645 | def _getFilesToLint(input_api, output_api, lint_config_files, |
| 646 | default_linted_directories, accepted_endings, results): |
Mathias Bynens | 0ec5661 | 2020-06-19 07:14:03 | [diff] [blame] | 647 | run_full_check = False |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 648 | files_to_lint = [] |
| 649 | |
| 650 | # We are changing the lint configuration; run the full check. |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 651 | if len(lint_config_files) != 0: |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 652 | results.append( |
| 653 | output_api.PresubmitNotifyResult('Running full lint check')) |
Mathias Bynens | 0ec5661 | 2020-06-19 07:14:03 | [diff] [blame] | 654 | run_full_check = True |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 655 | else: |
| 656 | # Only run the linter on files that are relevant, to save PRESUBMIT time. |
| 657 | files_to_lint = _getAffectedFiles(input_api, |
| 658 | default_linted_directories, ['D'], |
| 659 | accepted_endings) |
| 660 | |
Jack Franklin | 130d2ae | 2022-07-12 09:51:26 | [diff] [blame] | 661 | # Exclude front_end/third_party and front_end/generated files. |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 662 | files_to_lint = [ |
Jack Franklin | 130d2ae | 2022-07-12 09:51:26 | [diff] [blame] | 663 | file for file in files_to_lint |
| 664 | if "third_party" not in file or "generated" not in file |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 665 | ] |
Paul Lewis | 2b9224f | 2020-09-08 17:13:10 | [diff] [blame] | 666 | |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 667 | if len(files_to_lint) == 0: |
Mathias Bynens | 1b2c5e4 | 2020-06-18 06:29:21 | [diff] [blame] | 668 | results.append( |
| 669 | output_api.PresubmitNotifyResult( |
| 670 | 'No affected files for lint check')) |
| 671 | |
Tim van der Lippe | fb1dc17 | 2021-05-11 15:40:26 | [diff] [blame] | 672 | should_bail_out = len(files_to_lint) == 0 and not run_full_check |
Mathias Bynens | 0ec5661 | 2020-06-19 07:14:03 | [diff] [blame] | 673 | return should_bail_out, files_to_lint |