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