Kevin Lubick | 7f7b6ab | 2021-08-16 15:00:15 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 2 | # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | |
| 7 | """Top-level presubmit script for Skia. |
| 8 | |
| 9 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 10 | for more details about the presubmit API built into gcl. |
| 11 | """ |
| 12 | |
commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 13 | import fnmatch |
rmistry@google.com | f6c5f75 | 2013-03-29 17:26:00 +0000 | [diff] [blame] | 14 | import os |
commit-bot@chromium.org | cfdc596 | 2014-01-31 17:33:04 +0000 | [diff] [blame] | 15 | import re |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 16 | import subprocess |
rmistry@google.com | f6c5f75 | 2013-03-29 17:26:00 +0000 | [diff] [blame] | 17 | import sys |
commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 18 | import traceback |
rmistry@google.com | f6c5f75 | 2013-03-29 17:26:00 +0000 | [diff] [blame] | 19 | |
rmistry@google.com | c299344 | 2013-01-23 14:35:58 +0000 | [diff] [blame] | 20 | |
Ravi Mistry | 5773516 | 2019-07-25 13:45:15 -0400 | [diff] [blame] | 21 | RELEASE_NOTES_FILE_NAME = 'RELEASE_NOTES.txt' |
commit-bot@chromium.org | 745e08c | 2014-02-03 14:18:32 +0000 | [diff] [blame] | 22 | |
rmistry | d88b0be | 2016-05-20 03:50:01 -0700 | [diff] [blame] | 23 | GOLD_TRYBOT_URL = 'https://gold.skia.org/search?issue=' |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 24 | |
Eric Boren | 1eec99c | 2018-04-26 13:09:48 -0400 | [diff] [blame] | 25 | SERVICE_ACCOUNT_SUFFIX = [ |
Eric Boren | 47ed6f1 | 2018-04-26 14:02:43 -0400 | [diff] [blame] | 26 | '@%s.iam.gserviceaccount.com' % project for project in [ |
Eric Boren | 6ad3ca4 | 2018-09-07 14:22:16 -0400 | [diff] [blame] | 27 | 'skia-buildbots.google.com', 'skia-swarming-bots', 'skia-public', |
Ravi Mistry | 53c4423 | 2019-03-12 08:51:42 -0400 | [diff] [blame] | 28 | 'skia-corp.google.com', 'chops-service-accounts']] |
Eric Boren | dd98829 | 2018-01-02 13:29:21 -0500 | [diff] [blame] | 29 | |
rmistry@google.com | 547012d | 2013-04-12 19:45:46 +0000 | [diff] [blame] | 30 | |
rmistry@google.com | 713276b | 2013-01-25 18:27:34 +0000 | [diff] [blame] | 31 | def _CheckChangeHasEol(input_api, output_api, source_file_filter=None): |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 32 | """Checks that files end with at least one \n (LF).""" |
rmistry@google.com | 713276b | 2013-01-25 18:27:34 +0000 | [diff] [blame] | 33 | eof_files = [] |
| 34 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 35 | contents = input_api.ReadFile(f, 'rb') |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 36 | # Check that the file ends in at least one newline character. |
rmistry@google.com | 713276b | 2013-01-25 18:27:34 +0000 | [diff] [blame] | 37 | if len(contents) > 1 and contents[-1:] != '\n': |
| 38 | eof_files.append(f.LocalPath()) |
| 39 | |
| 40 | if eof_files: |
| 41 | return [output_api.PresubmitPromptWarning( |
| 42 | 'These files should end in a newline character:', |
| 43 | items=eof_files)] |
| 44 | return [] |
| 45 | |
| 46 | |
Ben Wagner | cf42e98 | 2018-02-09 17:41:20 -0500 | [diff] [blame] | 47 | def _JsonChecks(input_api, output_api): |
| 48 | """Run checks on any modified json files.""" |
| 49 | failing_files = [] |
| 50 | for affected_file in input_api.AffectedFiles(None): |
| 51 | affected_file_path = affected_file.LocalPath() |
| 52 | is_json = affected_file_path.endswith('.json') |
| 53 | is_metadata = (affected_file_path.startswith('site/') and |
| 54 | affected_file_path.endswith('/METADATA')) |
| 55 | if is_json or is_metadata: |
| 56 | try: |
| 57 | input_api.json.load(open(affected_file_path, 'r')) |
| 58 | except ValueError: |
| 59 | failing_files.append(affected_file_path) |
| 60 | |
| 61 | results = [] |
| 62 | if failing_files: |
| 63 | results.append( |
| 64 | output_api.PresubmitError( |
| 65 | 'The following files contain invalid json:\n%s\n\n' % |
| 66 | '\n'.join(failing_files))) |
| 67 | return results |
| 68 | |
| 69 | |
rmistry | 01cbf6c | 2015-03-12 07:48:40 -0700 | [diff] [blame] | 70 | def _IfDefChecks(input_api, output_api): |
| 71 | """Ensures if/ifdef are not before includes. See skbug/3362 for details.""" |
| 72 | comment_block_start_pattern = re.compile('^\s*\/\*.*$') |
| 73 | comment_block_middle_pattern = re.compile('^\s+\*.*') |
| 74 | comment_block_end_pattern = re.compile('^\s+\*\/.*$') |
| 75 | single_line_comment_pattern = re.compile('^\s*//.*$') |
| 76 | def is_comment(line): |
| 77 | return (comment_block_start_pattern.match(line) or |
| 78 | comment_block_middle_pattern.match(line) or |
| 79 | comment_block_end_pattern.match(line) or |
| 80 | single_line_comment_pattern.match(line)) |
| 81 | |
| 82 | empty_line_pattern = re.compile('^\s*$') |
| 83 | def is_empty_line(line): |
| 84 | return empty_line_pattern.match(line) |
| 85 | |
| 86 | failing_files = [] |
| 87 | for affected_file in input_api.AffectedSourceFiles(None): |
| 88 | affected_file_path = affected_file.LocalPath() |
| 89 | if affected_file_path.endswith('.cpp') or affected_file_path.endswith('.h'): |
| 90 | f = open(affected_file_path) |
| 91 | for line in f.xreadlines(): |
| 92 | if is_comment(line) or is_empty_line(line): |
| 93 | continue |
| 94 | # The below will be the first real line after comments and newlines. |
| 95 | if line.startswith('#if 0 '): |
| 96 | pass |
| 97 | elif line.startswith('#if ') or line.startswith('#ifdef '): |
| 98 | failing_files.append(affected_file_path) |
| 99 | break |
| 100 | |
| 101 | results = [] |
| 102 | if failing_files: |
| 103 | results.append( |
| 104 | output_api.PresubmitError( |
| 105 | 'The following files have #if or #ifdef before includes:\n%s\n\n' |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 106 | 'See https://bug.skia.org/3362 for why this should be fixed.' % |
rmistry | 01cbf6c | 2015-03-12 07:48:40 -0700 | [diff] [blame] | 107 | '\n'.join(failing_files))) |
| 108 | return results |
| 109 | |
| 110 | |
borenet | c7c9180 | 2015-03-25 04:47:02 -0700 | [diff] [blame] | 111 | def _CopyrightChecks(input_api, output_api, source_file_filter=None): |
| 112 | results = [] |
| 113 | year_pattern = r'\d{4}' |
| 114 | year_range_pattern = r'%s(-%s)?' % (year_pattern, year_pattern) |
| 115 | years_pattern = r'%s(,%s)*,?' % (year_range_pattern, year_range_pattern) |
| 116 | copyright_pattern = ( |
| 117 | r'Copyright (\([cC]\) )?%s \w+' % years_pattern) |
| 118 | |
| 119 | for affected_file in input_api.AffectedSourceFiles(source_file_filter): |
John Stiles | d836f84 | 2020-09-14 10:21:44 -0400 | [diff] [blame] | 120 | if ('third_party/' in affected_file.LocalPath() or |
| 121 | 'tests/sksl/' in affected_file.LocalPath()): |
borenet | c7c9180 | 2015-03-25 04:47:02 -0700 | [diff] [blame] | 122 | continue |
| 123 | contents = input_api.ReadFile(affected_file, 'rb') |
| 124 | if not re.search(copyright_pattern, contents): |
| 125 | results.append(output_api.PresubmitError( |
| 126 | '%s is missing a correct copyright header.' % affected_file)) |
| 127 | return results |
| 128 | |
| 129 | |
borenet | 2dbbfa5 | 2016-10-14 06:32:09 -0700 | [diff] [blame] | 130 | def _InfraTests(input_api, output_api): |
| 131 | """Run the infra tests.""" |
borenet | 1ed2ae4 | 2016-07-26 11:52:17 -0700 | [diff] [blame] | 132 | results = [] |
mtklein | 3da80f5 | 2016-07-27 04:14:07 -0700 | [diff] [blame] | 133 | if not any(f.LocalPath().startswith('infra') |
| 134 | for f in input_api.AffectedFiles()): |
| 135 | return results |
| 136 | |
borenet | 2dbbfa5 | 2016-10-14 06:32:09 -0700 | [diff] [blame] | 137 | cmd = ['python', os.path.join('infra', 'bots', 'infra_tests.py')] |
borenet | 60b0a2d | 2016-10-04 12:45:41 -0700 | [diff] [blame] | 138 | try: |
| 139 | subprocess.check_output(cmd) |
| 140 | except subprocess.CalledProcessError as e: |
| 141 | results.append(output_api.PresubmitError( |
| 142 | '`%s` failed:\n%s' % (' '.join(cmd), e.output))) |
| 143 | return results |
| 144 | |
| 145 | |
mtklein | 4db3b79 | 2016-08-03 14:18:22 -0700 | [diff] [blame] | 146 | def _CheckGNFormatted(input_api, output_api): |
| 147 | """Make sure any .gn files we're changing have been formatted.""" |
Ben Wagner | 3c4a9d3 | 2020-02-14 14:28:33 -0500 | [diff] [blame] | 148 | files = [] |
Corentin Wallez | 6a5187a | 2020-04-08 10:24:04 +0200 | [diff] [blame] | 149 | for f in input_api.AffectedFiles(include_deletes=False): |
Ben Wagner | 3c4a9d3 | 2020-02-14 14:28:33 -0500 | [diff] [blame] | 150 | if (f.LocalPath().endswith('.gn') or |
| 151 | f.LocalPath().endswith('.gni')): |
| 152 | files.append(f) |
| 153 | if not files: |
| 154 | return [] |
mtklein | 4db3b79 | 2016-08-03 14:18:22 -0700 | [diff] [blame] | 155 | |
Ben Wagner | 3c4a9d3 | 2020-02-14 14:28:33 -0500 | [diff] [blame] | 156 | cmd = ['python', os.path.join('bin', 'fetch-gn')] |
| 157 | try: |
| 158 | subprocess.check_output(cmd) |
| 159 | except subprocess.CalledProcessError as e: |
| 160 | return [output_api.PresubmitError( |
| 161 | '`%s` failed:\n%s' % (' '.join(cmd), e.output))] |
| 162 | |
| 163 | results = [] |
| 164 | for f in files: |
Brian Osman | 70f24af | 2020-02-18 15:08:27 -0500 | [diff] [blame] | 165 | gn = 'gn.exe' if 'win32' in sys.platform else 'gn' |
Ben Wagner | 06265e0 | 2020-02-13 19:02:46 -0500 | [diff] [blame] | 166 | gn = os.path.join(input_api.PresubmitLocalPath(), 'bin', gn) |
Mike Klein | 7a1c53d | 2016-10-11 14:03:06 -0400 | [diff] [blame] | 167 | cmd = [gn, 'format', '--dry-run', f.LocalPath()] |
mtklein | 4db3b79 | 2016-08-03 14:18:22 -0700 | [diff] [blame] | 168 | try: |
| 169 | subprocess.check_output(cmd) |
| 170 | except subprocess.CalledProcessError: |
Ben Wagner | 06265e0 | 2020-02-13 19:02:46 -0500 | [diff] [blame] | 171 | fix = 'bin/gn format ' + f.LocalPath() |
mtklein | 4db3b79 | 2016-08-03 14:18:22 -0700 | [diff] [blame] | 172 | results.append(output_api.PresubmitError( |
mtklein | d434b01 | 2016-08-10 07:30:58 -0700 | [diff] [blame] | 173 | '`%s` failed, try\n\t%s' % (' '.join(cmd), fix))) |
mtklein | 4db3b79 | 2016-08-03 14:18:22 -0700 | [diff] [blame] | 174 | return results |
| 175 | |
Ravi Mistry | 6eca579 | 2020-12-16 11:42:29 -0500 | [diff] [blame] | 176 | |
| 177 | def _CheckGitConflictMarkers(input_api, output_api): |
| 178 | pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$') |
| 179 | results = [] |
| 180 | for f in input_api.AffectedFiles(): |
| 181 | for line_num, line in f.ChangedContents(): |
| 182 | if f.LocalPath().endswith('.md'): |
| 183 | # First-level headers in markdown look a lot like version control |
| 184 | # conflict markers. http://daringfireball.net/projects/markdown/basics |
| 185 | continue |
| 186 | if pattern.match(line): |
| 187 | results.append( |
| 188 | output_api.PresubmitError( |
| 189 | 'Git conflict markers found in %s:%d %s' % ( |
| 190 | f.LocalPath(), line_num, line))) |
| 191 | return results |
| 192 | |
| 193 | |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 194 | def _CheckIncludesFormatted(input_api, output_api): |
| 195 | """Make sure #includes in files we're changing have been formatted.""" |
Mike Klein | f9ad5ba | 2019-07-29 12:34:39 -0500 | [diff] [blame] | 196 | 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] | 197 | cmd = ['python', |
| 198 | 'tools/rewrite_includes.py', |
Mike Klein | f9ad5ba | 2019-07-29 12:34:39 -0500 | [diff] [blame] | 199 | '--dry-run'] + files |
Hal Canary | 4df3d53 | 2019-07-30 13:49:45 -0400 | [diff] [blame] | 200 | if 0 != subprocess.call(cmd): |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 201 | return [output_api.PresubmitError('`%s` failed' % ' '.join(cmd))] |
| 202 | return [] |
borenet | 1ed2ae4 | 2016-07-26 11:52:17 -0700 | [diff] [blame] | 203 | |
Eric Boren | 58d1f76 | 2019-07-19 08:07:44 -0400 | [diff] [blame] | 204 | |
Ben Wagner | 8885550 | 2017-10-12 17:55:19 -0400 | [diff] [blame] | 205 | class _WarningsAsErrors(): |
| 206 | def __init__(self, output_api): |
| 207 | self.output_api = output_api |
| 208 | self.old_warning = None |
| 209 | def __enter__(self): |
| 210 | self.old_warning = self.output_api.PresubmitPromptWarning |
| 211 | self.output_api.PresubmitPromptWarning = self.output_api.PresubmitError |
| 212 | return self.output_api |
| 213 | def __exit__(self, ex_type, ex_value, ex_traceback): |
| 214 | self.output_api.PresubmitPromptWarning = self.old_warning |
| 215 | |
| 216 | |
Eric Boren | 6dc0021 | 2019-07-24 15:15:43 -0400 | [diff] [blame] | 217 | def _CheckDEPSValid(input_api, output_api): |
| 218 | """Ensure that DEPS contains valid entries.""" |
| 219 | results = [] |
| 220 | script = os.path.join('infra', 'bots', 'check_deps.py') |
| 221 | relevant_files = ('DEPS', script) |
| 222 | for f in input_api.AffectedFiles(): |
| 223 | if f.LocalPath() in relevant_files: |
| 224 | break |
| 225 | else: |
| 226 | return results |
| 227 | cmd = ['python', script] |
| 228 | try: |
| 229 | subprocess.check_output(cmd, stderr=subprocess.STDOUT) |
| 230 | except subprocess.CalledProcessError as e: |
| 231 | results.append(output_api.PresubmitError(e.output)) |
| 232 | return results |
| 233 | |
| 234 | |
Kevin Lubick | 2cd8067 | 2021-07-01 11:03:36 -0400 | [diff] [blame] | 235 | def _RegenerateAllExamplesCPP(input_api, output_api): |
| 236 | """Regenerates all_examples.cpp if an example was added or deleted.""" |
| 237 | if not any(f.LocalPath().startswith('docs/examples/') |
| 238 | for f in input_api.AffectedFiles()): |
| 239 | return [] |
| 240 | command_str = 'tools/fiddle/make_all_examples_cpp.py' |
| 241 | cmd = ['python', command_str] |
| 242 | if 0 != subprocess.call(cmd): |
| 243 | return [output_api.PresubmitError('`%s` failed' % ' '.join(cmd))] |
| 244 | |
| 245 | results = [] |
| 246 | git_diff_output = input_api.subprocess.check_output( |
| 247 | ['git', 'diff', '--no-ext-diff']) |
| 248 | if git_diff_output: |
| 249 | results += [output_api.PresubmitError( |
| 250 | 'Diffs found after running "%s":\n\n%s\n' |
| 251 | 'Please commit or discard the above changes.' % ( |
| 252 | command_str, |
| 253 | git_diff_output, |
| 254 | ) |
| 255 | )] |
| 256 | return results |
| 257 | |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 258 | def _CommonChecks(input_api, output_api): |
| 259 | """Presubmit checks common to upload and commit.""" |
| 260 | results = [] |
| 261 | sources = lambda x: (x.LocalPath().endswith('.h') or |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 262 | x.LocalPath().endswith('.py') or |
| 263 | x.LocalPath().endswith('.sh') or |
mtklein | 18e5580 | 2015-03-25 07:21:20 -0700 | [diff] [blame] | 264 | x.LocalPath().endswith('.m') or |
| 265 | x.LocalPath().endswith('.mm') or |
| 266 | x.LocalPath().endswith('.go') or |
| 267 | x.LocalPath().endswith('.c') or |
| 268 | x.LocalPath().endswith('.cc') or |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 269 | x.LocalPath().endswith('.cpp')) |
Ben Wagner | 8885550 | 2017-10-12 17:55:19 -0400 | [diff] [blame] | 270 | results.extend(_CheckChangeHasEol( |
| 271 | input_api, output_api, source_file_filter=sources)) |
| 272 | with _WarningsAsErrors(output_api): |
| 273 | results.extend(input_api.canned_checks.CheckChangeHasNoCR( |
| 274 | input_api, output_api, source_file_filter=sources)) |
| 275 | results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace( |
| 276 | input_api, output_api, source_file_filter=sources)) |
Ben Wagner | cf42e98 | 2018-02-09 17:41:20 -0500 | [diff] [blame] | 277 | results.extend(_JsonChecks(input_api, output_api)) |
rmistry | 01cbf6c | 2015-03-12 07:48:40 -0700 | [diff] [blame] | 278 | results.extend(_IfDefChecks(input_api, output_api)) |
borenet | c7c9180 | 2015-03-25 04:47:02 -0700 | [diff] [blame] | 279 | results.extend(_CopyrightChecks(input_api, output_api, |
| 280 | source_file_filter=sources)) |
Eric Boren | 6dc0021 | 2019-07-24 15:15:43 -0400 | [diff] [blame] | 281 | results.extend(_CheckDEPSValid(input_api, output_api)) |
Mike Klein | bb41343 | 2019-07-26 11:55:40 -0500 | [diff] [blame] | 282 | results.extend(_CheckIncludesFormatted(input_api, output_api)) |
Mike Klein | 96f6401 | 2020-04-03 10:59:37 -0500 | [diff] [blame] | 283 | results.extend(_CheckGNFormatted(input_api, output_api)) |
Ravi Mistry | 6eca579 | 2020-12-16 11:42:29 -0500 | [diff] [blame] | 284 | results.extend(_CheckGitConflictMarkers(input_api, output_api)) |
Kevin Lubick | 2cd8067 | 2021-07-01 11:03:36 -0400 | [diff] [blame] | 285 | results.extend(_RegenerateAllExamplesCPP(input_api, output_api)) |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 286 | return results |
| 287 | |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 288 | |
| 289 | def CheckChangeOnUpload(input_api, output_api): |
Ravi Mistry | 4c0ffe7 | 2020-03-02 13:19:02 -0500 | [diff] [blame] | 290 | """Presubmit checks for the change on upload.""" |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 291 | results = [] |
| 292 | results.extend(_CommonChecks(input_api, output_api)) |
borenet | 1ed2ae4 | 2016-07-26 11:52:17 -0700 | [diff] [blame] | 293 | # Run on upload, not commit, since the presubmit bot apparently doesn't have |
borenet | 60b0a2d | 2016-10-04 12:45:41 -0700 | [diff] [blame] | 294 | # coverage or Go installed. |
borenet | 2dbbfa5 | 2016-10-14 06:32:09 -0700 | [diff] [blame] | 295 | results.extend(_InfraTests(input_api, output_api)) |
Ravi Mistry | 5773516 | 2019-07-25 13:45:15 -0400 | [diff] [blame] | 296 | results.extend(_CheckReleaseNotesForPublicAPI(input_api, output_api)) |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 297 | return results |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 298 | |
| 299 | |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 300 | class CodeReview(object): |
| 301 | """Abstracts which codereview tool is used for the specified issue.""" |
| 302 | |
| 303 | def __init__(self, input_api): |
| 304 | self._issue = input_api.change.issue |
| 305 | self._gerrit = input_api.gerrit |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 306 | |
| 307 | def GetOwnerEmail(self): |
Aaron Gable | a49909a | 2017-10-09 12:50:52 -0700 | [diff] [blame] | 308 | return self._gerrit.GetChangeOwner(self._issue) |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 309 | |
| 310 | def GetSubject(self): |
Aaron Gable | a49909a | 2017-10-09 12:50:52 -0700 | [diff] [blame] | 311 | return self._gerrit.GetChangeInfo(self._issue)['subject'] |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 312 | |
| 313 | def GetDescription(self): |
Aaron Gable | a49909a | 2017-10-09 12:50:52 -0700 | [diff] [blame] | 314 | return self._gerrit.GetChangeDescription(self._issue) |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 315 | |
Ravi Mistry | 39eabb6 | 2016-10-05 08:41:12 -0400 | [diff] [blame] | 316 | def GetReviewers(self): |
Aaron Gable | a49909a | 2017-10-09 12:50:52 -0700 | [diff] [blame] | 317 | code_review_label = ( |
| 318 | self._gerrit.GetChangeInfo(self._issue)['labels']['Code-Review']) |
| 319 | return [r['email'] for r in code_review_label.get('all', [])] |
Ravi Mistry | 39eabb6 | 2016-10-05 08:41:12 -0400 | [diff] [blame] | 320 | |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 321 | def GetApprovers(self): |
| 322 | approvers = [] |
Aaron Gable | a49909a | 2017-10-09 12:50:52 -0700 | [diff] [blame] | 323 | code_review_label = ( |
| 324 | self._gerrit.GetChangeInfo(self._issue)['labels']['Code-Review']) |
| 325 | for m in code_review_label.get('all', []): |
| 326 | if m.get("value") == 1: |
| 327 | approvers.append(m["email"]) |
rmistry | b398ecc | 2016-08-29 08:13:29 -0700 | [diff] [blame] | 328 | return approvers |
| 329 | |
| 330 | |
Ravi Mistry | 5773516 | 2019-07-25 13:45:15 -0400 | [diff] [blame] | 331 | def _CheckReleaseNotesForPublicAPI(input_api, output_api): |
| 332 | """Checks to see if release notes file is updated with public API changes.""" |
| 333 | results = [] |
| 334 | public_api_changed = False |
| 335 | release_file_changed = False |
| 336 | for affected_file in input_api.AffectedFiles(): |
| 337 | affected_file_path = affected_file.LocalPath() |
| 338 | file_path, file_ext = os.path.splitext(affected_file_path) |
| 339 | # We only care about files that end in .h and are under the top-level |
| 340 | # include dir, but not include/private. |
| 341 | if (file_ext == '.h' and |
| 342 | file_path.split(os.path.sep)[0] == 'include' and |
| 343 | 'private' not in file_path): |
| 344 | public_api_changed = True |
| 345 | elif affected_file_path == RELEASE_NOTES_FILE_NAME: |
| 346 | release_file_changed = True |
| 347 | |
| 348 | if public_api_changed and not release_file_changed: |
| 349 | results.append(output_api.PresubmitPromptWarning( |
| 350 | 'If this change affects a client API, please add a summary line ' |
| 351 | 'to the %s file.' % RELEASE_NOTES_FILE_NAME)) |
| 352 | return results |
| 353 | |
| 354 | |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 355 | def PostUploadHook(gerrit, change, output_api): |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 356 | """git cl upload will call this hook after the issue is created/modified. |
| 357 | |
| 358 | This hook does the following: |
| 359 | * 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] | 360 | * Adds 'No-Try: true' if the CL contains only docs changes. |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 361 | """ |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 362 | if not change.issue: |
| 363 | return [] |
| 364 | |
| 365 | # Skip PostUploadHooks for all auto-commit service account bots. New |
| 366 | # patchsets (caused due to PostUploadHooks) invalidates the CQ+2 vote from |
| 367 | # the "--use-commit-queue" flag to "git cl upload". |
| 368 | for suffix in SERVICE_ACCOUNT_SUFFIX: |
| 369 | if change.author_email.endswith(suffix): |
| 370 | return [] |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 371 | |
| 372 | results = [] |
Ravi Mistry | 27095f2 | 2021-04-22 12:51:49 +0000 | [diff] [blame] | 373 | at_least_one_docs_change = False |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 374 | all_docs_changes = True |
| 375 | for affected_file in change.AffectedFiles(): |
| 376 | affected_file_path = affected_file.LocalPath() |
| 377 | file_path, _ = os.path.splitext(affected_file_path) |
Ravi Mistry | 27095f2 | 2021-04-22 12:51:49 +0000 | [diff] [blame] | 378 | if 'site' == file_path.split(os.path.sep)[0]: |
| 379 | at_least_one_docs_change = True |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 380 | else: |
| 381 | all_docs_changes = False |
Ravi Mistry | 27095f2 | 2021-04-22 12:51:49 +0000 | [diff] [blame] | 382 | if at_least_one_docs_change and not all_docs_changes: |
| 383 | break |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 384 | |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 385 | footers = change.GitFootersFromDescription() |
| 386 | description_changed = False |
Ravi Mistry | b5e2acc | 2017-12-07 11:10:11 -0500 | [diff] [blame] | 387 | |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 388 | # If the change includes only doc changes then add No-Try: true in the |
| 389 | # CL's description if it does not exist yet. |
| 390 | if all_docs_changes and 'true' not in footers.get('No-Try', []): |
| 391 | description_changed = True |
Edward Lemur | c631b7c | 2020-02-04 15:30:18 -0500 | [diff] [blame] | 392 | change.AddDescriptionFooter('No-Try', 'true') |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 393 | results.append( |
| 394 | output_api.PresubmitNotifyResult( |
| 395 | 'This change has only doc changes. Automatically added ' |
| 396 | '\'No-Try: true\' to the CL\'s description')) |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 397 | |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 398 | # If the description has changed update it. |
| 399 | if description_changed: |
| 400 | gerrit.UpdateDescription( |
| 401 | change.FullDescriptionText(), change.issue) |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 402 | |
Edward Lemur | 2b7876c | 2020-01-17 18:48:13 -0500 | [diff] [blame] | 403 | return results |
rmistry | d223fb2 | 2015-02-26 10:16:13 -0800 | [diff] [blame] | 404 | |
| 405 | |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 406 | def CheckChangeOnCommit(input_api, output_api): |
Ravi Mistry | 4c0ffe7 | 2020-03-02 13:19:02 -0500 | [diff] [blame] | 407 | """Presubmit checks for the change on commit.""" |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 408 | results = [] |
rmistry@google.com | 6be0b4c | 2013-01-17 14:50:59 +0000 | [diff] [blame] | 409 | results.extend(_CommonChecks(input_api, output_api)) |
Ravi Mistry | a70cb8a | 2017-09-12 13:52:05 -0400 | [diff] [blame] | 410 | # Checks for the presence of 'DO NOT''SUBMIT' in CL description and in |
| 411 | # content of files. |
| 412 | results.extend( |
| 413 | input_api.canned_checks.CheckDoNotSubmit(input_api, output_api)) |
rmistry@google.com | 8e3ff8c | 2013-01-17 12:55:34 +0000 | [diff] [blame] | 414 | return results |