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