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