blob: 75f930619f9f6aee31b329f92f8a81361487d635 [file] [log] [blame]
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +00001# 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
8See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
9for more details about the presubmit API built into gcl.
10"""
11
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +000012import fnmatch
rmistry@google.comf6c5f752013-03-29 17:26:00 +000013import os
commit-bot@chromium.orgcfdc5962014-01-31 17:33:04 +000014import re
rmistryd223fb22015-02-26 10:16:13 -080015import subprocess
rmistry@google.comf6c5f752013-03-29 17:26:00 +000016import sys
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +000017import traceback
rmistry@google.comf6c5f752013-03-29 17:26:00 +000018
rmistry@google.comc2993442013-01-23 14:35:58 +000019
commit-bot@chromium.orgcfdc5962014-01-31 17:33:04 +000020REVERT_CL_SUBJECT_PREFIX = 'Revert '
21
rmistry@google.com547012d2013-04-12 19:45:46 +000022SKIA_TREE_STATUS_URL = 'http://skia-tree-status.appspot.com'
23
rmistryf2d83ca2014-08-26 10:30:29 -070024# Please add the complete email address here (and not just 'xyz@' or 'xyz').
rmistry@google.comfb4a68d2013-08-12 14:51:20 +000025PUBLIC_API_OWNERS = (
26 'reed@chromium.org',
27 'reed@google.com',
28 'bsalomon@chromium.org',
29 'bsalomon@google.com',
rmistry83fab472014-07-18 05:25:56 -070030 'djsollen@chromium.org',
31 'djsollen@google.com',
rmistry@google.comfb4a68d2013-08-12 14:51:20 +000032)
33
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +000034AUTHORS_FILE_NAME = 'AUTHORS'
35
rmistryd223fb22015-02-26 10:16:13 -080036DOCS_PREVIEW_URL = 'https://skia.org/?cl='
37
rmistry@google.com547012d2013-04-12 19:45:46 +000038
rmistry@google.com713276b2013-01-25 18:27:34 +000039def _CheckChangeHasEol(input_api, output_api, source_file_filter=None):
40 """Checks that files end with atleast one \n (LF)."""
41 eof_files = []
42 for f in input_api.AffectedSourceFiles(source_file_filter):
43 contents = input_api.ReadFile(f, 'rb')
44 # Check that the file ends in atleast one newline character.
45 if len(contents) > 1 and contents[-1:] != '\n':
46 eof_files.append(f.LocalPath())
47
48 if eof_files:
49 return [output_api.PresubmitPromptWarning(
50 'These files should end in a newline character:',
51 items=eof_files)]
52 return []
53
54
Eric Borenbb0ef0a2014-06-25 11:13:27 -040055def _PythonChecks(input_api, output_api):
56 """Run checks on any modified Python files."""
57 pylint_disabled_warnings = (
58 'F0401', # Unable to import.
59 'E0611', # No name in module.
60 'W0232', # Class has no __init__ method.
61 'E1002', # Use of super on an old style class.
62 'W0403', # Relative import used.
63 'R0201', # Method could be a function.
64 'E1003', # Using class name in super.
65 'W0613', # Unused argument.
66 )
67 # Run Pylint on only the modified python files. Unfortunately it still runs
68 # Pylint on the whole file instead of just the modified lines.
69 affected_python_files = []
70 for affected_file in input_api.AffectedSourceFiles(None):
71 affected_file_path = affected_file.LocalPath()
72 if affected_file_path.endswith('.py'):
73 affected_python_files.append(affected_file_path)
74 return input_api.canned_checks.RunPylint(
75 input_api, output_api,
76 disabled_warnings=pylint_disabled_warnings,
77 white_list=affected_python_files)
78
79
rmistry@google.com6be0b4c2013-01-17 14:50:59 +000080def _CommonChecks(input_api, output_api):
81 """Presubmit checks common to upload and commit."""
82 results = []
83 sources = lambda x: (x.LocalPath().endswith('.h') or
84 x.LocalPath().endswith('.gypi') or
85 x.LocalPath().endswith('.gyp') or
86 x.LocalPath().endswith('.py') or
87 x.LocalPath().endswith('.sh') or
88 x.LocalPath().endswith('.cpp'))
89 results.extend(
rmistry@google.com713276b2013-01-25 18:27:34 +000090 _CheckChangeHasEol(
rmistry@google.com6be0b4c2013-01-17 14:50:59 +000091 input_api, output_api, source_file_filter=sources))
Eric Borenbb0ef0a2014-06-25 11:13:27 -040092 results.extend(_PythonChecks(input_api, output_api))
rmistry@google.com6be0b4c2013-01-17 14:50:59 +000093 return results
94
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +000095
96def CheckChangeOnUpload(input_api, output_api):
rmistry@google.com6be0b4c2013-01-17 14:50:59 +000097 """Presubmit checks for the change on upload.
98
99 The following are the presubmit checks:
100 * Check change has one and only one EOL.
101 """
102 results = []
103 results.extend(_CommonChecks(input_api, output_api))
104 return results
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000105
106
rmistry@google.comc2993442013-01-23 14:35:58 +0000107def _CheckTreeStatus(input_api, output_api, json_url):
108 """Check whether to allow commit.
109
110 Args:
111 input_api: input related apis.
112 output_api: output related apis.
113 json_url: url to download json style status.
114 """
115 tree_status_results = input_api.canned_checks.CheckTreeIsOpen(
116 input_api, output_api, json_url=json_url)
117 if not tree_status_results:
118 # Check for caution state only if tree is not closed.
119 connection = input_api.urllib2.urlopen(json_url)
120 status = input_api.json.loads(connection.read())
121 connection.close()
rmistry@google.comf6c5f752013-03-29 17:26:00 +0000122 if ('caution' in status['message'].lower() and
123 os.isatty(sys.stdout.fileno())):
124 # Display a prompt only if we are in an interactive shell. Without this
125 # check the commit queue behaves incorrectly because it considers
126 # prompts to be failures.
rmistry@google.comc2993442013-01-23 14:35:58 +0000127 short_text = 'Tree state is: ' + status['general_state']
128 long_text = status['message'] + '\n' + json_url
129 tree_status_results.append(
130 output_api.PresubmitPromptWarning(
131 message=short_text, long_text=long_text))
rmistry@google.com547012d2013-04-12 19:45:46 +0000132 else:
133 # Tree status is closed. Put in message about contacting sheriff.
134 connection = input_api.urllib2.urlopen(
135 SKIA_TREE_STATUS_URL + '/current-sheriff')
136 sheriff_details = input_api.json.loads(connection.read())
137 if sheriff_details:
138 tree_status_results[0]._message += (
139 '\n\nPlease contact the current Skia sheriff (%s) if you are trying '
140 'to submit a build fix\nand do not know how to submit because the '
141 'tree is closed') % sheriff_details['username']
rmistry@google.comc2993442013-01-23 14:35:58 +0000142 return tree_status_results
143
144
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000145def _CheckOwnerIsInAuthorsFile(input_api, output_api):
146 results = []
147 issue = input_api.change.issue
148 if issue and input_api.rietveld:
rmistry83fab472014-07-18 05:25:56 -0700149 issue_properties = input_api.rietveld.get_issue_properties(
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000150 issue=int(issue), messages=False)
151 owner_email = issue_properties['owner_email']
152
153 try:
154 authors_content = ''
155 for line in open(AUTHORS_FILE_NAME):
156 if not line.startswith('#'):
157 authors_content += line
158 email_fnmatches = re.findall('<(.*)>', authors_content)
159 for email_fnmatch in email_fnmatches:
160 if fnmatch.fnmatch(owner_email, email_fnmatch):
161 # Found a match, the user is in the AUTHORS file break out of the loop
162 break
163 else:
164 # TODO(rmistry): Remove the below CLA messaging once a CLA checker has
165 # been added to the CQ.
166 results.append(
167 output_api.PresubmitError(
168 'The email %s is not in Skia\'s AUTHORS file.\n'
169 'Issue owner, this CL must include an addition to the Skia AUTHORS '
170 'file.\n'
171 'Googler reviewers, please check that the AUTHORS entry '
172 'corresponds to an email address in http://goto/cla-signers. If it '
173 'does not then ask the issue owner to sign the CLA at '
174 'https://developers.google.com/open-source/cla/individual '
175 '(individual) or '
176 'https://developers.google.com/open-source/cla/corporate '
177 '(corporate).'
rmistry83fab472014-07-18 05:25:56 -0700178 % owner_email))
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000179 except IOError:
180 # Do not fail if authors file cannot be found.
181 traceback.print_exc()
182 input_api.logging.error('AUTHORS file not found!')
183
184 return results
185
186
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000187def _CheckLGTMsForPublicAPI(input_api, output_api):
188 """Check LGTMs for public API changes.
189
190 For public API files make sure there is an LGTM from the list of owners in
191 PUBLIC_API_OWNERS.
192 """
193 results = []
194 requires_owner_check = False
rmistry9407ece2014-08-26 14:00:54 -0700195 for affected_file in input_api.AffectedFiles():
196 affected_file_path = affected_file.LocalPath()
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000197 file_path, file_ext = os.path.splitext(affected_file_path)
rmistry9407ece2014-08-26 14:00:54 -0700198 # We only care about files that end in .h and are under the top-level
199 # include dir.
200 if file_ext == '.h' and 'include' == file_path.split(os.path.sep)[0]:
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000201 requires_owner_check = True
202
203 if not requires_owner_check:
204 return results
205
206 lgtm_from_owner = False
207 issue = input_api.change.issue
208 if issue and input_api.rietveld:
209 issue_properties = input_api.rietveld.get_issue_properties(
210 issue=int(issue), messages=True)
commit-bot@chromium.orgcfdc5962014-01-31 17:33:04 +0000211 if re.match(REVERT_CL_SUBJECT_PREFIX, issue_properties['subject'], re.I):
212 # It is a revert CL, ignore the public api owners check.
213 return results
rmistryf2d83ca2014-08-26 10:30:29 -0700214
215 match = re.search(r'^TBR=(.*)$', issue_properties['description'], re.M)
216 if match:
217 tbr_entries = match.group(1).strip().split(',')
218 for owner in PUBLIC_API_OWNERS:
219 if owner in tbr_entries or owner.split('@')[0] in tbr_entries:
220 # If an owner is specified in the TBR= line then ignore the public
221 # api owners check.
222 return results
223
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000224 if issue_properties['owner_email'] in PUBLIC_API_OWNERS:
225 # An owner created the CL that is an automatic LGTM.
226 lgtm_from_owner = True
227
228 messages = issue_properties.get('messages')
229 if messages:
230 for message in messages:
231 if (message['sender'] in PUBLIC_API_OWNERS and
232 'lgtm' in message['text'].lower()):
233 # Found an lgtm in a message from an owner.
234 lgtm_from_owner = True
Eric Borenbb0ef0a2014-06-25 11:13:27 -0400235 break
commit-bot@chromium.orgcfdc5962014-01-31 17:33:04 +0000236
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000237 if not lgtm_from_owner:
238 results.append(
239 output_api.PresubmitError(
240 'Since the CL is editing public API, you must have an LGTM from '
241 'one of: %s' % str(PUBLIC_API_OWNERS)))
242 return results
243
244
rmistryd223fb22015-02-26 10:16:13 -0800245def PostUploadHook(cl, change, output_api):
246 """git cl upload will call this hook after the issue is created/modified.
247
248 This hook does the following:
249 * Adds a link to preview docs changes if there are any docs changes in the CL.
250 * Adds 'NOTRY=true' if the CL contains only docs changes.
251 """
252
253 results = []
254 atleast_one_docs_change = False
255 all_docs_changes = True
256 for affected_file in change.AffectedFiles():
257 affected_file_path = affected_file.LocalPath()
258 file_path, _ = os.path.splitext(affected_file_path)
259 if 'site' == file_path.split(os.path.sep)[0]:
260 atleast_one_docs_change = True
261 else:
262 all_docs_changes = False
263 if atleast_one_docs_change and not all_docs_changes:
264 break
265
266 issue = cl.issue
267 rietveld_obj = cl.RpcServer()
268 if issue and rietveld_obj:
269 original_description = rietveld_obj.get_description(issue)
270 new_description = original_description
271
272 # If the change includes only doc changes then add NOTRY=true in the
273 # CL's description if it does not exist yet.
274 if all_docs_changes and not re.search(
275 r'^NOTRY=true$', original_description, re.M | re.I):
276 new_description += '\nNOTRY=true'
277 results.append(
278 output_api.PresubmitNotifyResult(
279 'This change has only doc changes. Automatically added '
280 '\'NOTRY=true\' to the CL\'s description'))
281
282 # If there is atleast one docs change then add preview link in the CL's
283 # description if it does not already exist there.
284 if atleast_one_docs_change and not re.search(
285 r'^DOCS_PREVIEW=.*', original_description, re.M | re.I):
286 # Automatically add a link to where the docs can be previewed.
287 new_description += '\nDOCS_PREVIEW= %s%s' % (DOCS_PREVIEW_URL, issue)
288 results.append(
289 output_api.PresubmitNotifyResult(
290 'Automatically added a link to preview the docs changes to the '
291 'CL\'s description'))
292
293 # If the description has changed update it.
294 if new_description != original_description:
295 rietveld_obj.update_description(issue, new_description)
296
297 return results
298
299
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000300def CheckChangeOnCommit(input_api, output_api):
301 """Presubmit checks for the change on commit.
302
303 The following are the presubmit checks:
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000304 * Check change has one and only one EOL.
rmistry@google.comc2993442013-01-23 14:35:58 +0000305 * Ensures that the Skia tree is open in
306 http://skia-tree-status.appspot.com/. Shows a warning if it is in 'Caution'
307 state and an error if it is in 'Closed' state.
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000308 """
309 results = []
rmistry@google.com6be0b4c2013-01-17 14:50:59 +0000310 results.extend(_CommonChecks(input_api, output_api))
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000311 results.extend(
rmistry@google.comc2993442013-01-23 14:35:58 +0000312 _CheckTreeStatus(input_api, output_api, json_url=(
rmistry@google.com547012d2013-04-12 19:45:46 +0000313 SKIA_TREE_STATUS_URL + '/banner-status?format=json')))
rmistry@google.comfb4a68d2013-08-12 14:51:20 +0000314 results.extend(_CheckLGTMsForPublicAPI(input_api, output_api))
commit-bot@chromium.org745e08c2014-02-03 14:18:32 +0000315 results.extend(_CheckOwnerIsInAuthorsFile(input_api, output_api))
rmistry@google.com8e3ff8c2013-01-17 12:55:34 +0000316 return results