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