Kevin Lubick | 7f7b6ab | 2021-08-16 15:00:15 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 2 | # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | |
| 7 | """Top-level presubmit script for Skia. |
| 8 | |
| 9 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 10 | for more details about the presubmit API built into gcl. |
| 11 | """ |
| 12 | |
commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 13 | import fnmatch |
rmistry@google.com | f6c5f75 | 2013-03-29 17:26:00 +0000 | [diff] [blame] | 14 | import os |
commit-bot@chromium.org | cfdc596 | 2014-01-31 17:33:04 +0000 | [diff] [blame] | 15 | import re |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 16 | import subprocess |
rmistry@google.com | f6c5f75 | 2013-03-29 17:26:00 +0000 | [diff] [blame] | 17 | import sys |
commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 18 | import traceback |
rmistry@google.com | f6c5f75 | 2013-03-29 17:26:00 +0000 | [diff] [blame] | 19 | |
rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 20 | |
Chris Mumford | be879e0 | 2023-04-24 09:07:20 -0700 | [diff] [blame] | 21 | RELEASE_NOTES_DIR = 'relnotes' |
Chris Mumford | b0dbd9e | 2023-03-29 08:15:18 -0700 | [diff] [blame] | 22 | RELEASE_NOTES_FILE_NAME = 'RELEASE_NOTES.md' |
Chris Mumford | be879e0 | 2023-04-24 09:07:20 -0700 | [diff] [blame] | 23 | RELEASE_NOTES_README = '//relnotes/README.md' |
commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 24 | |
rmistry | d88b0be | 2016-05-20 03:50:01 -0700 | [diff] [blame] | 25 | GOLD_TRYBOT_URL = 'https://gold.skia.org/search?issue=' |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 26 | |
Eric Boren | 1eec99c | 2018-04-26 13:09:48 -0400 | [diff] [blame] | 27 | SERVICE_ACCOUNT_SUFFIX = [ |
Eric Boren | 47ed6f1 | 2018-04-26 14:02:43 -0400 | [diff] [blame] | 28 | '@%s.iam.gserviceaccount.com' % project for project in [ |
Eric Boren | 6ad3ca4 | 2018-09-07 14:22:16 -0400 | [diff] [blame] | 29 | 'skia-buildbots.google.com', 'skia-swarming-bots', 'skia-public', |
Ravi Mistry | 53c4423 | 2019-03-12 08:51:42 -0400 | [diff] [blame] | 30 | 'skia-corp.google.com', 'chops-service-accounts']] |
Eric Boren | dd98829 | 2018-01-02 13:29:21 -0500 | [diff] [blame] | 31 | |
Kevin Lubick | 2f57626 | 2022-05-06 08:30:28 -0400 | [diff] [blame] | 32 | USE_PYTHON3 = True |
| 33 | |
rmistry@google.com | 547012d | 2013-04-12 19:45:46 +0000 | [diff] [blame] | 34 | |
rmistry@google.com | 713276b | 2013-01-25 18:27:34 +0000 | [diff] [blame] | 35 | def _CheckChangeHasEol(input_api, output_api, source_file_filter=None): |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 36 | """Checks that files end with at least one \n (LF).""" |
rmistry@google.com | 713276b | 2013-01-25 18:27:34 +0000 | [diff] [blame] | 37 | eof_files = [] |
| 38 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 39 | contents = input_api.ReadFile(f, 'rb') |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 40 | # Check that the file ends in at least one newline character. |
rmistry@google.com | 713276b | 2013-01-25 18:27:34 +0000 | [diff] [blame] | 41 | if len(contents) > 1 and contents[-1:] != '\n': |
| 42 | eof_files.append(f.LocalPath()) |
| 43 | |
| 44 | if eof_files: |
| 45 | return [output_api.PresubmitPromptWarning( |
| 46 | 'These files should end in a newline character:', |
| 47 | items=eof_files)] |
| 48 | return [] |
| 49 | |
| 50 | |
Ben Wagner | cf42e98 | 2018-02-09 17:41:20 -0500 | [diff] [blame] | 51 | def _JsonChecks(input_api, output_api): |
| 52 | """Run checks on any modified json files.""" |
| 53 | failing_files = [] |
| 54 | for affected_file in input_api.AffectedFiles(None): |
| 55 | affected_file_path = affected_file.LocalPath() |
| 56 | is_json = affected_file_path.endswith('.json') |
| 57 | is_metadata = (affected_file_path.startswith('site/') and |
| 58 | affected_file_path.endswith('/METADATA')) |
| 59 | if is_json or is_metadata: |
| 60 | try: |
| 61 | input_api.json.load(open(affected_file_path, 'r')) |
| 62 | except ValueError: |
| 63 | failing_files.append(affected_file_path) |
| 64 | |
| 65 | results = [] |
| 66 | if failing_files: |
| 67 | results.append( |
| 68 | output_api.PresubmitError( |
| 69 | 'The following files contain invalid json:\n%s\n\n' % |
| 70 | '\n'.join(failing_files))) |
| 71 | return results |
| 72 | |
| 73 | |
rmistry | 01cbf6c | 2015-03-12 07:48:40 -0700 | [diff] [blame] | 74 | def _IfDefChecks(input_api, output_api): |
| 75 | """Ensures if/ifdef are not before includes. See skbug/3362 for details.""" |
| 76 | comment_block_start_pattern = re.compile('^\s*\/\*.*$') |
| 77 | comment_block_middle_pattern = re.compile('^\s+\*.*') |
| 78 | comment_block_end_pattern = re.compile('^\s+\*\/.*$') |
| 79 | single_line_comment_pattern = re.compile('^\s*//.*$') |
| 80 | def is_comment(line): |
| 81 | return (comment_block_start_pattern.match(line) or |
| 82 | comment_block_middle_pattern.match(line) or |
| 83 | comment_block_end_pattern.match(line) or |
| 84 | single_line_comment_pattern.match(line)) |
| 85 | |
| 86 | empty_line_pattern = re.compile('^\s*$') |
| 87 | def is_empty_line(line): |
| 88 | return empty_line_pattern.match(line) |
| 89 | |
| 90 | failing_files = [] |
| 91 | for affected_file in input_api.AffectedSourceFiles(None): |
| 92 | affected_file_path = affected_file.LocalPath() |
| 93 | if affected_file_path.endswith('.cpp') or affected_file_path.endswith('.h'): |
| 94 | f = open(affected_file_path) |
James Godfrey-Kittle | 5d553ed | 2022-05-06 14:06:40 -0400 | [diff] [blame] | 95 | for line in f: |
rmistry | 01cbf6c | 2015-03-12 07:48:40 -0700 | [diff] [blame] | 96 | if is_comment(line) or is_empty_line(line): |
| 97 | continue |
| 98 | # The below will be the first real line after comments and newlines. |
| 99 | if line.startswith('#if 0 '): |
| 100 | pass |
| 101 | elif line.startswith('#if ') or line.startswith('#ifdef '): |
| 102 | failing_files.append(affected_file_path) |
| 103 | break |
| 104 | |
| 105 | results = [] |
| 106 | if failing_files: |
| 107 | results.append( |
| 108 | output_api.PresubmitError( |
| 109 | 'The following files have #if or #ifdef before includes:\n%s\n\n' |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 110 | 'See https://bug.skia.org/3362 for why this should be fixed.' % |
rmistry | 01cbf6c | 2015-03-12 07:48:40 -0700 | [diff] [blame] | 111 | '\n'.join(failing_files))) |
| 112 | return results |
| 113 | |
| 114 | |
borenet | c7c9180 | 2015-03-25 04:47:02 -0700 | [diff] [blame] | 115 | def _CopyrightChecks(input_api, output_api, source_file_filter=None): |
| 116 | results = [] |
| 117 | year_pattern = r'\d{4}' |
| 118 | year_range_pattern = r'%s(-%s)?' % (year_pattern, year_pattern) |
| 119 | years_pattern = r'%s(,%s)*,?' % (year_range_pattern, year_range_pattern) |
| 120 | copyright_pattern = ( |
| 121 | r'Copyright (\([cC]\) )?%s \w+' % years_pattern) |
| 122 | |
| 123 | for affected_file in input_api.AffectedSourceFiles(source_file_filter): |
John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 124 | if ('third_party/' in affected_file.LocalPath() or |
Kevin Lubick | fed97e8 | 2022-03-25 14:59:33 -0400 | [diff] [blame] | 125 | 'tests/sksl/' in affected_file.LocalPath() or |
Kevin Lubick | 1b1e64c | 2022-07-18 13:30:16 -0400 | [diff] [blame] | 126 | 'bazel/rbe/' in affected_file.LocalPath() or |
Chris Mumford | 4c967b7 | 2022-08-02 09:31:02 -0700 | [diff] [blame] | 127 | 'bazel/external/' in affected_file.LocalPath() or |
| 128 | 'bazel/exporter/interfaces/mocks/' in affected_file.LocalPath()): |
borenet | c7c9180 | 2015-03-25 04:47:02 -0700 | [diff] [blame] | 129 | continue |
| 130 | contents = input_api.ReadFile(affected_file, 'rb') |
| 131 | if not re.search(copyright_pattern, contents): |
| 132 | results.append(output_api.PresubmitError( |
| 133 | '%s is missing a correct copyright header.' % affected_file)) |
| 134 | return results |
| 135 | |
| 136 | |
borenet | 2dbbfa5 | 2016-10-14 06:32:09 -0700 | [diff] [blame] | 137 | def _InfraTests(input_api, output_api): |
| 138 | """Run the infra tests.""" |
borenet | 1ed2ae4 | 2016-07-26 11:52:17 -0700 | [diff] [blame] | 139 | results = [] |
mtklein | 3da80f5 | 2016-07-27 04:14:07 -0700 | [diff] [blame] | 140 | if not any(f.LocalPath().startswith('infra') |
| 141 | for f in input_api.AffectedFiles()): |
| 142 | return results |
| 143 | |
James Godfrey-Kittle | 5d553ed | 2022-05-06 14:06:40 -0400 | [diff] [blame] | 144 | cmd = ['python3', os.path.join('infra', 'bots', 'infra_tests.py')] |
borenet | 60b0a2d | 2016-10-04 12:45:41 -0700 | [diff] [blame] | 145 | try: |
| 146 | subprocess.check_output(cmd) |
| 147 | except subprocess.CalledProcessError as e: |
| 148 | results.append(output_api.PresubmitError( |
| 149 | '`%s` failed:\n%s' % (' '.join(cmd), e.output))) |
| 150 | return results |
| 151 | |
| 152 | |
mtklein | 4db3b79 | 2016-08-03 14:18:22 -0700 | [diff] [blame] | 153 | def _CheckGNFormatted(input_api, output_api): |
| 154 | """Make sure any .gn files we're changing have been formatted.""" |
Ben Wagner | 3c4a9d3 | 2020-02-14 14:28:33 -0500 | [diff] [blame] | 155 | files = [] |
Corentin Wallez | 6a5187a | 2020-04-08 10:24:04 +0200 | [diff] [blame] | 156 | for f in input_api.AffectedFiles(include_deletes=False): |
Ben Wagner | 3c4a9d3 | 2020-02-14 14:28:33 -0500 | [diff] [blame] | 157 | if (f.LocalPath().endswith('.gn') or |
| 158 | f.LocalPath().endswith('.gni')): |
| 159 | files.append(f) |
| 160 | if not files: |
| 161 | return [] |
mtklein | 4db3b79 | 2016-08-03 14:18:22 -0700 | [diff] [blame] | 162 | |
James Godfrey-Kittle | 5d553ed | 2022-05-06 14:06:40 -0400 | [diff] [blame] | 163 | cmd = ['python3', os.path.join('bin', 'fetch-gn')] |
Ben Wagner | 3c4a9d3 | 2020-02-14 14:28:33 -0500 | [diff] [blame] | 164 | try: |
| 165 | subprocess.check_output(cmd) |
| 166 | except subprocess.CalledProcessError as e: |
| 167 | return [output_api.PresubmitError( |
| 168 | '`%s` failed:\n%s' % (' '.join(cmd), e.output))] |
| 169 | |
| 170 | results = [] |
| 171 | for f in files: |
Brian Osman | 70f24af | 2020-02-18 15:08:27 -0500 | [diff] [blame] | 172 | gn = 'gn.exe' if 'win32' in sys.platform else 'gn' |
Ben Wagner | 06265e0 | 2020-02-13 19:02:46 -0500 | [diff] [blame] | 173 | gn = os.path.join(input_api.PresubmitLocalPath(), 'bin', gn) |
Mike Klein | 7a1c53d | 2016-10-11 14:03:06 -0400 | [diff] [blame] | 174 | cmd = [gn, 'format', '--dry-run', f.LocalPath()] |
mtklein | 4db3b79 | 2016-08-03 14:18:22 -0700 | [diff] [blame] | 175 | try: |
| 176 | subprocess.check_output(cmd) |
| 177 | except subprocess.CalledProcessError: |
Ben Wagner | 06265e0 | 2020-02-13 19:02:46 -0500 | [diff] [blame] | 178 | fix = 'bin/gn format ' + f.LocalPath() |
mtklein | 4db3b79 | 2016-08-03 14:18:22 -0700 | [diff] [blame] | 179 | results.append(output_api.PresubmitError( |
mtklein | d434b01 | 2016-08-10 07:30:58 -0700 | [diff] [blame] | 180 | '`%s` failed, try\n\t%s' % (' '.join(cmd), fix))) |
mtklein | 4db3b79 | 2016-08-03 14:18:22 -0700 | [diff] [blame] | 181 | return results |
| 182 | |
Ravi Mistry | 6eca579 | 2020-12-16 11:42:29 -0500 | [diff] [blame] | 183 | |
| 184 | def _CheckGitConflictMarkers(input_api, output_api): |
| 185 | pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$') |
| 186 | results = [] |
| 187 | for f in input_api.AffectedFiles(): |
| 188 | for line_num, line in f.ChangedContents(): |
| 189 | if f.LocalPath().endswith('.md'): |
| 190 | # First-level headers in markdown look a lot like version control |
| 191 | # conflict markers. http://daringfireball.net/projects/markdown/basics |
| 192 | continue |
| 193 | if pattern.match(line): |
| 194 | results.append( |
| 195 | output_api.PresubmitError( |
| 196 | 'Git conflict markers found in %s:%d %s' % ( |
| 197 | f.LocalPath(), line_num, line))) |
| 198 | return results |
| 199 | |
| 200 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 201 | def _CheckIncludesFormatted(input_api, output_api): |
| 202 | """Make sure #includes in files we're changing have been formatted.""" |
Mike Klein | f9ad5ba | 2019-07-29 12:34:39 -0500 | [diff] [blame] | 203 | files = [str(f) for f in input_api.AffectedFiles() if f.Action() != 'D'] |
James Godfrey-Kittle | 5d553ed | 2022-05-06 14:06:40 -0400 | [diff] [blame] | 204 | cmd = ['python3', |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 205 | 'tools/rewrite_includes.py', |
Mike Klein | f9ad5ba | 2019-07-29 12:34:39 -0500 | [diff] [blame] | 206 | '--dry-run'] + files |
Hal Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 207 | if 0 != subprocess.call(cmd): |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 208 | return [output_api.PresubmitError('`%s` failed' % ' '.join(cmd))] |
| 209 | return [] |
borenet | 1ed2ae4 | 2016-07-26 11:52:17 -0700 | [diff] [blame] | 210 | |
Eric Boren | 58d1f76 | 2019-07-19 08:07:44 -0400 | [diff] [blame] | 211 | |
Ben Wagner | 8885550 | 2017-10-12 17:55:19 -0400 | [diff] [blame] | 212 | class _WarningsAsErrors(): |
| 213 | def __init__(self, output_api): |
| 214 | self.output_api = output_api |
| 215 | self.old_warning = None |
| 216 | def __enter__(self): |
| 217 | self.old_warning = self.output_api.PresubmitPromptWarning |
| 218 | self.output_api.PresubmitPromptWarning = self.output_api.PresubmitError |
| 219 | return self.output_api |
| 220 | def __exit__(self, ex_type, ex_value, ex_traceback): |
| 221 | self.output_api.PresubmitPromptWarning = self.old_warning |
| 222 | |
| 223 | |
Kevin Lubick | 2cd8067 | 2021-07-01 11:03:36 -0400 | [diff] [blame] | 224 | def _RegenerateAllExamplesCPP(input_api, output_api): |
| 225 | """Regenerates all_examples.cpp if an example was added or deleted.""" |
| 226 | if not any(f.LocalPath().startswith('docs/examples/') |
| 227 | for f in input_api.AffectedFiles()): |
| 228 | return [] |
| 229 | command_str = 'tools/fiddle/make_all_examples_cpp.py' |
James Godfrey-Kittle | 5d553ed | 2022-05-06 14:06:40 -0400 | [diff] [blame] | 230 | cmd = ['python3', command_str] |
Kevin Lubick | 2cd8067 | 2021-07-01 11:03:36 -0400 | [diff] [blame] | 231 | if 0 != subprocess.call(cmd): |
| 232 | return [output_api.PresubmitError('`%s` failed' % ' '.join(cmd))] |
| 233 | |
| 234 | results = [] |
| 235 | git_diff_output = input_api.subprocess.check_output( |
| 236 | ['git', 'diff', '--no-ext-diff']) |
| 237 | if git_diff_output: |
| 238 | results += [output_api.PresubmitError( |
| 239 | 'Diffs found after running "%s":\n\n%s\n' |
| 240 | 'Please commit or discard the above changes.' % ( |
| 241 | command_str, |
| 242 | git_diff_output, |
| 243 | ) |
| 244 | )] |
| 245 | return results |
| 246 | |
Chris Mumford | 6f6793b | 2022-12-01 07:19:27 -0800 | [diff] [blame] | 247 | |
Kevin Lubick | d7dc6d7 | 2023-02-13 09:28:29 -0500 | [diff] [blame] | 248 | def _CheckExamplesForPrivateAPIs(input_api, output_api): |
| 249 | """We only want our checked-in examples (aka fiddles) to show public API.""" |
| 250 | banned_includes = [ |
| 251 | input_api.re.compile(r'#\s*include\s+("src/.*)'), |
| 252 | input_api.re.compile(r'#\s*include\s+("include/private/.*)'), |
| 253 | ] |
| 254 | file_filter = lambda x: (x.LocalPath().startswith('docs/examples/')) |
| 255 | errors = [] |
| 256 | for affected_file in input_api.AffectedSourceFiles(file_filter): |
| 257 | affected_filepath = affected_file.LocalPath() |
| 258 | for (line_num, line) in affected_file.ChangedContents(): |
| 259 | for re in banned_includes: |
| 260 | match = re.search(line) |
| 261 | if match: |
| 262 | errors.append('%s:%s: Fiddles should not use private/internal API like %s.' % ( |
| 263 | affected_filepath, line_num, match.group(1))) |
| 264 | |
| 265 | if errors: |
| 266 | return [output_api.PresubmitError('\n'.join(errors))] |
| 267 | return [] |
| 268 | |
| 269 | |
Chris Mumford | 6f6793b | 2022-12-01 07:19:27 -0800 | [diff] [blame] | 270 | def _CheckGeneratedBazelBUILDFiles(input_api, output_api): |
| 271 | if 'win32' in sys.platform: |
| 272 | # TODO(crbug.com/skia/12541): Remove when Bazel builds work on Windows. |
| 273 | # Note: `make` is not installed on Windows by default. |
| 274 | return [] |
Kevin Lubick | d568705 | 2022-12-02 10:25:26 -0500 | [diff] [blame] | 275 | if 'darwin' in sys.platform: |
| 276 | # This takes too long on Mac with default settings. Probably due to sandboxing. |
| 277 | return [] |
Chris Mumford | 6f6793b | 2022-12-01 07:19:27 -0800 | [diff] [blame] | 278 | for affected_file in input_api.AffectedFiles(include_deletes=True): |
| 279 | affected_file_path = affected_file.LocalPath() |
| 280 | if (affected_file_path.endswith('.go') or |
| 281 | affected_file_path.endswith('BUILD.bazel')): |
| 282 | return _RunCommandAndCheckGitDiff(output_api, |
| 283 | ['make', '-C', 'bazel', 'generate_go']) |
| 284 | return [] # No modified Go source files. |
| 285 | |
| 286 | |
Kevin Lubick | ad09d4b | 2022-05-04 09:59:13 -0400 | [diff] [blame] | 287 | def _CheckBazelBUILDFiles(input_api, output_api): |
| 288 | """Makes sure our BUILD.bazel files are compatible with G3.""" |
| 289 | results = [] |
Kevin Lubick | 269e687 | 2022-05-04 11:06:10 -0400 | [diff] [blame] | 290 | for affected_file in input_api.AffectedFiles(include_deletes=False): |
Kevin Lubick | ad09d4b | 2022-05-04 09:59:13 -0400 | [diff] [blame] | 291 | affected_file_path = affected_file.LocalPath() |
| 292 | is_bazel = affected_file_path.endswith('BUILD.bazel') |
Kevin Lubick | 3413ca4 | 2022-05-06 13:20:12 -0400 | [diff] [blame] | 293 | # This list lines up with the one in autoroller_lib.py (see G3). |
Kevin Lubick | c123b5a | 2022-05-27 15:12:31 -0400 | [diff] [blame] | 294 | excluded_paths = ["infra/", "bazel/rbe/", "bazel/external/", "bazel/common_config_settings/", |
Kevin Lubick | 12a3336 | 2022-10-05 16:47:13 -0400 | [diff] [blame] | 295 | "modules/canvaskit/go/", "experimental/", "bazel/platform", "third_party/", |
Kevin Lubick | bcfa3ef | 2022-11-07 10:18:09 -0500 | [diff] [blame] | 296 | "tests/", "resources/", "bazel/deps_parser/", "bazel/exporter_tool/", |
Kevin Lubick | b80e74d | 2023-05-25 08:18:17 -0400 | [diff] [blame] | 297 | "tools/gpu/gl/interface/", "bazel/utils/", "include/config/", |
Kevin Lubick | 6f4fd97 | 2023-08-29 10:44:24 -0400 | [diff] [blame] | 298 | "bench/", "example/external_client/"] |
Kevin Lubick | 3413ca4 | 2022-05-06 13:20:12 -0400 | [diff] [blame] | 299 | is_excluded = any(affected_file_path.startswith(n) for n in excluded_paths) |
| 300 | if is_bazel and not is_excluded: |
Kevin Lubick | ad09d4b | 2022-05-04 09:59:13 -0400 | [diff] [blame] | 301 | with open(affected_file_path, 'r') as file: |
| 302 | contents = file.read() |
Kevin Lubick | ee62fad | 2022-06-01 14:45:46 -0400 | [diff] [blame] | 303 | if 'exports_files_legacy(' not in contents: |
Kevin Lubick | ad09d4b | 2022-05-04 09:59:13 -0400 | [diff] [blame] | 304 | results.append(output_api.PresubmitError( |
| 305 | ('%s needs to call exports_files_legacy() to support legacy G3 ' + |
| 306 | 'rules.\nPut this near the top of the file, beneath ' + |
| 307 | 'licenses(["notice"]).') % affected_file_path |
| 308 | )) |
| 309 | if 'licenses(["notice"])' not in contents: |
| 310 | results.append(output_api.PresubmitError( |
| 311 | ('%s needs to have\nlicenses(["notice"])\nimmediately after ' + |
| 312 | 'the load() calls to comply with G3 policies.') % affected_file_path |
| 313 | )) |
Kevin Lubick | 25a8f60 | 2022-07-28 16:03:56 -0400 | [diff] [blame] | 314 | if 'cc_library(' in contents and '"skia_cc_library"' not in contents: |
Kevin Lubick | ad09d4b | 2022-05-04 09:59:13 -0400 | [diff] [blame] | 315 | results.append(output_api.PresubmitError( |
Kevin Lubick | 25a8f60 | 2022-07-28 16:03:56 -0400 | [diff] [blame] | 316 | ('%s needs to load skia_cc_library from macros.bzl instead of using the ' + |
Kevin Lubick | ad09d4b | 2022-05-04 09:59:13 -0400 | [diff] [blame] | 317 | 'native one. This allows us to build differently for G3.\n' + |
Kevin Lubick | 25a8f60 | 2022-07-28 16:03:56 -0400 | [diff] [blame] | 318 | 'Add "skia_cc_library" to load("//bazel:macros.bzl", ...)') |
Kevin Lubick | ad09d4b | 2022-05-04 09:59:13 -0400 | [diff] [blame] | 319 | % affected_file_path |
| 320 | )) |
| 321 | return results |
| 322 | |
Kevin Lubick | 8d9d9fa | 2022-05-17 13:43:52 -0400 | [diff] [blame] | 323 | |
| 324 | def _CheckPublicBzl(input_api, output_api): |
| 325 | """Reminds devs to add/remove files from public.bzl.""" |
| 326 | results = [] |
| 327 | public_bzl = '' |
| 328 | with open('public.bzl', 'r', encoding='utf-8') as f: |
| 329 | public_bzl = f.read().strip() |
| 330 | for affected_file in input_api.AffectedFiles(include_deletes=True): |
| 331 | # action is A for newly added, D for newly deleted, M for modified |
| 332 | action = affected_file.Action() |
| 333 | affected_file_path = affected_file.LocalPath() |
| 334 | if ((affected_file_path.startswith("include") or affected_file_path.startswith("src")) and |
| 335 | (affected_file_path.endswith(".cpp") or affected_file_path.endswith(".h"))): |
| 336 | affected_file_path = '"' + affected_file_path + '"' |
| 337 | if action == "D" and affected_file_path in public_bzl: |
| 338 | results.append(output_api.PresubmitError( |
| 339 | "Need to delete %s from public.bzl (or rename it)" % affected_file_path)) |
| 340 | elif action == "A" and affected_file_path not in public_bzl: |
| 341 | results.append(output_api.PresubmitPromptWarning( |
| 342 | "You may need to add %s to public.bzl" % affected_file_path)) |
| 343 | return results |
| 344 | |
| 345 | |
Kevin Lubick | 2c65579 | 2022-05-27 13:56:03 -0400 | [diff] [blame] | 346 | def _RunCommandAndCheckGitDiff(output_api, command): |
| 347 | """Run an arbitrary command. Fail if it produces any diffs.""" |
| 348 | command_str = ' '.join(command) |
| 349 | results = [] |
| 350 | |
| 351 | try: |
| 352 | output = subprocess.check_output( |
| 353 | command, |
| 354 | stderr=subprocess.STDOUT, encoding='utf-8') |
| 355 | except subprocess.CalledProcessError as e: |
| 356 | results += [output_api.PresubmitError( |
| 357 | 'Command "%s" returned non-zero exit code %d. Output: \n\n%s' % ( |
| 358 | command_str, |
| 359 | e.returncode, |
| 360 | e.output, |
| 361 | ) |
| 362 | )] |
| 363 | |
| 364 | git_diff_output = subprocess.check_output( |
| 365 | ['git', 'diff', '--no-ext-diff'], encoding='utf-8') |
| 366 | if git_diff_output: |
| 367 | results += [output_api.PresubmitError( |
| 368 | 'Diffs found after running "%s":\n\n%s\n' |
| 369 | 'Please commit or discard the above changes.' % ( |
| 370 | command_str, |
| 371 | git_diff_output, |
| 372 | ) |
| 373 | )] |
| 374 | |
| 375 | return results |
| 376 | |
| 377 | |
Chris Mumford | 008981f | 2022-12-01 08:29:24 -0800 | [diff] [blame] | 378 | def _CheckGNIGenerated(input_api, output_api): |
| 379 | """Ensures that the generated *.gni files are current. |
| 380 | |
| 381 | The Bazel project files are authoritative and some *.gni files are |
| 382 | generated from them using the exporter_tool. This check ensures they |
| 383 | are still current. |
| 384 | """ |
| 385 | if 'win32' in sys.platform: |
| 386 | # TODO(crbug.com/skia/12541): Remove when Bazel builds work on Windows. |
| 387 | # Note: `make` is not installed on Windows by default. |
| 388 | return [ |
| 389 | output_api.PresubmitPromptWarning( |
| 390 | 'Skipping Bazel=>GNI export check on Windows (unsupported platform).' |
| 391 | ) |
| 392 | ] |
Kevin Lubick | ad28b6c | 2022-12-02 13:42:17 -0500 | [diff] [blame] | 393 | if 'darwin' in sys.platform: |
| 394 | # This takes too long on Mac with default settings. Probably due to sandboxing. |
| 395 | return [] |
Kevin Lubick | a2630de | 2023-03-13 16:15:27 -0400 | [diff] [blame] | 396 | should_run = False |
Chris Mumford | 008981f | 2022-12-01 08:29:24 -0800 | [diff] [blame] | 397 | for affected_file in input_api.AffectedFiles(include_deletes=True): |
| 398 | affected_file_path = affected_file.LocalPath() |
| 399 | if affected_file_path.endswith('BUILD.bazel') or affected_file_path.endswith('.gni'): |
Kevin Lubick | a2630de | 2023-03-13 16:15:27 -0400 | [diff] [blame] | 400 | should_run = True |
| 401 | # Generate GNI files and verify no changes. |
| 402 | if should_run: |
| 403 | return _RunCommandAndCheckGitDiff(output_api, |
| 404 | ['make', '-C', 'bazel', 'generate_gni']) |
Chris Mumford | 008981f | 2022-12-01 08:29:24 -0800 | [diff] [blame] | 405 | |
| 406 | # No Bazel build files changed. |
| 407 | return [] |
| 408 | |
| 409 | |
Kevin Lubick | 2c65579 | 2022-05-27 13:56:03 -0400 | [diff] [blame] | 410 | def _CheckBuildifier(input_api, output_api): |
| 411 | """Runs Buildifier and fails on linting errors, or if it produces any diffs. |
| 412 | |
| 413 | This check only runs if the affected files include any WORKSPACE, BUILD, |
| 414 | BUILD.bazel or *.bzl files. |
| 415 | """ |
| 416 | files = [] |
Leandro Lovisolo | 4c099aa | 2023-11-15 00:22:21 +0000 | [diff] [blame] | 417 | # Please keep the below exclude patterns in sync with those in the //:buildifier rule definition. |
Kevin Lubick | 2c65579 | 2022-05-27 13:56:03 -0400 | [diff] [blame] | 418 | for affected_file in input_api.AffectedFiles(include_deletes=False): |
| 419 | affected_file_path = affected_file.LocalPath() |
| 420 | if affected_file_path.endswith('BUILD.bazel') or affected_file_path.endswith('.bzl'): |
Leandro Lovisolo | 17b801d | 2023-08-14 18:20:58 +0000 | [diff] [blame] | 421 | if not affected_file_path.endswith('public.bzl') and \ |
| 422 | not affected_file_path.endswith('go_repositories.bzl') and \ |
Leandro Lovisolo | 4c099aa | 2023-11-15 00:22:21 +0000 | [diff] [blame] | 423 | not "bazel/rbe/gce_linux/" in affected_file_path and \ |
| 424 | not affected_file_path.startswith("third_party/externals/") and \ |
| 425 | not "node_modules/" in affected_file_path: # Skip generated files. |
Kevin Lubick | d0519f1 | 2022-06-02 15:42:58 -0400 | [diff] [blame] | 426 | files.append(affected_file_path) |
Kevin Lubick | 2c65579 | 2022-05-27 13:56:03 -0400 | [diff] [blame] | 427 | if not files: |
| 428 | return [] |
| 429 | try: |
| 430 | subprocess.check_output( |
| 431 | ['buildifier', '--version'], |
| 432 | stderr=subprocess.STDOUT) |
Kevin Lubick | d39593d | 2022-06-02 15:22:29 -0400 | [diff] [blame] | 433 | except: |
| 434 | return [output_api.PresubmitNotifyResult( |
Kevin Lubick | 2c65579 | 2022-05-27 13:56:03 -0400 | [diff] [blame] | 435 | 'Skipping buildifier check because it is not on PATH. \n' + |
Kevin Lubick | d39593d | 2022-06-02 15:22:29 -0400 | [diff] [blame] | 436 | 'You can download it from https://github.com/bazelbuild/buildtools/releases')] |
Kevin Lubick | 2c65579 | 2022-05-27 13:56:03 -0400 | [diff] [blame] | 437 | |
| 438 | return _RunCommandAndCheckGitDiff( |
| 439 | # One can change --lint=warn to --lint=fix to have things automatically fixed where possible. |
| 440 | # However, --lint=fix will not cause a presubmit error if there are things that require |
| 441 | # manual intervention, so we leave --lint=warn on by default. |
Leandro Lovisolo | 4c099aa | 2023-11-15 00:22:21 +0000 | [diff] [blame] | 442 | # |
| 443 | # Please keep the below arguments in sync with those in the //:buildifier rule definition. |
| 444 | output_api, [ |
| 445 | 'buildifier', |
| 446 | '--mode=fix', |
| 447 | '--lint=warn', |
| 448 | '--warnings', |
| 449 | ','.join([ |
| 450 | '-native-android', |
| 451 | '-native-cc', |
| 452 | '-native-py', |
| 453 | ]) |
| 454 | ] + files) |
Kevin Lubick | 2c65579 | 2022-05-27 13:56:03 -0400 | [diff] [blame] | 455 | |
| 456 | |
Kevin Lubick | 07e7bc1 | 2022-10-03 09:18:35 -0400 | [diff] [blame] | 457 | def _CheckBannedAPIs(input_api, output_api): |
| 458 | """Check source code for functions and packages that should not be used.""" |
| 459 | |
| 460 | # A list of tuples of a regex to match an API and a suggested replacement for |
| 461 | # that API. There is an optional third parameter for files which *can* use this |
| 462 | # API without warning. |
| 463 | banned_replacements = [ |
| 464 | (r'std::stof\(', 'std::strtof(), which does not throw'), |
| 465 | (r'std::stod\(', 'std::strtod(), which does not throw'), |
| 466 | (r'std::stold\(', 'std::strtold(), which does not throw'), |
| 467 | ] |
| 468 | |
Kevin Lubick | 7f92cf8 | 2023-02-28 14:18:00 -0500 | [diff] [blame] | 469 | # These defines are either there or not, and using them with just an #if is a |
| 470 | # subtle, frustrating bug. |
John Stiles | e9e4d0d | 2023-08-28 18:31:47 -0400 | [diff] [blame] | 471 | existence_defines = ['SK_GANESH', 'SK_GRAPHITE', 'SK_GL', 'SK_VULKAN', 'SK_DAWN', 'SK_METAL', |
| 472 | 'SK_DIRECT3D', 'SK_DEBUG', 'GR_TEST_UTILS', 'GRAPHITE_TEST_UTILS'] |
Kevin Lubick | 7f92cf8 | 2023-02-28 14:18:00 -0500 | [diff] [blame] | 473 | for d in existence_defines: |
| 474 | banned_replacements.append(('#if {}'.format(d), |
| 475 | '#if defined({})'.format(d))) |
Kevin Lubick | 07e7bc1 | 2022-10-03 09:18:35 -0400 | [diff] [blame] | 476 | compiled_replacements = [] |
| 477 | for rep in banned_replacements: |
| 478 | exceptions = [] |
| 479 | if len(rep) == 3: |
| 480 | (re, replacement, exceptions) = rep |
| 481 | else: |
| 482 | (re, replacement) = rep |
| 483 | |
| 484 | compiled_re = input_api.re.compile(re) |
| 485 | compiled_exceptions = [input_api.re.compile(exc) for exc in exceptions] |
| 486 | compiled_replacements.append( |
| 487 | (compiled_re, replacement, compiled_exceptions)) |
| 488 | |
| 489 | errors = [] |
| 490 | file_filter = lambda x: (x.LocalPath().endswith('.h') or |
| 491 | x.LocalPath().endswith('.cpp') or |
| 492 | x.LocalPath().endswith('.cc') or |
| 493 | x.LocalPath().endswith('.m') or |
| 494 | x.LocalPath().endswith('.mm')) |
| 495 | for affected_file in input_api.AffectedSourceFiles(file_filter): |
| 496 | affected_filepath = affected_file.LocalPath() |
| 497 | for (line_num, line) in affected_file.ChangedContents(): |
| 498 | for (re, replacement, exceptions) in compiled_replacements: |
| 499 | match = re.search(line) |
| 500 | if match: |
| 501 | for exc in exceptions: |
| 502 | if exc.search(affected_filepath): |
| 503 | break |
| 504 | else: |
| 505 | errors.append('%s:%s: Instead of %s, please use %s.' % ( |
| 506 | affected_filepath, line_num, match.group(), replacement)) |
| 507 | |
| 508 | if errors: |
| 509 | return [output_api.PresubmitError('\n'.join(errors))] |
| 510 | |
| 511 | return [] |
| 512 | |
| 513 | |
Kevin Lubick | 2afb9c4 | 2022-11-01 14:30:10 -0400 | [diff] [blame] | 514 | def _CheckDEPS(input_api, output_api): |
| 515 | """If DEPS was modified, run the deps_parser to update bazel/deps.bzl""" |
| 516 | needs_running = False |
| 517 | for affected_file in input_api.AffectedFiles(include_deletes=False): |
| 518 | affected_file_path = affected_file.LocalPath() |
| 519 | if affected_file_path.endswith('DEPS') or affected_file_path.endswith('deps.bzl'): |
| 520 | needs_running = True |
| 521 | break |
| 522 | if not needs_running: |
| 523 | return [] |
| 524 | try: |
| 525 | subprocess.check_output( |
| 526 | ['bazelisk', '--version'], |
| 527 | stderr=subprocess.STDOUT) |
| 528 | except: |
| 529 | return [output_api.PresubmitNotifyResult( |
| 530 | 'Skipping DEPS check because bazelisk is not on PATH. \n' + |
| 531 | 'You can download it from https://github.com/bazelbuild/bazelisk/releases/tag/v1.14.0')] |
| 532 | |
| 533 | return _RunCommandAndCheckGitDiff( |
| 534 | output_api, ['bazelisk', 'run', '//bazel/deps_parser']) |
| 535 | |
| 536 | |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 537 | def _CommonChecks(input_api, output_api): |
| 538 | """Presubmit checks common to upload and commit.""" |
| 539 | results = [] |
| 540 | sources = lambda x: (x.LocalPath().endswith('.h') or |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 541 | x.LocalPath().endswith('.py') or |
| 542 | x.LocalPath().endswith('.sh') or |
mtklein | 18e5580 | 2015-03-25 07:21:20 -0700 | [diff] [blame] | 543 | x.LocalPath().endswith('.m') or |
| 544 | x.LocalPath().endswith('.mm') or |
| 545 | x.LocalPath().endswith('.go') or |
| 546 | x.LocalPath().endswith('.c') or |
| 547 | x.LocalPath().endswith('.cc') or |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 548 | x.LocalPath().endswith('.cpp')) |
Ben Wagner | 8885550 | 2017-10-12 17:55:19 -0400 | [diff] [blame] | 549 | results.extend(_CheckChangeHasEol( |
| 550 | input_api, output_api, source_file_filter=sources)) |
| 551 | with _WarningsAsErrors(output_api): |
| 552 | results.extend(input_api.canned_checks.CheckChangeHasNoCR( |
| 553 | input_api, output_api, source_file_filter=sources)) |
| 554 | results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace( |
| 555 | input_api, output_api, source_file_filter=sources)) |
Ben Wagner | cf42e98 | 2018-02-09 17:41:20 -0500 | [diff] [blame] | 556 | results.extend(_JsonChecks(input_api, output_api)) |
rmistry | 01cbf6c | 2015-03-12 07:48:40 -0700 | [diff] [blame] | 557 | results.extend(_IfDefChecks(input_api, output_api)) |
borenet | c7c9180 | 2015-03-25 04:47:02 -0700 | [diff] [blame] | 558 | results.extend(_CopyrightChecks(input_api, output_api, |
| 559 | source_file_filter=sources)) |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 560 | results.extend(_CheckIncludesFormatted(input_api, output_api)) |
Mike Klein | 96f6401 | 2020-04-03 10:59:37 -0500 | [diff] [blame] | 561 | results.extend(_CheckGNFormatted(input_api, output_api)) |
Ravi Mistry | 6eca579 | 2020-12-16 11:42:29 -0500 | [diff] [blame] | 562 | results.extend(_CheckGitConflictMarkers(input_api, output_api)) |
Kevin Lubick | 2cd8067 | 2021-07-01 11:03:36 -0400 | [diff] [blame] | 563 | results.extend(_RegenerateAllExamplesCPP(input_api, output_api)) |
Kevin Lubick | d7dc6d7 | 2023-02-13 09:28:29 -0500 | [diff] [blame] | 564 | results.extend(_CheckExamplesForPrivateAPIs(input_api, output_api)) |
Kevin Lubick | ad09d4b | 2022-05-04 09:59:13 -0400 | [diff] [blame] | 565 | results.extend(_CheckBazelBUILDFiles(input_api, output_api)) |
Kevin Lubick | 07e7bc1 | 2022-10-03 09:18:35 -0400 | [diff] [blame] | 566 | results.extend(_CheckBannedAPIs(input_api, output_api)) |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 567 | return results |
| 568 | |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 569 | |
| 570 | def CheckChangeOnUpload(input_api, output_api): |
Ravi Mistry | 4c0ffe7 | 2020-03-02 13:19:02 -0500 | [diff] [blame] | 571 | """Presubmit checks for the change on upload.""" |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 572 | results = [] |
| 573 | results.extend(_CommonChecks(input_api, output_api)) |
borenet | 1ed2ae4 | 2016-07-26 11:52:17 -0700 | [diff] [blame] | 574 | # Run on upload, not commit, since the presubmit bot apparently doesn't have |
borenet | 60b0a2d | 2016-10-04 12:45:41 -0700 | [diff] [blame] | 575 | # coverage or Go installed. |
borenet | 2dbbfa5 | 2016-10-14 06:32:09 -0700 | [diff] [blame] | 576 | results.extend(_InfraTests(input_api, output_api)) |
Chris Mumford | be879e0 | 2023-04-24 09:07:20 -0700 | [diff] [blame] | 577 | results.extend(_CheckTopReleaseNotesChanged(input_api, output_api)) |
Ravi Mistry | 5773516 | 2019-07-25 13:45:15 -0400 | [diff] [blame] | 578 | results.extend(_CheckReleaseNotesForPublicAPI(input_api, output_api)) |
Kevin Lubick | 8d9d9fa | 2022-05-17 13:43:52 -0400 | [diff] [blame] | 579 | # Only check public.bzl on upload because new files are likely to be a source |
| 580 | # of false positives and we don't want to unnecessarily block commits. |
| 581 | results.extend(_CheckPublicBzl(input_api, output_api)) |
Kevin Lubick | 2c65579 | 2022-05-27 13:56:03 -0400 | [diff] [blame] | 582 | # Buildifier might not be on the CI machines. |
| 583 | results.extend(_CheckBuildifier(input_api, output_api)) |
Kevin Lubick | 2afb9c4 | 2022-11-01 14:30:10 -0400 | [diff] [blame] | 584 | # We don't want this to block the CQ (for now). |
| 585 | results.extend(_CheckDEPS(input_api, output_api)) |
Kevin Lubick | dd0bfa0 | 2022-12-02 08:37:58 -0500 | [diff] [blame] | 586 | # Bazelisk is not yet included in the Presubmit job. |
| 587 | results.extend(_CheckGeneratedBazelBUILDFiles(input_api, output_api)) |
Chris Mumford | 008981f | 2022-12-01 08:29:24 -0800 | [diff] [blame] | 588 | results.extend(_CheckGNIGenerated(input_api, output_api)) |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 589 | return results |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 590 | |
| 591 | |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 592 | class CodeReview(object): |
| 593 | """Abstracts which codereview tool is used for the specified issue.""" |
| 594 | |
| 595 | def __init__(self, input_api): |
| 596 | self._issue = input_api.change.issue |
| 597 | self._gerrit = input_api.gerrit |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 598 | |
| 599 | def GetOwnerEmail(self): |
Aaron Gable | a49909a | 2017-10-09 12:50:52 -0700 | [diff] [blame] | 600 | return self._gerrit.GetChangeOwner(self._issue) |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 601 | |
| 602 | def GetSubject(self): |
Aaron Gable | a49909a | 2017-10-09 12:50:52 -0700 | [diff] [blame] | 603 | return self._gerrit.GetChangeInfo(self._issue)['subject'] |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 604 | |
| 605 | def GetDescription(self): |
Aaron Gable | a49909a | 2017-10-09 12:50:52 -0700 | [diff] [blame] | 606 | return self._gerrit.GetChangeDescription(self._issue) |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 607 | |
Ravi Mistry | 39eabb6 | 2016-10-05 08:41:12 -0400 | [diff] [blame] | 608 | def GetReviewers(self): |
Aaron Gable | a49909a | 2017-10-09 12:50:52 -0700 | [diff] [blame] | 609 | code_review_label = ( |
| 610 | self._gerrit.GetChangeInfo(self._issue)['labels']['Code-Review']) |
| 611 | return [r['email'] for r in code_review_label.get('all', [])] |
Ravi Mistry | 39eabb6 | 2016-10-05 08:41:12 -0400 | [diff] [blame] | 612 | |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 613 | def GetApprovers(self): |
| 614 | approvers = [] |
Aaron Gable | a49909a | 2017-10-09 12:50:52 -0700 | [diff] [blame] | 615 | code_review_label = ( |
| 616 | self._gerrit.GetChangeInfo(self._issue)['labels']['Code-Review']) |
| 617 | for m in code_review_label.get('all', []): |
| 618 | if m.get("value") == 1: |
| 619 | approvers.append(m["email"]) |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 620 | return approvers |
| 621 | |
| 622 | |
Ravi Mistry | 5773516 | 2019-07-25 13:45:15 -0400 | [diff] [blame] | 623 | def _CheckReleaseNotesForPublicAPI(input_api, output_api): |
Chris Mumford | be879e0 | 2023-04-24 09:07:20 -0700 | [diff] [blame] | 624 | """Checks to see if a release notes file is added or edited with public API changes.""" |
Ravi Mistry | 5773516 | 2019-07-25 13:45:15 -0400 | [diff] [blame] | 625 | results = [] |
| 626 | public_api_changed = False |
| 627 | release_file_changed = False |
| 628 | for affected_file in input_api.AffectedFiles(): |
| 629 | affected_file_path = affected_file.LocalPath() |
| 630 | file_path, file_ext = os.path.splitext(affected_file_path) |
| 631 | # We only care about files that end in .h and are under the top-level |
| 632 | # include dir, but not include/private. |
| 633 | if (file_ext == '.h' and |
Chris Mumford | 0e8fd7b | 2023-04-24 14:04:54 -0700 | [diff] [blame] | 634 | file_path.split(os.path.sep)[0] == 'include' and |
Ravi Mistry | 5773516 | 2019-07-25 13:45:15 -0400 | [diff] [blame] | 635 | 'private' not in file_path): |
| 636 | public_api_changed = True |
Chris Mumford | be879e0 | 2023-04-24 09:07:20 -0700 | [diff] [blame] | 637 | elif os.path.dirname(file_path) == RELEASE_NOTES_DIR: |
Ravi Mistry | 5773516 | 2019-07-25 13:45:15 -0400 | [diff] [blame] | 638 | release_file_changed = True |
| 639 | |
| 640 | if public_api_changed and not release_file_changed: |
| 641 | results.append(output_api.PresubmitPromptWarning( |
Chris Mumford | be879e0 | 2023-04-24 09:07:20 -0700 | [diff] [blame] | 642 | 'If this change affects a client API, please add a new summary ' |
| 643 | 'file in the %s directory. More information can be found in ' |
| 644 | '%s.' % (RELEASE_NOTES_DIR, RELEASE_NOTES_README))) |
| 645 | return results |
| 646 | |
| 647 | |
| 648 | def _CheckTopReleaseNotesChanged(input_api, output_api): |
| 649 | """Warns if the top level release notes file was changed. |
| 650 | |
| 651 | The top level file is now auto-edited, and new release notes should |
| 652 | be added to the RELEASE_NOTES_DIR directory""" |
| 653 | results = [] |
| 654 | top_relnotes_changed = False |
| 655 | release_file_changed = False |
| 656 | for affected_file in input_api.AffectedFiles(): |
| 657 | affected_file_path = affected_file.LocalPath() |
| 658 | file_path, file_ext = os.path.splitext(affected_file_path) |
| 659 | if affected_file_path == RELEASE_NOTES_FILE_NAME: |
| 660 | top_relnotes_changed = True |
| 661 | elif os.path.dirname(file_path) == RELEASE_NOTES_DIR: |
| 662 | release_file_changed = True |
| 663 | # When relnotes_util is run it will modify RELEASE_NOTES_FILE_NAME |
| 664 | # and delete the individual note files in RELEASE_NOTES_DIR. |
| 665 | # So, if both paths are modified do not emit a warning. |
| 666 | if top_relnotes_changed and not release_file_changed: |
| 667 | results.append(output_api.PresubmitPromptWarning( |
| 668 | 'Do not edit %s directly. %s is automatically edited during the ' |
| 669 | 'release process. Release notes should be added as new files in ' |
| 670 | 'the %s directory. More information can be found in %s.' % (RELEASE_NOTES_FILE_NAME, |
| 671 | RELEASE_NOTES_FILE_NAME, |
| 672 | RELEASE_NOTES_DIR, |
| 673 | RELEASE_NOTES_README))) |
Ravi Mistry | 5773516 | 2019-07-25 13:45:15 -0400 | [diff] [blame] | 674 | return results |
| 675 | |
| 676 | |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 677 | def PostUploadHook(gerrit, change, output_api): |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 678 | """git cl upload will call this hook after the issue is created/modified. |
| 679 | |
| 680 | This hook does the following: |
| 681 | * Adds a link to preview docs changes if there are any docs changes in the CL. |
Ravi Mistry | 355feab | 2017-05-23 14:24:08 -0400 | [diff] [blame] | 682 | * Adds 'No-Try: true' if the CL contains only docs changes. |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 683 | """ |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 684 | if not change.issue: |
| 685 | return [] |
| 686 | |
| 687 | # Skip PostUploadHooks for all auto-commit service account bots. New |
| 688 | # patchsets (caused due to PostUploadHooks) invalidates the CQ+2 vote from |
| 689 | # the "--use-commit-queue" flag to "git cl upload". |
| 690 | for suffix in SERVICE_ACCOUNT_SUFFIX: |
| 691 | if change.author_email.endswith(suffix): |
| 692 | return [] |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 693 | |
| 694 | results = [] |
Ravi Mistry | 27095f2 | 2021-04-22 12:51:49 +0000 | [diff] [blame] | 695 | at_least_one_docs_change = False |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 696 | all_docs_changes = True |
| 697 | for affected_file in change.AffectedFiles(): |
| 698 | affected_file_path = affected_file.LocalPath() |
| 699 | file_path, _ = os.path.splitext(affected_file_path) |
Ravi Mistry | 27095f2 | 2021-04-22 12:51:49 +0000 | [diff] [blame] | 700 | if 'site' == file_path.split(os.path.sep)[0]: |
| 701 | at_least_one_docs_change = True |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 702 | else: |
| 703 | all_docs_changes = False |
Ravi Mistry | 27095f2 | 2021-04-22 12:51:49 +0000 | [diff] [blame] | 704 | if at_least_one_docs_change and not all_docs_changes: |
| 705 | break |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 706 | |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 707 | footers = change.GitFootersFromDescription() |
| 708 | description_changed = False |
Ravi Mistry | b5e2acc | 2017-12-07 11:10:11 -0500 | [diff] [blame] | 709 | |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 710 | # If the change includes only doc changes then add No-Try: true in the |
| 711 | # CL's description if it does not exist yet. |
| 712 | if all_docs_changes and 'true' not in footers.get('No-Try', []): |
| 713 | description_changed = True |
Edward Lemur | c631b7c | 2020-02-04 15:30:18 -0500 | [diff] [blame] | 714 | change.AddDescriptionFooter('No-Try', 'true') |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 715 | results.append( |
| 716 | output_api.PresubmitNotifyResult( |
| 717 | 'This change has only doc changes. Automatically added ' |
| 718 | '\'No-Try: true\' to the CL\'s description')) |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 719 | |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 720 | # If the description has changed update it. |
| 721 | if description_changed: |
| 722 | gerrit.UpdateDescription( |
| 723 | change.FullDescriptionText(), change.issue) |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 724 | |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 725 | return results |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 726 | |
| 727 | |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 728 | def CheckChangeOnCommit(input_api, output_api): |
Ravi Mistry | 4c0ffe7 | 2020-03-02 13:19:02 -0500 | [diff] [blame] | 729 | """Presubmit checks for the change on commit.""" |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 730 | results = [] |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 731 | results.extend(_CommonChecks(input_api, output_api)) |
Ravi Mistry | a70cb8a | 2017-09-12 13:52:05 -0400 | [diff] [blame] | 732 | # Checks for the presence of 'DO NOT''SUBMIT' in CL description and in |
| 733 | # content of files. |
| 734 | results.extend( |
| 735 | input_api.canned_checks.CheckDoNotSubmit(input_api, output_api)) |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 736 | return results |