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