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