OLD | NEW |
| (Empty) |
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 """Handler for chrome/app/policy/policy_templates.json | |
6 | |
7 This handler examines changes to the policy_templates.json file and posts | |
8 comments with checklists for the patch author and reviewer to go through to | |
9 avoid common pitfalls. | |
10 """ | |
11 | |
12 import os | |
13 | |
14 import jinja2 | |
15 | |
16 import model.app_config | |
17 import util | |
18 import handlers.policy_checklist.parser | |
19 | |
20 | |
21 POLICY_TEMPLATES_FILE = 'chrome/app/policy/policy_templates.json' | |
22 MAX_INLINE_COMMENTS = 10 | |
23 | |
24 | |
25 REVIEW_MESSAGE_TEMPLATE = 'review_message.txt' | |
26 ADDITION_COMMENT_TEMPLATE = 'addition_comment.txt' | |
27 MODIFICATION_COMMENT_TEMPLATE = 'modification_comment.txt' | |
28 | |
29 | |
30 JINJA_ENVIRONMENT = jinja2.Environment( | |
31 loader=jinja2.FileSystemLoader(os.path.dirname(__file__))) | |
32 | |
33 | |
34 def prepare_address_list(addr, email_list): | |
35 """Prepares |email_list| for use as rietveld query parameter. | |
36 | |
37 Canonicalizes the entries in |email_list|, removes any occurrences of |addr|, | |
38 joins the entries with commas and returns the result. | |
39 """ | |
40 return ','.join([util.canonicalize_email(entry) | |
41 for entry in email_list if entry != addr]) | |
42 | |
43 | |
44 def process(addr, message, review, rietveld): | |
45 """Handles reviews for chrome/app/policy/policy_templates.json. | |
46 | |
47 This looks at the patch to identify additions/modifications to policy | |
48 definitions and posts comments with a checklist intended for the author and | |
49 reviewer to go through in order to catch common mistakes. | |
50 """ | |
51 | |
52 if POLICY_TEMPLATES_FILE not in review.latest_patchset.files: | |
53 return | |
54 | |
55 # Only process the change if the mail is directly to us or we haven't | |
56 # processed this review yet. | |
57 client_id = model.app_config.get().client_id | |
58 if (not addr in util.get_emails(getattr(message, 'to', '')) and | |
59 client_id in [m.sender for m in review.issue_data.messages]): | |
60 return | |
61 | |
62 # Don't process reverts. | |
63 if 'revert' in review.issue_data.description.lower(): | |
64 return | |
65 | |
66 # Parse the patch, look at the chunks and generate inline comments. | |
67 chunks = handlers.policy_checklist.parser.parse( | |
68 review.latest_patchset.files[POLICY_TEMPLATES_FILE].patch.lines) | |
69 for chunk in chunks[0:MAX_INLINE_COMMENTS]: | |
70 if chunk.additions and not chunk.removals: | |
71 template = JINJA_ENVIRONMENT.get_template(ADDITION_COMMENT_TEMPLATE) | |
72 else: | |
73 template = JINJA_ENVIRONMENT.get_template(MODIFICATION_COMMENT_TEMPLATE) | |
74 | |
75 if chunk.comment_pos[1] is not None: | |
76 line, side = chunk.comment_pos[1], 'b' | |
77 elif chunk.comment_pos[0] is not None: | |
78 line, side = chunk.comment_pos[0], 'a' | |
79 else: | |
80 # No suitable position? | |
81 continue | |
82 | |
83 rietveld.add_inline_comment( | |
84 review.issue_id, review.latest_patchset.patchset, | |
85 review.latest_patchset.files[POLICY_TEMPLATES_FILE].id, | |
86 line, side, template.render(review=review, chunk=chunk)) | |
87 | |
88 # Finally, post all inline comments. | |
89 if len(chunks) > 0: | |
90 template = JINJA_ENVIRONMENT.get_template(REVIEW_MESSAGE_TEMPLATE) | |
91 rietveld.publish_inline_comments( | |
92 review.issue_id, template.render(review=review), | |
93 prepare_address_list(addr, review.issue_data.reviewers), | |
94 prepare_address_list(addr, review.issue_data.cc)) | |
OLD | NEW |