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 | |
commit-bot@chromium.org | cfdc596 | 2014-01-31 17:33:04 +0000 | [diff] [blame] | 21 | REVERT_CL_SUBJECT_PREFIX = 'Revert ' |
| 22 | |
rmistry | f2d83ca | 2014-08-26 10:30:29 -0700 | [diff] [blame] | 23 | # Please add the complete email address here (and not just 'xyz@' or 'xyz'). |
rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 24 | PUBLIC_API_OWNERS = ( |
Mike Klein | 3f041f8 | 2021-02-03 09:36:14 -0600 | [diff] [blame] | 25 | 'brianosman@google.com', |
rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 26 | 'bsalomon@google.com', |
rmistry | 83fab47 | 2014-07-18 05:25:56 -0700 | [diff] [blame] | 27 | 'djsollen@chromium.org', |
| 28 | 'djsollen@google.com', |
Ravi Mistry | fbff329 | 2017-01-19 12:00:08 -0500 | [diff] [blame] | 29 | 'hcm@chromium.org', |
| 30 | 'hcm@google.com', |
Mike Klein | 3f041f8 | 2021-02-03 09:36:14 -0600 | [diff] [blame] | 31 | 'reed@chromium.org', |
| 32 | 'reed@google.com', |
rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 33 | ) |
| 34 | |
commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 35 | AUTHORS_FILE_NAME = 'AUTHORS' |
Ravi Mistry | 5773516 | 2019-07-25 13:45:15 -0400 | [diff] [blame] | 36 | RELEASE_NOTES_FILE_NAME = 'RELEASE_NOTES.txt' |
commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 37 | |
Ravi Mistry | 27095f2 | 2021-04-22 12:51:49 +0000 | [diff] [blame] | 38 | DOCS_PREVIEW_URL = 'https://skia.org/?cl={issue}' |
rmistry | d88b0be | 2016-05-20 03:50:01 -0700 | [diff] [blame] | 39 | GOLD_TRYBOT_URL = 'https://gold.skia.org/search?issue=' |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 40 | |
Eric Boren | 1eec99c | 2018-04-26 13:09:48 -0400 | [diff] [blame] | 41 | SERVICE_ACCOUNT_SUFFIX = [ |
Eric Boren | 47ed6f1 | 2018-04-26 14:02:43 -0400 | [diff] [blame] | 42 | '@%s.iam.gserviceaccount.com' % project for project in [ |
Eric Boren | 6ad3ca4 | 2018-09-07 14:22:16 -0400 | [diff] [blame] | 43 | 'skia-buildbots.google.com', 'skia-swarming-bots', 'skia-public', |
Ravi Mistry | 53c4423 | 2019-03-12 08:51:42 -0400 | [diff] [blame] | 44 | 'skia-corp.google.com', 'chops-service-accounts']] |
Eric Boren | dd98829 | 2018-01-02 13:29:21 -0500 | [diff] [blame] | 45 | |
rmistry@google.com | 547012d | 2013-04-12 19:45:46 +0000 | [diff] [blame] | 46 | |
rmistry@google.com | 713276b | 2013-01-25 18:27:34 +0000 | [diff] [blame] | 47 | def _CheckChangeHasEol(input_api, output_api, source_file_filter=None): |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 48 | """Checks that files end with at least one \n (LF).""" |
rmistry@google.com | 713276b | 2013-01-25 18:27:34 +0000 | [diff] [blame] | 49 | eof_files = [] |
| 50 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 51 | contents = input_api.ReadFile(f, 'rb') |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 52 | # Check that the file ends in at least one newline character. |
rmistry@google.com | 713276b | 2013-01-25 18:27:34 +0000 | [diff] [blame] | 53 | if len(contents) > 1 and contents[-1:] != '\n': |
| 54 | eof_files.append(f.LocalPath()) |
| 55 | |
| 56 | if eof_files: |
| 57 | return [output_api.PresubmitPromptWarning( |
| 58 | 'These files should end in a newline character:', |
| 59 | items=eof_files)] |
| 60 | return [] |
| 61 | |
| 62 | |
Ben Wagner | cf42e98 | 2018-02-09 17:41:20 -0500 | [diff] [blame] | 63 | def _JsonChecks(input_api, output_api): |
| 64 | """Run checks on any modified json files.""" |
| 65 | failing_files = [] |
| 66 | for affected_file in input_api.AffectedFiles(None): |
| 67 | affected_file_path = affected_file.LocalPath() |
| 68 | is_json = affected_file_path.endswith('.json') |
| 69 | is_metadata = (affected_file_path.startswith('site/') and |
| 70 | affected_file_path.endswith('/METADATA')) |
| 71 | if is_json or is_metadata: |
| 72 | try: |
| 73 | input_api.json.load(open(affected_file_path, 'r')) |
| 74 | except ValueError: |
| 75 | failing_files.append(affected_file_path) |
| 76 | |
| 77 | results = [] |
| 78 | if failing_files: |
| 79 | results.append( |
| 80 | output_api.PresubmitError( |
| 81 | 'The following files contain invalid json:\n%s\n\n' % |
| 82 | '\n'.join(failing_files))) |
| 83 | return results |
| 84 | |
| 85 | |
rmistry | 01cbf6c | 2015-03-12 07:48:40 -0700 | [diff] [blame] | 86 | def _IfDefChecks(input_api, output_api): |
| 87 | """Ensures if/ifdef are not before includes. See skbug/3362 for details.""" |
| 88 | comment_block_start_pattern = re.compile('^\s*\/\*.*$') |
| 89 | comment_block_middle_pattern = re.compile('^\s+\*.*') |
| 90 | comment_block_end_pattern = re.compile('^\s+\*\/.*$') |
| 91 | single_line_comment_pattern = re.compile('^\s*//.*$') |
| 92 | def is_comment(line): |
| 93 | return (comment_block_start_pattern.match(line) or |
| 94 | comment_block_middle_pattern.match(line) or |
| 95 | comment_block_end_pattern.match(line) or |
| 96 | single_line_comment_pattern.match(line)) |
| 97 | |
| 98 | empty_line_pattern = re.compile('^\s*$') |
| 99 | def is_empty_line(line): |
| 100 | return empty_line_pattern.match(line) |
| 101 | |
| 102 | failing_files = [] |
| 103 | for affected_file in input_api.AffectedSourceFiles(None): |
| 104 | affected_file_path = affected_file.LocalPath() |
| 105 | if affected_file_path.endswith('.cpp') or affected_file_path.endswith('.h'): |
| 106 | f = open(affected_file_path) |
| 107 | for line in f.xreadlines(): |
| 108 | if is_comment(line) or is_empty_line(line): |
| 109 | continue |
| 110 | # The below will be the first real line after comments and newlines. |
| 111 | if line.startswith('#if 0 '): |
| 112 | pass |
| 113 | elif line.startswith('#if ') or line.startswith('#ifdef '): |
| 114 | failing_files.append(affected_file_path) |
| 115 | break |
| 116 | |
| 117 | results = [] |
| 118 | if failing_files: |
| 119 | results.append( |
| 120 | output_api.PresubmitError( |
| 121 | 'The following files have #if or #ifdef before includes:\n%s\n\n' |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 122 | 'See https://bug.skia.org/3362 for why this should be fixed.' % |
rmistry | 01cbf6c | 2015-03-12 07:48:40 -0700 | [diff] [blame] | 123 | '\n'.join(failing_files))) |
| 124 | return results |
| 125 | |
| 126 | |
borenet | c7c9180 | 2015-03-25 04:47:02 -0700 | [diff] [blame] | 127 | def _CopyrightChecks(input_api, output_api, source_file_filter=None): |
| 128 | results = [] |
| 129 | year_pattern = r'\d{4}' |
| 130 | year_range_pattern = r'%s(-%s)?' % (year_pattern, year_pattern) |
| 131 | years_pattern = r'%s(,%s)*,?' % (year_range_pattern, year_range_pattern) |
| 132 | copyright_pattern = ( |
| 133 | r'Copyright (\([cC]\) )?%s \w+' % years_pattern) |
| 134 | |
| 135 | for affected_file in input_api.AffectedSourceFiles(source_file_filter): |
John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 136 | if ('third_party/' in affected_file.LocalPath() or |
| 137 | 'tests/sksl/' in affected_file.LocalPath()): |
borenet | c7c9180 | 2015-03-25 04:47:02 -0700 | [diff] [blame] | 138 | continue |
| 139 | contents = input_api.ReadFile(affected_file, 'rb') |
| 140 | if not re.search(copyright_pattern, contents): |
| 141 | results.append(output_api.PresubmitError( |
| 142 | '%s is missing a correct copyright header.' % affected_file)) |
| 143 | return results |
| 144 | |
| 145 | |
borenet | 2dbbfa5 | 2016-10-14 06:32:09 -0700 | [diff] [blame] | 146 | def _InfraTests(input_api, output_api): |
| 147 | """Run the infra tests.""" |
borenet | 1ed2ae4 | 2016-07-26 11:52:17 -0700 | [diff] [blame] | 148 | results = [] |
mtklein | 3da80f5 | 2016-07-27 04:14:07 -0700 | [diff] [blame] | 149 | if not any(f.LocalPath().startswith('infra') |
| 150 | for f in input_api.AffectedFiles()): |
| 151 | return results |
| 152 | |
borenet | 2dbbfa5 | 2016-10-14 06:32:09 -0700 | [diff] [blame] | 153 | cmd = ['python', os.path.join('infra', 'bots', 'infra_tests.py')] |
borenet | 60b0a2d | 2016-10-04 12:45:41 -0700 | [diff] [blame] | 154 | try: |
| 155 | subprocess.check_output(cmd) |
| 156 | except subprocess.CalledProcessError as e: |
| 157 | results.append(output_api.PresubmitError( |
| 158 | '`%s` failed:\n%s' % (' '.join(cmd), e.output))) |
| 159 | return results |
| 160 | |
| 161 | |
mtklein | 4db3b79 | 2016-08-03 14:18:22 -0700 | [diff] [blame] | 162 | def _CheckGNFormatted(input_api, output_api): |
| 163 | """Make sure any .gn files we're changing have been formatted.""" |
Ben Wagner | 3c4a9d3 | 2020-02-14 14:28:33 -0500 | [diff] [blame] | 164 | files = [] |
Corentin Wallez | 6a5187a | 2020-04-08 10:24:04 +0200 | [diff] [blame] | 165 | for f in input_api.AffectedFiles(include_deletes=False): |
Ben Wagner | 3c4a9d3 | 2020-02-14 14:28:33 -0500 | [diff] [blame] | 166 | if (f.LocalPath().endswith('.gn') or |
| 167 | f.LocalPath().endswith('.gni')): |
| 168 | files.append(f) |
| 169 | if not files: |
| 170 | return [] |
mtklein | 4db3b79 | 2016-08-03 14:18:22 -0700 | [diff] [blame] | 171 | |
Ben Wagner | 3c4a9d3 | 2020-02-14 14:28:33 -0500 | [diff] [blame] | 172 | cmd = ['python', os.path.join('bin', 'fetch-gn')] |
| 173 | try: |
| 174 | subprocess.check_output(cmd) |
| 175 | except subprocess.CalledProcessError as e: |
| 176 | return [output_api.PresubmitError( |
| 177 | '`%s` failed:\n%s' % (' '.join(cmd), e.output))] |
| 178 | |
| 179 | results = [] |
| 180 | for f in files: |
Brian Osman | 70f24af | 2020-02-18 15:08:27 -0500 | [diff] [blame] | 181 | gn = 'gn.exe' if 'win32' in sys.platform else 'gn' |
Ben Wagner | 06265e0 | 2020-02-13 19:02:46 -0500 | [diff] [blame] | 182 | gn = os.path.join(input_api.PresubmitLocalPath(), 'bin', gn) |
Mike Klein | 7a1c53d | 2016-10-11 14:03:06 -0400 | [diff] [blame] | 183 | cmd = [gn, 'format', '--dry-run', f.LocalPath()] |
mtklein | 4db3b79 | 2016-08-03 14:18:22 -0700 | [diff] [blame] | 184 | try: |
| 185 | subprocess.check_output(cmd) |
| 186 | except subprocess.CalledProcessError: |
Ben Wagner | 06265e0 | 2020-02-13 19:02:46 -0500 | [diff] [blame] | 187 | fix = 'bin/gn format ' + f.LocalPath() |
mtklein | 4db3b79 | 2016-08-03 14:18:22 -0700 | [diff] [blame] | 188 | results.append(output_api.PresubmitError( |
mtklein | d434b01 | 2016-08-10 07:30:58 -0700 | [diff] [blame] | 189 | '`%s` failed, try\n\t%s' % (' '.join(cmd), fix))) |
mtklein | 4db3b79 | 2016-08-03 14:18:22 -0700 | [diff] [blame] | 190 | return results |
| 191 | |
Ravi Mistry | 6eca579 | 2020-12-16 11:42:29 -0500 | [diff] [blame] | 192 | |
| 193 | def _CheckGitConflictMarkers(input_api, output_api): |
| 194 | pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$') |
| 195 | results = [] |
| 196 | for f in input_api.AffectedFiles(): |
| 197 | for line_num, line in f.ChangedContents(): |
| 198 | if f.LocalPath().endswith('.md'): |
| 199 | # First-level headers in markdown look a lot like version control |
| 200 | # conflict markers. http://daringfireball.net/projects/markdown/basics |
| 201 | continue |
| 202 | if pattern.match(line): |
| 203 | results.append( |
| 204 | output_api.PresubmitError( |
| 205 | 'Git conflict markers found in %s:%d %s' % ( |
| 206 | f.LocalPath(), line_num, line))) |
| 207 | return results |
| 208 | |
| 209 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 210 | def _CheckIncludesFormatted(input_api, output_api): |
| 211 | """Make sure #includes in files we're changing have been formatted.""" |
Mike Klein | f9ad5ba | 2019-07-29 12:34:39 -0500 | [diff] [blame] | 212 | files = [str(f) for f in input_api.AffectedFiles() if f.Action() != 'D'] |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 213 | cmd = ['python', |
| 214 | 'tools/rewrite_includes.py', |
Mike Klein | f9ad5ba | 2019-07-29 12:34:39 -0500 | [diff] [blame] | 215 | '--dry-run'] + files |
Hal Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 216 | if 0 != subprocess.call(cmd): |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 217 | return [output_api.PresubmitError('`%s` failed' % ' '.join(cmd))] |
| 218 | return [] |
borenet | 1ed2ae4 | 2016-07-26 11:52:17 -0700 | [diff] [blame] | 219 | |
Eric Boren | 58d1f76 | 2019-07-19 08:07:44 -0400 | [diff] [blame] | 220 | |
Ben Wagner | 8885550 | 2017-10-12 17:55:19 -0400 | [diff] [blame] | 221 | class _WarningsAsErrors(): |
| 222 | def __init__(self, output_api): |
| 223 | self.output_api = output_api |
| 224 | self.old_warning = None |
| 225 | def __enter__(self): |
| 226 | self.old_warning = self.output_api.PresubmitPromptWarning |
| 227 | self.output_api.PresubmitPromptWarning = self.output_api.PresubmitError |
| 228 | return self.output_api |
| 229 | def __exit__(self, ex_type, ex_value, ex_traceback): |
| 230 | self.output_api.PresubmitPromptWarning = self.old_warning |
| 231 | |
| 232 | |
Eric Boren | 6dc0021 | 2019-07-24 15:15:43 -0400 | [diff] [blame] | 233 | def _CheckDEPSValid(input_api, output_api): |
| 234 | """Ensure that DEPS contains valid entries.""" |
| 235 | results = [] |
| 236 | script = os.path.join('infra', 'bots', 'check_deps.py') |
| 237 | relevant_files = ('DEPS', script) |
| 238 | for f in input_api.AffectedFiles(): |
| 239 | if f.LocalPath() in relevant_files: |
| 240 | break |
| 241 | else: |
| 242 | return results |
| 243 | cmd = ['python', script] |
| 244 | try: |
| 245 | subprocess.check_output(cmd, stderr=subprocess.STDOUT) |
| 246 | except subprocess.CalledProcessError as e: |
| 247 | results.append(output_api.PresubmitError(e.output)) |
| 248 | return results |
| 249 | |
| 250 | |
Kevin Lubick | 2cd8067 | 2021-07-01 11:03:36 -0400 | [diff] [blame] | 251 | def _RegenerateAllExamplesCPP(input_api, output_api): |
| 252 | """Regenerates all_examples.cpp if an example was added or deleted.""" |
| 253 | if not any(f.LocalPath().startswith('docs/examples/') |
| 254 | for f in input_api.AffectedFiles()): |
| 255 | return [] |
| 256 | command_str = 'tools/fiddle/make_all_examples_cpp.py' |
| 257 | cmd = ['python', command_str] |
| 258 | if 0 != subprocess.call(cmd): |
| 259 | return [output_api.PresubmitError('`%s` failed' % ' '.join(cmd))] |
| 260 | |
| 261 | results = [] |
| 262 | git_diff_output = input_api.subprocess.check_output( |
| 263 | ['git', 'diff', '--no-ext-diff']) |
| 264 | if git_diff_output: |
| 265 | results += [output_api.PresubmitError( |
| 266 | 'Diffs found after running "%s":\n\n%s\n' |
| 267 | 'Please commit or discard the above changes.' % ( |
| 268 | command_str, |
| 269 | git_diff_output, |
| 270 | ) |
| 271 | )] |
| 272 | return results |
| 273 | |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 274 | def _CommonChecks(input_api, output_api): |
| 275 | """Presubmit checks common to upload and commit.""" |
| 276 | results = [] |
| 277 | sources = lambda x: (x.LocalPath().endswith('.h') or |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 278 | x.LocalPath().endswith('.py') or |
| 279 | x.LocalPath().endswith('.sh') or |
mtklein | 18e5580 | 2015-03-25 07:21:20 -0700 | [diff] [blame] | 280 | x.LocalPath().endswith('.m') or |
| 281 | x.LocalPath().endswith('.mm') or |
| 282 | x.LocalPath().endswith('.go') or |
| 283 | x.LocalPath().endswith('.c') or |
| 284 | x.LocalPath().endswith('.cc') or |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 285 | x.LocalPath().endswith('.cpp')) |
Ben Wagner | 8885550 | 2017-10-12 17:55:19 -0400 | [diff] [blame] | 286 | results.extend(_CheckChangeHasEol( |
| 287 | input_api, output_api, source_file_filter=sources)) |
| 288 | with _WarningsAsErrors(output_api): |
| 289 | results.extend(input_api.canned_checks.CheckChangeHasNoCR( |
| 290 | input_api, output_api, source_file_filter=sources)) |
| 291 | results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace( |
| 292 | input_api, output_api, source_file_filter=sources)) |
Ben Wagner | cf42e98 | 2018-02-09 17:41:20 -0500 | [diff] [blame] | 293 | results.extend(_JsonChecks(input_api, output_api)) |
rmistry | 01cbf6c | 2015-03-12 07:48:40 -0700 | [diff] [blame] | 294 | results.extend(_IfDefChecks(input_api, output_api)) |
borenet | c7c9180 | 2015-03-25 04:47:02 -0700 | [diff] [blame] | 295 | results.extend(_CopyrightChecks(input_api, output_api, |
| 296 | source_file_filter=sources)) |
Eric Boren | 6dc0021 | 2019-07-24 15:15:43 -0400 | [diff] [blame] | 297 | results.extend(_CheckDEPSValid(input_api, output_api)) |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 298 | results.extend(_CheckIncludesFormatted(input_api, output_api)) |
Mike Klein | 96f6401 | 2020-04-03 10:59:37 -0500 | [diff] [blame] | 299 | results.extend(_CheckGNFormatted(input_api, output_api)) |
Ravi Mistry | 6eca579 | 2020-12-16 11:42:29 -0500 | [diff] [blame] | 300 | results.extend(_CheckGitConflictMarkers(input_api, output_api)) |
Kevin Lubick | 2cd8067 | 2021-07-01 11:03:36 -0400 | [diff] [blame] | 301 | results.extend(_RegenerateAllExamplesCPP(input_api, output_api)) |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 302 | return results |
| 303 | |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 304 | |
| 305 | def CheckChangeOnUpload(input_api, output_api): |
Ravi Mistry | 4c0ffe7 | 2020-03-02 13:19:02 -0500 | [diff] [blame] | 306 | """Presubmit checks for the change on upload.""" |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 307 | results = [] |
| 308 | results.extend(_CommonChecks(input_api, output_api)) |
borenet | 1ed2ae4 | 2016-07-26 11:52:17 -0700 | [diff] [blame] | 309 | # Run on upload, not commit, since the presubmit bot apparently doesn't have |
borenet | 60b0a2d | 2016-10-04 12:45:41 -0700 | [diff] [blame] | 310 | # coverage or Go installed. |
borenet | 2dbbfa5 | 2016-10-14 06:32:09 -0700 | [diff] [blame] | 311 | results.extend(_InfraTests(input_api, output_api)) |
Ravi Mistry | 5773516 | 2019-07-25 13:45:15 -0400 | [diff] [blame] | 312 | results.extend(_CheckReleaseNotesForPublicAPI(input_api, output_api)) |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 313 | return results |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 314 | |
| 315 | |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 316 | class CodeReview(object): |
| 317 | """Abstracts which codereview tool is used for the specified issue.""" |
| 318 | |
| 319 | def __init__(self, input_api): |
| 320 | self._issue = input_api.change.issue |
| 321 | self._gerrit = input_api.gerrit |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 322 | |
| 323 | def GetOwnerEmail(self): |
Aaron Gable | a49909a | 2017-10-09 12:50:52 -0700 | [diff] [blame] | 324 | return self._gerrit.GetChangeOwner(self._issue) |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 325 | |
| 326 | def GetSubject(self): |
Aaron Gable | a49909a | 2017-10-09 12:50:52 -0700 | [diff] [blame] | 327 | return self._gerrit.GetChangeInfo(self._issue)['subject'] |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 328 | |
| 329 | def GetDescription(self): |
Aaron Gable | a49909a | 2017-10-09 12:50:52 -0700 | [diff] [blame] | 330 | return self._gerrit.GetChangeDescription(self._issue) |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 331 | |
Ravi Mistry | 39eabb6 | 2016-10-05 08:41:12 -0400 | [diff] [blame] | 332 | def GetReviewers(self): |
Aaron Gable | a49909a | 2017-10-09 12:50:52 -0700 | [diff] [blame] | 333 | code_review_label = ( |
| 334 | self._gerrit.GetChangeInfo(self._issue)['labels']['Code-Review']) |
| 335 | return [r['email'] for r in code_review_label.get('all', [])] |
Ravi Mistry | 39eabb6 | 2016-10-05 08:41:12 -0400 | [diff] [blame] | 336 | |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 337 | def GetApprovers(self): |
| 338 | approvers = [] |
Aaron Gable | a49909a | 2017-10-09 12:50:52 -0700 | [diff] [blame] | 339 | code_review_label = ( |
| 340 | self._gerrit.GetChangeInfo(self._issue)['labels']['Code-Review']) |
| 341 | for m in code_review_label.get('all', []): |
| 342 | if m.get("value") == 1: |
| 343 | approvers.append(m["email"]) |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 344 | return approvers |
| 345 | |
| 346 | |
commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 347 | def _CheckOwnerIsInAuthorsFile(input_api, output_api): |
| 348 | results = [] |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 349 | if input_api.change.issue: |
| 350 | cr = CodeReview(input_api) |
commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 351 | |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 352 | owner_email = cr.GetOwnerEmail() |
Eric Boren | dd98829 | 2018-01-02 13:29:21 -0500 | [diff] [blame] | 353 | |
| 354 | # Service accounts don't need to be in AUTHORS. |
Eric Boren | 1eec99c | 2018-04-26 13:09:48 -0400 | [diff] [blame] | 355 | for suffix in SERVICE_ACCOUNT_SUFFIX: |
| 356 | if owner_email.endswith(suffix): |
| 357 | return results |
Eric Boren | dd98829 | 2018-01-02 13:29:21 -0500 | [diff] [blame] | 358 | |
commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 359 | try: |
| 360 | authors_content = '' |
| 361 | for line in open(AUTHORS_FILE_NAME): |
| 362 | if not line.startswith('#'): |
| 363 | authors_content += line |
| 364 | email_fnmatches = re.findall('<(.*)>', authors_content) |
| 365 | for email_fnmatch in email_fnmatches: |
| 366 | if fnmatch.fnmatch(owner_email, email_fnmatch): |
| 367 | # Found a match, the user is in the AUTHORS file break out of the loop |
| 368 | break |
| 369 | else: |
commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 370 | results.append( |
| 371 | output_api.PresubmitError( |
| 372 | 'The email %s is not in Skia\'s AUTHORS file.\n' |
| 373 | 'Issue owner, this CL must include an addition to the Skia AUTHORS ' |
rmistry | 9806d4d | 2015-10-01 08:10:54 -0700 | [diff] [blame] | 374 | 'file.' |
rmistry | 83fab47 | 2014-07-18 05:25:56 -0700 | [diff] [blame] | 375 | % owner_email)) |
commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 376 | except IOError: |
| 377 | # Do not fail if authors file cannot be found. |
| 378 | traceback.print_exc() |
| 379 | input_api.logging.error('AUTHORS file not found!') |
| 380 | |
| 381 | return results |
| 382 | |
| 383 | |
Ravi Mistry | 5773516 | 2019-07-25 13:45:15 -0400 | [diff] [blame] | 384 | def _CheckReleaseNotesForPublicAPI(input_api, output_api): |
| 385 | """Checks to see if release notes file is updated with public API changes.""" |
| 386 | results = [] |
| 387 | public_api_changed = False |
| 388 | release_file_changed = False |
| 389 | for affected_file in input_api.AffectedFiles(): |
| 390 | affected_file_path = affected_file.LocalPath() |
| 391 | file_path, file_ext = os.path.splitext(affected_file_path) |
| 392 | # We only care about files that end in .h and are under the top-level |
| 393 | # include dir, but not include/private. |
| 394 | if (file_ext == '.h' and |
| 395 | file_path.split(os.path.sep)[0] == 'include' and |
| 396 | 'private' not in file_path): |
| 397 | public_api_changed = True |
| 398 | elif affected_file_path == RELEASE_NOTES_FILE_NAME: |
| 399 | release_file_changed = True |
| 400 | |
| 401 | if public_api_changed and not release_file_changed: |
| 402 | results.append(output_api.PresubmitPromptWarning( |
| 403 | 'If this change affects a client API, please add a summary line ' |
| 404 | 'to the %s file.' % RELEASE_NOTES_FILE_NAME)) |
| 405 | return results |
| 406 | |
| 407 | |
| 408 | |
rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 409 | def _CheckLGTMsForPublicAPI(input_api, output_api): |
| 410 | """Check LGTMs for public API changes. |
| 411 | |
| 412 | For public API files make sure there is an LGTM from the list of owners in |
| 413 | PUBLIC_API_OWNERS. |
| 414 | """ |
| 415 | results = [] |
| 416 | requires_owner_check = False |
rmistry | 9407ece | 2014-08-26 14:00:54 -0700 | [diff] [blame] | 417 | for affected_file in input_api.AffectedFiles(): |
| 418 | affected_file_path = affected_file.LocalPath() |
rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 419 | file_path, file_ext = os.path.splitext(affected_file_path) |
rmistry | 9407ece | 2014-08-26 14:00:54 -0700 | [diff] [blame] | 420 | # We only care about files that end in .h and are under the top-level |
mtklein | bda1267 | 2015-07-28 08:54:12 -0700 | [diff] [blame] | 421 | # include dir, but not include/private. |
| 422 | if (file_ext == '.h' and |
| 423 | 'include' == file_path.split(os.path.sep)[0] and |
| 424 | 'private' not in file_path): |
rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 425 | requires_owner_check = True |
| 426 | |
| 427 | if not requires_owner_check: |
| 428 | return results |
| 429 | |
| 430 | lgtm_from_owner = False |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 431 | if input_api.change.issue: |
| 432 | cr = CodeReview(input_api) |
| 433 | |
| 434 | if re.match(REVERT_CL_SUBJECT_PREFIX, cr.GetSubject(), re.I): |
commit-bot@chromium.org | cfdc596 | 2014-01-31 17:33:04 +0000 | [diff] [blame] | 435 | # It is a revert CL, ignore the public api owners check. |
| 436 | return results |
rmistry | f2d83ca | 2014-08-26 10:30:29 -0700 | [diff] [blame] | 437 | |
Ravi Mistry | 39eabb6 | 2016-10-05 08:41:12 -0400 | [diff] [blame] | 438 | if input_api.gerrit: |
| 439 | for reviewer in cr.GetReviewers(): |
| 440 | if reviewer in PUBLIC_API_OWNERS: |
| 441 | # If an owner is specified as an reviewer in Gerrit then ignore the |
| 442 | # public api owners check. |
rmistry | f2d83ca | 2014-08-26 10:30:29 -0700 | [diff] [blame] | 443 | return results |
Ravi Mistry | 39eabb6 | 2016-10-05 08:41:12 -0400 | [diff] [blame] | 444 | else: |
| 445 | match = re.search(r'^TBR=(.*)$', cr.GetDescription(), re.M) |
| 446 | if match: |
| 447 | tbr_section = match.group(1).strip().split(' ')[0] |
| 448 | tbr_entries = tbr_section.split(',') |
| 449 | for owner in PUBLIC_API_OWNERS: |
| 450 | if owner in tbr_entries or owner.split('@')[0] in tbr_entries: |
| 451 | # If an owner is specified in the TBR= line then ignore the public |
| 452 | # api owners check. |
| 453 | return results |
rmistry | f2d83ca | 2014-08-26 10:30:29 -0700 | [diff] [blame] | 454 | |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 455 | if cr.GetOwnerEmail() in PUBLIC_API_OWNERS: |
rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 456 | # An owner created the CL that is an automatic LGTM. |
| 457 | lgtm_from_owner = True |
| 458 | |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 459 | for approver in cr.GetApprovers(): |
| 460 | if approver in PUBLIC_API_OWNERS: |
| 461 | # Found an lgtm in a message from an owner. |
| 462 | lgtm_from_owner = True |
| 463 | break |
commit-bot@chromium.org | cfdc596 | 2014-01-31 17:33:04 +0000 | [diff] [blame] | 464 | |
rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 465 | if not lgtm_from_owner: |
| 466 | results.append( |
| 467 | output_api.PresubmitError( |
mtklein | bda1267 | 2015-07-28 08:54:12 -0700 | [diff] [blame] | 468 | "If this CL adds to or changes Skia's public API, you need an LGTM " |
| 469 | "from any of %s. If this CL only removes from or doesn't change " |
Ravi Mistry | dbb84c2 | 2016-10-05 12:47:44 -0400 | [diff] [blame] | 470 | "Skia's public API, please add a short note to the CL saying so. " |
Aaron Gable | a49909a | 2017-10-09 12:50:52 -0700 | [diff] [blame] | 471 | "Add one of the owners as a reviewer to your CL as well as to the " |
| 472 | "TBR= line. If you don't know if this CL affects Skia's public " |
| 473 | "API, treat it like it does." % str(PUBLIC_API_OWNERS))) |
rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 474 | return results |
| 475 | |
| 476 | |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 477 | def PostUploadHook(gerrit, change, output_api): |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 478 | """git cl upload will call this hook after the issue is created/modified. |
| 479 | |
| 480 | This hook does the following: |
| 481 | * 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] | 482 | * Adds 'No-Try: true' if the CL contains only docs changes. |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 483 | """ |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 484 | if not change.issue: |
| 485 | return [] |
| 486 | |
| 487 | # Skip PostUploadHooks for all auto-commit service account bots. New |
| 488 | # patchsets (caused due to PostUploadHooks) invalidates the CQ+2 vote from |
| 489 | # the "--use-commit-queue" flag to "git cl upload". |
| 490 | for suffix in SERVICE_ACCOUNT_SUFFIX: |
| 491 | if change.author_email.endswith(suffix): |
| 492 | return [] |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 493 | |
| 494 | results = [] |
Ravi Mistry | 27095f2 | 2021-04-22 12:51:49 +0000 | [diff] [blame] | 495 | at_least_one_docs_change = False |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 496 | all_docs_changes = True |
| 497 | for affected_file in change.AffectedFiles(): |
| 498 | affected_file_path = affected_file.LocalPath() |
| 499 | file_path, _ = os.path.splitext(affected_file_path) |
Ravi Mistry | 27095f2 | 2021-04-22 12:51:49 +0000 | [diff] [blame] | 500 | if 'site' == file_path.split(os.path.sep)[0]: |
| 501 | at_least_one_docs_change = True |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 502 | else: |
| 503 | all_docs_changes = False |
Ravi Mistry | 27095f2 | 2021-04-22 12:51:49 +0000 | [diff] [blame] | 504 | if at_least_one_docs_change and not all_docs_changes: |
| 505 | break |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 506 | |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 507 | footers = change.GitFootersFromDescription() |
| 508 | description_changed = False |
Ravi Mistry | b5e2acc | 2017-12-07 11:10:11 -0500 | [diff] [blame] | 509 | |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 510 | # If the change includes only doc changes then add No-Try: true in the |
| 511 | # CL's description if it does not exist yet. |
| 512 | if all_docs_changes and 'true' not in footers.get('No-Try', []): |
| 513 | description_changed = True |
Edward Lemur | c631b7c | 2020-02-04 15:30:18 -0500 | [diff] [blame] | 514 | change.AddDescriptionFooter('No-Try', 'true') |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 515 | results.append( |
| 516 | output_api.PresubmitNotifyResult( |
| 517 | 'This change has only doc changes. Automatically added ' |
| 518 | '\'No-Try: true\' to the CL\'s description')) |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 519 | |
Ravi Mistry | 27095f2 | 2021-04-22 12:51:49 +0000 | [diff] [blame] | 520 | # If there is at least one docs change then add preview link in the CL's |
| 521 | # description if it does not already exist there. |
| 522 | docs_preview_link = DOCS_PREVIEW_URL.format(issue=change.issue) |
| 523 | if (at_least_one_docs_change |
| 524 | and docs_preview_link not in footers.get('Docs-Preview', [])): |
| 525 | # Automatically add a link to where the docs can be previewed. |
| 526 | description_changed = True |
| 527 | change.AddDescriptionFooter('Docs-Preview', docs_preview_link) |
| 528 | results.append( |
| 529 | output_api.PresubmitNotifyResult( |
| 530 | 'Automatically added a link to preview the docs changes to the ' |
| 531 | 'CL\'s description')) |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 532 | |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 533 | # If the description has changed update it. |
| 534 | if description_changed: |
| 535 | gerrit.UpdateDescription( |
| 536 | change.FullDescriptionText(), change.issue) |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 537 | |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 538 | return results |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 539 | |
| 540 | |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 541 | def CheckChangeOnCommit(input_api, output_api): |
Ravi Mistry | 4c0ffe7 | 2020-03-02 13:19:02 -0500 | [diff] [blame] | 542 | """Presubmit checks for the change on commit.""" |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 543 | results = [] |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 544 | results.extend(_CommonChecks(input_api, output_api)) |
rmistry@google.com | fb4a68d | 2013-08-12 14:51:20 +0000 | [diff] [blame] | 545 | results.extend(_CheckLGTMsForPublicAPI(input_api, output_api)) |
commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 546 | results.extend(_CheckOwnerIsInAuthorsFile(input_api, output_api)) |
Ravi Mistry | a70cb8a | 2017-09-12 13:52:05 -0400 | [diff] [blame] | 547 | # Checks for the presence of 'DO NOT''SUBMIT' in CL description and in |
| 548 | # content of files. |
| 549 | results.extend( |
| 550 | input_api.canned_checks.CheckDoNotSubmit(input_api, output_api)) |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 551 | return results |