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