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