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