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): |
Tim van der Lippe | c2756dc | 2021-12-08 12:17:32 +0000 | [diff] [blame] | 78 | if file.endswith('OWNERS') and 'OWNERS' in dirs: |
| 79 | return True |
Yang Guo | a7845d5 | 2019-10-31 11:30:23 +0100 | [diff] [blame] | 80 | for dir in dirs: |
| 81 | if IsParentDir(file, dir): |
| 82 | return True |
| 83 | |
Tim van der Lippe | 4050a30 | 2021-04-13 10:21:21 +0100 | [diff] [blame] | 84 | EXCLUSIVE_CHANGE_DIRECTORIES = [ |
| 85 | [ |
| 86 | 'third_party', 'v8', |
| 87 | input_api.os_path.join('front_end', 'generated') |
| 88 | ], |
| 89 | [ |
| 90 | 'node_modules', |
Tim van der Lippe | 4050a30 | 2021-04-13 10:21:21 +0100 | [diff] [blame] | 91 | 'package-lock.json', |
| 92 | input_api.os_path.join('scripts', 'deps', 'manage_node_deps.py'), |
| 93 | ], |
Tim van der Lippe | c2756dc | 2021-12-08 12:17:32 +0000 | [diff] [blame] | 94 | ['OWNERS'], |
Tim van der Lippe | 4050a30 | 2021-04-13 10:21:21 +0100 | [diff] [blame] | 95 | ] |
| 96 | |
Yang Guo | a7845d5 | 2019-10-31 11:30:23 +0100 | [diff] [blame] | 97 | affected_files = input_api.LocalPaths() |
Yang Guo | a7845d5 | 2019-10-31 11:30:23 +0100 | [diff] [blame] | 98 | num_affected = len(affected_files) |
| 99 | for dirs in EXCLUSIVE_CHANGE_DIRECTORIES: |
Paul Lewis | 14effba | 2019-12-02 14:56:40 +0000 | [diff] [blame] | 100 | dir_list = ', '.join(dirs) |
Tim van der Lippe | fb1dc17 | 2021-05-11 16:40:26 +0100 | [diff] [blame] | 101 | affected_in_dir = [ |
| 102 | file for file in affected_files if FileIsInDir(file, dirs) |
| 103 | ] |
Yang Guo | a7845d5 | 2019-10-31 11:30:23 +0100 | [diff] [blame] | 104 | num_in_dir = len(affected_in_dir) |
| 105 | if num_in_dir == 0: |
| 106 | continue |
Tim van der Lippe | ebb94a9 | 2019-11-19 17:07:53 +0000 | [diff] [blame] | 107 | # Addition of new third_party folders must have a new entry in `.gitignore` |
| 108 | if '.gitignore' in affected_files: |
| 109 | num_in_dir = num_in_dir + 1 |
Yang Guo | a7845d5 | 2019-10-31 11:30:23 +0100 | [diff] [blame] | 110 | if num_in_dir < num_affected: |
Tim van der Lippe | 239963b | 2021-04-09 10:43:38 +0100 | [diff] [blame] | 111 | unexpected_files = [ |
| 112 | file for file in affected_files if file not in affected_in_dir |
| 113 | ] |
| 114 | results.append( |
| 115 | output_api.PresubmitError( |
| 116 | ('CLs that affect files in "%s" should be limited to these files/directories.' |
| 117 | % dir_list) + |
| 118 | ('\nUnexpected files: %s.' % unexpected_files) + |
| 119 | '\nYou can disable this check by adding DISABLE_THIRD_PARTY_CHECK=<reason> to your commit message' |
| 120 | )) |
Brandon Goddard | e702867 | 2020-01-30 09:31:04 -0800 | [diff] [blame] | 121 | break |
| 122 | |
| 123 | return results |
Yang Guo | a7845d5 | 2019-10-31 11:30:23 +0100 | [diff] [blame] | 124 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 125 | |
Sigurd Schneider | 5c9b4f9 | 2021-01-22 11:09:55 +0100 | [diff] [blame] | 126 | def _CheckBugAssociation(input_api, output_api, is_committing): |
| 127 | results = [output_api.PresubmitNotifyResult('Bug Association Check:')] |
| 128 | bugs = input_api.change.BugsFromDescription() |
| 129 | message = ( |
| 130 | "Each CL should be associated with a bug, use \'Bug:\' or \'Fixed:\' lines in\n" |
| 131 | "the footer of the commit description. If you explicitly don\'t want to\n" |
| 132 | "set a bug, use \'Bug: none\' in the footer of the commit description.\n\n" |
| 133 | "Note: The footer of the commit description is the last block of lines in\n" |
| 134 | "the commit description that doesn't contain empty lines. This means that\n" |
| 135 | "any \'Bug:\' or \'Fixed:\' lines that are eventually followed by an empty\n" |
| 136 | "line are not detected by this presubmit check.") |
| 137 | |
| 138 | if not bugs: |
| 139 | if is_committing: |
| 140 | results.append(output_api.PresubmitError(message)) |
| 141 | else: |
| 142 | results.append(output_api.PresubmitNotifyResult(message)) |
| 143 | |
| 144 | for bug in bugs: |
| 145 | results.append(output_api.PresubmitNotifyResult(('%s') % bug)) |
| 146 | |
| 147 | return results |
| 148 | |
| 149 | |
Brandon Goddard | 3310437 | 2020-08-13 08:49:23 -0700 | [diff] [blame] | 150 | def _CheckExperimentTelemetry(input_api, output_api): |
Brandon Goddard | 3310437 | 2020-08-13 08:49:23 -0700 | [diff] [blame] | 151 | experiment_telemetry_files = [ |
| 152 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', |
Christy Chen | ab9a44d | 2021-07-02 12:54:30 -0700 | [diff] [blame] | 153 | 'entrypoints', 'main', 'MainImpl.ts'), |
Brandon Goddard | 3310437 | 2020-08-13 08:49:23 -0700 | [diff] [blame] | 154 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', |
Tim van der Lippe | e024731 | 2021-04-01 15:25:30 +0100 | [diff] [blame] | 155 | 'core', 'host', 'UserMetrics.ts') |
Brandon Goddard | 3310437 | 2020-08-13 08:49:23 -0700 | [diff] [blame] | 156 | ] |
| 157 | affected_main_files = _getAffectedFiles(input_api, |
| 158 | experiment_telemetry_files, [], |
Christy Chen | ab9a44d | 2021-07-02 12:54:30 -0700 | [diff] [blame] | 159 | ['.ts']) |
Brandon Goddard | 3310437 | 2020-08-13 08:49:23 -0700 | [diff] [blame] | 160 | if len(affected_main_files) == 0: |
Tim van der Lippe | fb02346 | 2020-08-21 14:10:06 +0100 | [diff] [blame] | 161 | return [ |
| 162 | output_api.PresubmitNotifyResult( |
| 163 | 'No affected files for telemetry check') |
| 164 | ] |
Brandon Goddard | 3310437 | 2020-08-13 08:49:23 -0700 | [diff] [blame] | 165 | |
Tim van der Lippe | fb02346 | 2020-08-21 14:10:06 +0100 | [diff] [blame] | 166 | results = [ |
| 167 | output_api.PresubmitNotifyResult('Running Experiment Telemetry check:') |
| 168 | ] |
Brandon Goddard | 3310437 | 2020-08-13 08:49:23 -0700 | [diff] [blame] | 169 | script_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 170 | 'scripts', 'check_experiments.js') |
| 171 | results.extend(_checkWithNodeScript(input_api, output_api, script_path)) |
| 172 | return results |
| 173 | |
| 174 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 175 | def _CheckFormat(input_api, output_api): |
Sigurd Schneider | f3a1ecd | 2021-03-02 15:46:03 +0100 | [diff] [blame] | 176 | node_modules_affected_files = _getAffectedFiles(input_api, [ |
Tim van der Lippe | 6509dba | 2021-11-23 16:19:23 +0000 | [diff] [blame] | 177 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules'), |
| 178 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'front_end', |
| 179 | 'third_party') |
Sigurd Schneider | f3a1ecd | 2021-03-02 15:46:03 +0100 | [diff] [blame] | 180 | ], [], []) |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 181 | |
| 182 | # TODO(crbug.com/1068198): Remove once `git cl format --js` can handle large CLs. |
| 183 | if (len(node_modules_affected_files) > 0): |
Tim van der Lippe | 6509dba | 2021-11-23 16:19:23 +0000 | [diff] [blame] | 184 | return [ |
| 185 | output_api.PresubmitNotifyResult( |
| 186 | 'Skipping Format Checks because `node_modules`/`front_end/third_party` files are affected.' |
| 187 | ) |
| 188 | ] |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 189 | |
Brandon Goddard | e702867 | 2020-01-30 09:31:04 -0800 | [diff] [blame] | 190 | results = [output_api.PresubmitNotifyResult('Running Format Checks:')] |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 191 | |
Tim van der Lippe | f515fdc | 2020-03-06 16:18:25 +0000 | [diff] [blame] | 192 | return _ExecuteSubProcess(input_api, output_api, ['git', 'cl', 'format', '--js'], [], results) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 193 | |
Jack Franklin | 1aa212d | 2021-09-10 15:20:08 +0100 | [diff] [blame] | 194 | |
| 195 | def _CheckDevToolsRunESLintTests(input_api, output_api): |
| 196 | # Check for changes in the eslint_rules directory, and run the eslint rules |
| 197 | # tests if so. |
| 198 | # We don't do this on every CL as most do not touch the rules, but if we do |
| 199 | # change them we need to make sure all the tests are passing. |
Jack Franklin | 03db63a | 2021-09-16 14:40:56 +0100 | [diff] [blame] | 200 | original_sys_path = sys.path |
| 201 | try: |
| 202 | sys.path = sys.path + [ |
| 203 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts') |
| 204 | ] |
| 205 | import devtools_paths |
| 206 | finally: |
| 207 | sys.path = original_sys_path |
Jack Franklin | 1aa212d | 2021-09-10 15:20:08 +0100 | [diff] [blame] | 208 | eslint_rules_dir_path = input_api.os_path.join( |
| 209 | input_api.PresubmitLocalPath(), 'scripts', 'eslint_rules') |
| 210 | eslint_rules_affected_files = _getAffectedFiles(input_api, |
| 211 | [eslint_rules_dir_path], |
| 212 | [], []) |
| 213 | |
| 214 | if (len(eslint_rules_affected_files) == 0): |
| 215 | return [] |
| 216 | |
Jack Franklin | 03db63a | 2021-09-16 14:40:56 +0100 | [diff] [blame] | 217 | mocha_path = devtools_paths.mocha_path() |
Jack Franklin | 1aa212d | 2021-09-10 15:20:08 +0100 | [diff] [blame] | 218 | eslint_tests_path = input_api.os_path.join(eslint_rules_dir_path, 'tests', |
| 219 | '*_test.js') |
| 220 | |
| 221 | results = [output_api.PresubmitNotifyResult('ESLint rules unit tests')] |
| 222 | results.extend( |
| 223 | # The dot reporter is more concise which is useful to not get LOADS of |
| 224 | # output when just one test fails. |
| 225 | _checkWithNodeScript(input_api, output_api, mocha_path, |
| 226 | ['--reporter', 'dot', eslint_tests_path])) |
| 227 | return results |
| 228 | |
| 229 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 230 | def _CheckDevToolsStyleJS(input_api, output_api): |
Tim van der Lippe | fb02346 | 2020-08-21 14:10:06 +0100 | [diff] [blame] | 231 | results = [output_api.PresubmitNotifyResult('JS style check:')] |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 232 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 233 | 'scripts', 'test', |
Tim van der Lippe | f9e565e | 2021-11-08 16:22:11 +0000 | [diff] [blame] | 234 | 'run_lint_check_js.mjs') |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 +0000 | [diff] [blame] | 235 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 236 | front_end_directory = input_api.os_path.join( |
| 237 | input_api.PresubmitLocalPath(), 'front_end') |
Jack Franklin | bcfd6ad | 2021-02-17 10:12:50 +0000 | [diff] [blame] | 238 | 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] | 239 | 'ui', 'components', |
| 240 | 'docs') |
Alex Rudenko | 5556a90 | 2020-09-29 09:37:23 +0000 | [diff] [blame] | 241 | inspector_overlay_directory = input_api.os_path.join( |
| 242 | input_api.PresubmitLocalPath(), 'inspector_overlay') |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 243 | test_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 244 | 'test') |
| 245 | scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 246 | 'scripts') |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 +0000 | [diff] [blame] | 247 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 248 | default_linted_directories = [ |
Alex Rudenko | 5556a90 | 2020-09-29 09:37:23 +0000 | [diff] [blame] | 249 | front_end_directory, test_directory, scripts_directory, |
| 250 | inspector_overlay_directory |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 251 | ] |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 +0000 | [diff] [blame] | 252 | |
| 253 | eslint_related_files = [ |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 254 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules', |
| 255 | 'eslint'), |
Tim van der Lippe | cf4ab40 | 2021-02-12 14:30:58 +0000 | [diff] [blame] | 256 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules', |
| 257 | '@typescript-eslint'), |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 +0000 | [diff] [blame] | 258 | input_api.os_path.join(input_api.PresubmitLocalPath(), '.eslintrc.js'), |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 259 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 260 | '.eslintignore'), |
Tim van der Lippe | 33543ac | 2020-12-14 15:37:45 +0100 | [diff] [blame] | 261 | input_api.os_path.join(front_end_directory, '.eslintrc.js'), |
Jack Franklin | bcfd6ad | 2021-02-17 10:12:50 +0000 | [diff] [blame] | 262 | input_api.os_path.join(component_docs_directory, '.eslintrc.js'), |
Tim van der Lippe | 406249f | 2020-12-14 15:59:10 +0100 | [diff] [blame] | 263 | input_api.os_path.join(test_directory, '.eslintrc.js'), |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 264 | input_api.os_path.join(scripts_directory, 'test', |
| 265 | 'run_lint_check_js.py'), |
| 266 | input_api.os_path.join(scripts_directory, 'test', |
Tim van der Lippe | f9e565e | 2021-11-08 16:22:11 +0000 | [diff] [blame] | 267 | 'run_lint_check_js.mjs'), |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 +0000 | [diff] [blame] | 268 | input_api.os_path.join(scripts_directory, '.eslintrc.js'), |
| 269 | input_api.os_path.join(scripts_directory, 'eslint_rules'), |
| 270 | ] |
| 271 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 272 | lint_config_files = _getAffectedFiles(input_api, eslint_related_files, [], |
| 273 | ['.js', '.py', '.eslintignore']) |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 +0000 | [diff] [blame] | 274 | |
Mathias Bynens | 0ec5661 | 2020-06-19 09:14:03 +0200 | [diff] [blame] | 275 | should_bail_out, files_to_lint = _getFilesToLint( |
| 276 | input_api, output_api, lint_config_files, default_linted_directories, |
| 277 | ['.js', '.ts'], results) |
| 278 | if should_bail_out: |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 279 | return results |
Tim van der Lippe | 2a4ae2b | 2020-03-11 17:28:06 +0000 | [diff] [blame] | 280 | |
Brandon Goddard | e34e94f | 2021-04-12 10:58:26 -0700 | [diff] [blame] | 281 | # If there are more than 50 files to check, don't bother and check |
| 282 | # everything, so as to not run into command line length limits on Windows. |
| 283 | if len(files_to_lint) > 50: |
| 284 | files_to_lint = [] |
| 285 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 286 | results.extend( |
| 287 | _checkWithNodeScript(input_api, output_api, lint_path, files_to_lint)) |
Tim van der Lippe | 9813224 | 2020-04-14 17:16:54 +0100 | [diff] [blame] | 288 | return results |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 289 | |
| 290 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 291 | def _CheckDevToolsStyleCSS(input_api, output_api): |
Tim van der Lippe | fb02346 | 2020-08-21 14:10:06 +0100 | [diff] [blame] | 292 | results = [output_api.PresubmitNotifyResult('CSS style check:')] |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 293 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 294 | 'scripts', 'test', |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 +0000 | [diff] [blame] | 295 | 'run_lint_check_css.js') |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 296 | |
| 297 | front_end_directory = input_api.os_path.join( |
| 298 | input_api.PresubmitLocalPath(), 'front_end') |
Alex Rudenko | 5556a90 | 2020-09-29 09:37:23 +0000 | [diff] [blame] | 299 | inspector_overlay_directory = input_api.os_path.join( |
| 300 | input_api.PresubmitLocalPath(), 'inspector_overlay') |
| 301 | default_linted_directories = [ |
| 302 | front_end_directory, inspector_overlay_directory |
| 303 | ] |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 304 | |
| 305 | scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 306 | 'scripts') |
| 307 | |
| 308 | stylelint_related_files = [ |
| 309 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'node_modules', |
| 310 | 'stylelint'), |
| 311 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 312 | '.stylelintrc.json'), |
| 313 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 314 | '.stylelintignore'), |
| 315 | input_api.os_path.join(scripts_directory, 'test', |
Sigurd Schneider | 6523c51 | 2021-02-12 10:44:28 +0100 | [diff] [blame] | 316 | 'run_lint_check_css.js'), |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 317 | ] |
| 318 | |
| 319 | lint_config_files = _getAffectedFiles(input_api, stylelint_related_files, |
Sigurd Schneider | 6523c51 | 2021-02-12 10:44:28 +0100 | [diff] [blame] | 320 | [], []) |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 321 | |
Sigurd Schneider | e3bf6c2 | 2021-02-11 15:35:23 +0100 | [diff] [blame] | 322 | css_should_bail_out, css_files_to_lint = _getFilesToLint( |
Mathias Bynens | 0ec5661 | 2020-06-19 09:14:03 +0200 | [diff] [blame] | 323 | input_api, output_api, lint_config_files, default_linted_directories, |
| 324 | ['.css'], results) |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 325 | |
Sigurd Schneider | f3a1ecd | 2021-03-02 15:46:03 +0100 | [diff] [blame] | 326 | # If there are more than 50 files to check, don't bother and check |
| 327 | # everything, so as to not run into command line length limits on Windows. |
| 328 | if not css_should_bail_out: |
| 329 | if len(css_files_to_lint) < 50: |
| 330 | script_args = ["--files"] + css_files_to_lint |
| 331 | else: |
| 332 | script_args = [] # The defaults check all CSS files. |
| 333 | results.extend( |
| 334 | _checkWithNodeScript(input_api, output_api, lint_path, |
| 335 | script_args)) |
| 336 | |
Jack Franklin | bc30234 | 2021-01-18 10:03:30 +0000 | [diff] [blame] | 337 | return results |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 338 | |
| 339 | |
Tim van der Lippe | a53672d | 2021-07-08 15:52:35 +0100 | [diff] [blame] | 340 | def _CheckDevToolsNonJSFileLicenseHeaders(input_api, output_api): |
Tim van der Lippe | 8175250 | 2021-05-26 15:38:12 +0100 | [diff] [blame] | 341 | results = [ |
| 342 | output_api.PresubmitNotifyResult( |
| 343 | 'Python-like file license header check:') |
| 344 | ] |
Tim van der Lippe | a53672d | 2021-07-08 15:52:35 +0100 | [diff] [blame] | 345 | lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 346 | 'scripts', 'test', |
| 347 | 'run_header_check_non_js_files.js') |
Tim van der Lippe | 8175250 | 2021-05-26 15:38:12 +0100 | [diff] [blame] | 348 | |
| 349 | front_end_directory = input_api.os_path.join( |
| 350 | input_api.PresubmitLocalPath(), 'front_end') |
| 351 | inspector_overlay_directory = input_api.os_path.join( |
| 352 | input_api.PresubmitLocalPath(), 'inspector_overlay') |
| 353 | test_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 354 | 'test') |
| 355 | scripts_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 356 | 'scripts') |
Tim van der Lippe | 8b92954 | 2021-05-26 15:54:20 +0100 | [diff] [blame] | 357 | config_directory = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 358 | 'config') |
Tim van der Lippe | 8175250 | 2021-05-26 15:38:12 +0100 | [diff] [blame] | 359 | |
| 360 | default_linted_directories = [ |
| 361 | front_end_directory, test_directory, scripts_directory, |
Tim van der Lippe | 8b92954 | 2021-05-26 15:54:20 +0100 | [diff] [blame] | 362 | inspector_overlay_directory, config_directory |
Tim van der Lippe | 8175250 | 2021-05-26 15:38:12 +0100 | [diff] [blame] | 363 | ] |
| 364 | |
| 365 | check_related_files = [lint_path] |
| 366 | |
| 367 | lint_config_files = _getAffectedFiles(input_api, check_related_files, [], |
| 368 | ['.js']) |
| 369 | |
| 370 | should_bail_out, files_to_lint = _getFilesToLint( |
| 371 | input_api, output_api, lint_config_files, default_linted_directories, |
Tim van der Lippe | a53672d | 2021-07-08 15:52:35 +0100 | [diff] [blame] | 372 | ['BUILD.gn', '.gni', '.css'], results) |
Tim van der Lippe | 8175250 | 2021-05-26 15:38:12 +0100 | [diff] [blame] | 373 | if should_bail_out: |
| 374 | return results |
| 375 | |
| 376 | # If there are more than 50 files to check, don't bother and check |
| 377 | # everything, so as to not run into command line length limits on Windows. |
| 378 | if len(files_to_lint) > 50: |
| 379 | files_to_lint = [] |
| 380 | |
| 381 | results.extend( |
| 382 | _checkWithNodeScript(input_api, output_api, lint_path, files_to_lint)) |
| 383 | return results |
| 384 | |
| 385 | |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 +0000 | [diff] [blame] | 386 | def _CheckGeneratedFiles(input_api, output_api): |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 +0000 | [diff] [blame] | 387 | v8_directory_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'v8') |
| 388 | blink_directory_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'blink') |
| 389 | protocol_location = input_api.os_path.join(blink_directory_path, 'public', 'devtools_protocol') |
| 390 | 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] | 391 | 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] | 392 | |
| 393 | generated_aria_path = input_api.os_path.join(scripts_build_path, 'generate_aria.py') |
| 394 | generated_supported_css_path = input_api.os_path.join(scripts_build_path, 'generate_supported_css.py') |
| 395 | 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] | 396 | generated_protocol_typescript_path = input_api.os_path.join( |
| 397 | input_api.PresubmitLocalPath(), 'scripts', 'protocol_typescript') |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 +0000 | [diff] [blame] | 398 | concatenate_protocols_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', 'inspector_protocol', |
| 399 | 'concatenate_protocols.py') |
| 400 | |
| 401 | affected_files = _getAffectedFiles(input_api, [ |
| 402 | v8_directory_path, |
| 403 | blink_directory_path, |
Tim van der Lippe | 2a1eac2 | 2021-05-13 16:19:29 +0100 | [diff] [blame] | 404 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'third_party', |
| 405 | 'pyjson5'), |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 +0000 | [diff] [blame] | 406 | generated_aria_path, |
| 407 | generated_supported_css_path, |
| 408 | concatenate_protocols_path, |
| 409 | generated_protocol_path, |
Tim van der Lippe | 5d2d79b | 2020-03-23 11:45:04 +0000 | [diff] [blame] | 410 | scripts_generated_output_path, |
Tim van der Lippe | 2a1eac2 | 2021-05-13 16:19:29 +0100 | [diff] [blame] | 411 | generated_protocol_typescript_path, |
| 412 | ], [], ['.pdl', '.json5', '.py', '.js', '.ts']) |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 +0000 | [diff] [blame] | 413 | |
| 414 | if len(affected_files) == 0: |
Tim van der Lippe | fb02346 | 2020-08-21 14:10:06 +0100 | [diff] [blame] | 415 | return [ |
| 416 | output_api.PresubmitNotifyResult( |
| 417 | 'No affected files for generated files check') |
| 418 | ] |
Tim van der Lippe | b3b9076 | 2020-03-04 15:21:52 +0000 | [diff] [blame] | 419 | |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 +0000 | [diff] [blame] | 420 | results = [output_api.PresubmitNotifyResult('Running Generated Files Check:')] |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 +0000 | [diff] [blame] | 421 | generate_protocol_resources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts', 'deps', |
| 422 | 'generate_protocol_resources.py') |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 +0000 | [diff] [blame] | 423 | |
Tim van der Lippe | b0d65f1 | 2020-03-05 12:15:24 +0000 | [diff] [blame] | 424 | 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] | 425 | |
| 426 | |
Christy Chen | 2d6d9a6 | 2020-09-22 09:04:09 -0700 | [diff] [blame] | 427 | def _CollectStrings(input_api, output_api): |
| 428 | devtools_root = input_api.PresubmitLocalPath() |
| 429 | devtools_front_end = input_api.os_path.join(devtools_root, 'front_end') |
Tim van der Lippe | 25f1108 | 2021-06-24 16:28:08 +0100 | [diff] [blame] | 430 | script_path = input_api.os_path.join(devtools_root, 'third_party', 'i18n', |
| 431 | 'collect-strings.js') |
| 432 | affected_front_end_files = _getAffectedFiles( |
| 433 | input_api, [devtools_front_end, script_path], [], ['.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 | ] |
Tim van der Lippe | 25f1108 | 2021-06-24 16:28:08 +0100 | [diff] [blame] | 443 | results.extend( |
| 444 | _checkWithNodeScript(input_api, output_api, script_path, |
| 445 | [devtools_front_end])) |
Christy Chen | 2d6d9a6 | 2020-09-22 09:04:09 -0700 | [diff] [blame] | 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( |
Tim van der Lippe | 25f1108 | 2021-06-24 16:28:08 +0100 | [diff] [blame] | 460 | ['git', 'diff'], |
Jack Franklin | 324f304 | 2020-09-03 11:28:29 +0100 | [diff] [blame] | 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( |
Tim van der Lippe | 6977538 | 2021-05-27 17:06:12 +0100 | [diff] [blame] | 511 | input_api, |
| 512 | output_api, |
| 513 | source_file_filter=lambda file: not file.LocalPath().startswith( |
| 514 | 'node_modules'))) |
Tim van der Lippe | f8a8709 | 2020-09-14 13:01:18 +0100 | [diff] [blame] | 515 | results.extend( |
| 516 | input_api.canned_checks.CheckGenderNeutral(input_api, output_api)) |
| 517 | return results |
| 518 | |
| 519 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 520 | def _CommonChecks(input_api, output_api): |
Mathias Bynens | 032591d | 2019-10-21 11:51:31 +0200 | [diff] [blame] | 521 | """Checks common to both upload and commit.""" |
| 522 | results = [] |
Mathias Bynens | 011b007 | 2020-08-05 10:17:35 +0200 | [diff] [blame] | 523 | results.extend( |
| 524 | input_api.canned_checks.CheckAuthorizedAuthor( |
| 525 | input_api, output_api, bot_allowlist=[AUTOROLL_ACCOUNT])) |
Brandon Goddard | 3310437 | 2020-08-13 08:49:23 -0700 | [diff] [blame] | 526 | results.extend(_CheckExperimentTelemetry(input_api, output_api)) |
Tim van der Lippe | 4d004ec | 2020-03-03 18:32:01 +0000 | [diff] [blame] | 527 | results.extend(_CheckGeneratedFiles(input_api, output_api)) |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 528 | results.extend(_CheckDevToolsStyleJS(input_api, output_api)) |
| 529 | results.extend(_CheckDevToolsStyleCSS(input_api, output_api)) |
Jack Franklin | 1aa212d | 2021-09-10 15:20:08 +0100 | [diff] [blame] | 530 | results.extend(_CheckDevToolsRunESLintTests(input_api, output_api)) |
Tim van der Lippe | a53672d | 2021-07-08 15:52:35 +0100 | [diff] [blame] | 531 | results.extend(_CheckDevToolsNonJSFileLicenseHeaders( |
| 532 | input_api, output_api)) |
Jack Franklin | b10193f | 2021-03-19 10:25:08 +0000 | [diff] [blame] | 533 | |
Tim van der Lippe | 5497d48 | 2020-01-14 15:27:30 +0000 | [diff] [blame] | 534 | results.extend(_CheckFormat(input_api, output_api)) |
Yang Guo | a7845d5 | 2019-10-31 11:30:23 +0100 | [diff] [blame] | 535 | results.extend(_CheckChangesAreExclusiveToDirectory(input_api, output_api)) |
Tim van der Lippe | f8a8709 | 2020-09-14 13:01:18 +0100 | [diff] [blame] | 536 | # Run the canned checks from `depot_tools` after the custom DevTools checks. |
| 537 | # The canned checks for example check that lines have line endings. The |
| 538 | # DevTools presubmit checks automatically fix these issues. If we would run |
| 539 | # the canned checks before the DevTools checks, they would erroneously conclude |
| 540 | # that there are issues in the code. Since the canned checks are allowed to be |
| 541 | # ignored, a confusing message is shown that asks if the failed presubmit can |
| 542 | # be continued regardless. By fixing the issues before we reach the canned checks, |
| 543 | # we don't show the message to suppress these errors, which would otherwise be |
| 544 | # causing CQ to fail. |
| 545 | results.extend(_RunCannedChecks(input_api, output_api)) |
Kalon Hinds | d44fddf | 2020-12-10 05:43:25 -0800 | [diff] [blame] | 546 | return results |
| 547 | |
| 548 | |
| 549 | def _SideEffectChecks(input_api, output_api): |
| 550 | """Check side effects caused by other checks""" |
| 551 | results = [] |
Tim van der Lippe | 5279f84 | 2020-01-14 16:26:38 +0000 | [diff] [blame] | 552 | results.extend(_CheckNoUncheckedFiles(input_api, output_api)) |
Tim van der Lippe | 8fdda11 | 2020-01-27 11:27:06 +0000 | [diff] [blame] | 553 | results.extend(_CheckForTooLargeFiles(input_api, output_api)) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 554 | return results |
| 555 | |
| 556 | |
Liviu Rau | d614e09 | 2020-01-08 10:56:33 +0100 | [diff] [blame] | 557 | def CheckChangeOnUpload(input_api, output_api): |
| 558 | results = [] |
| 559 | results.extend(_CommonChecks(input_api, output_api)) |
Christy Chen | 2d6d9a6 | 2020-09-22 09:04:09 -0700 | [diff] [blame] | 560 | results.extend(_CollectStrings(input_api, output_api)) |
Kalon Hinds | d44fddf | 2020-12-10 05:43:25 -0800 | [diff] [blame] | 561 | # Run checks that rely on output from other DevTool checks |
| 562 | results.extend(_SideEffectChecks(input_api, output_api)) |
Sigurd Schneider | 5c9b4f9 | 2021-01-22 11:09:55 +0100 | [diff] [blame] | 563 | results.extend(_CheckBugAssociation(input_api, output_api, False)) |
Liviu Rau | d614e09 | 2020-01-08 10:56:33 +0100 | [diff] [blame] | 564 | return results |
| 565 | |
| 566 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 567 | def CheckChangeOnCommit(input_api, output_api): |
Mandy Chen | f0fbdbe | 2019-08-22 23:58:37 +0000 | [diff] [blame] | 568 | results = [] |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 569 | results.extend(_CommonChecks(input_api, output_api)) |
Christy Chen | 2d6d9a6 | 2020-09-22 09:04:09 -0700 | [diff] [blame] | 570 | results.extend(_CollectStrings(input_api, output_api)) |
Kalon Hinds | d44fddf | 2020-12-10 05:43:25 -0800 | [diff] [blame] | 571 | # Run checks that rely on output from other DevTool checks |
| 572 | results.extend(_SideEffectChecks(input_api, output_api)) |
Mathias Bynens | 032591d | 2019-10-21 11:51:31 +0200 | [diff] [blame] | 573 | results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api)) |
Sigurd Schneider | 5c9b4f9 | 2021-01-22 11:09:55 +0100 | [diff] [blame] | 574 | results.extend(_CheckBugAssociation(input_api, output_api, True)) |
Mandy Chen | f0fbdbe | 2019-08-22 23:58:37 +0000 | [diff] [blame] | 575 | return results |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 576 | |
| 577 | |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 +0000 | [diff] [blame] | 578 | 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] | 579 | """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] | 580 | under a parent directory with an accepted file ending. |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame] | 581 | """ |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 +0000 | [diff] [blame] | 582 | local_paths = [ |
| 583 | f.AbsoluteLocalPath() for f in input_api.AffectedFiles() if all(f.Action() != action for action in excluded_actions) |
| 584 | ] |
| 585 | affected_files = [ |
Tim van der Lippe | fb1dc17 | 2021-05-11 16:40:26 +0100 | [diff] [blame] | 586 | file_name for file_name in local_paths |
| 587 | if any(parent_directory in file_name |
| 588 | for parent_directory in parent_directories) and ( |
| 589 | len(accepted_endings) == 0 or any( |
| 590 | file_name.endswith(accepted_ending) |
| 591 | for accepted_ending in accepted_endings)) |
Mandy Chen | a6be46a | 2019-07-09 17:06:27 +0000 | [diff] [blame] | 592 | ] |
| 593 | return affected_files |
| 594 | |
| 595 | |
Tim van der Lippe | c461712 | 2020-03-06 16:24:19 +0000 | [diff] [blame] | 596 | 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] | 597 | original_sys_path = sys.path |
| 598 | try: |
Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame] | 599 | 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] | 600 | import devtools_paths |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 601 | finally: |
| 602 | sys.path = original_sys_path |
| 603 | |
Tim van der Lippe | c461712 | 2020-03-06 16:24:19 +0000 | [diff] [blame] | 604 | 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] | 605 | |
| 606 | |
Jack Franklin | 324f304 | 2020-09-03 11:28:29 +0100 | [diff] [blame] | 607 | def _checkWithTypeScript(input_api, |
| 608 | output_api, |
| 609 | tsc_arguments, |
| 610 | script_path, |
| 611 | script_arguments=[]): # pylint: disable=invalid-name |
| 612 | original_sys_path = sys.path |
| 613 | try: |
| 614 | sys.path = sys.path + [ |
| 615 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts') |
| 616 | ] |
| 617 | import devtools_paths |
| 618 | finally: |
| 619 | sys.path = original_sys_path |
| 620 | |
| 621 | # First run tsc to compile the TS script that we then run in the _ExecuteSubProcess call |
| 622 | tsc_compiler_process = input_api.subprocess.Popen( |
| 623 | [ |
| 624 | devtools_paths.node_path(), |
| 625 | devtools_paths.typescript_compiler_path() |
| 626 | ] + tsc_arguments, |
| 627 | stdout=input_api.subprocess.PIPE, |
| 628 | stderr=input_api.subprocess.STDOUT) |
| 629 | |
| 630 | out, _ = tsc_compiler_process.communicate() |
| 631 | if tsc_compiler_process.returncode != 0: |
| 632 | return [ |
| 633 | output_api.PresubmitError('Error compiling briges regenerator:\n' + |
Tim van der Lippe | fb1dc17 | 2021-05-11 16:40:26 +0100 | [diff] [blame] | 634 | out.decode('utf-8')) |
Jack Franklin | 324f304 | 2020-09-03 11:28:29 +0100 | [diff] [blame] | 635 | ] |
| 636 | |
| 637 | return _checkWithNodeScript(input_api, output_api, script_path, |
| 638 | script_arguments) |
| 639 | |
| 640 | |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 641 | def _getFilesToLint(input_api, output_api, lint_config_files, |
| 642 | default_linted_directories, accepted_endings, results): |
Mathias Bynens | 0ec5661 | 2020-06-19 09:14:03 +0200 | [diff] [blame] | 643 | run_full_check = False |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 644 | files_to_lint = [] |
| 645 | |
| 646 | # We are changing the lint configuration; run the full check. |
Tim van der Lippe | fb1dc17 | 2021-05-11 16:40:26 +0100 | [diff] [blame] | 647 | if len(lint_config_files) != 0: |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 648 | results.append( |
| 649 | output_api.PresubmitNotifyResult('Running full lint check')) |
Mathias Bynens | 0ec5661 | 2020-06-19 09:14:03 +0200 | [diff] [blame] | 650 | run_full_check = True |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 651 | else: |
| 652 | # Only run the linter on files that are relevant, to save PRESUBMIT time. |
| 653 | files_to_lint = _getAffectedFiles(input_api, |
| 654 | default_linted_directories, ['D'], |
| 655 | accepted_endings) |
| 656 | |
Paul Lewis | 2b9224f | 2020-09-08 18:13:10 +0100 | [diff] [blame] | 657 | # Exclude front_end/third_party files. |
Tim van der Lippe | fb1dc17 | 2021-05-11 16:40:26 +0100 | [diff] [blame] | 658 | files_to_lint = [ |
| 659 | file for file in files_to_lint if "third_party" not in file |
| 660 | ] |
Paul Lewis | 2b9224f | 2020-09-08 18:13:10 +0100 | [diff] [blame] | 661 | |
Tim van der Lippe | fb1dc17 | 2021-05-11 16:40:26 +0100 | [diff] [blame] | 662 | if len(files_to_lint) == 0: |
Mathias Bynens | 1b2c5e4 | 2020-06-18 08:29:21 +0200 | [diff] [blame] | 663 | results.append( |
| 664 | output_api.PresubmitNotifyResult( |
| 665 | 'No affected files for lint check')) |
| 666 | |
Tim van der Lippe | fb1dc17 | 2021-05-11 16:40:26 +0100 | [diff] [blame] | 667 | 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] | 668 | return should_bail_out, files_to_lint |