Chromium Code Reviews
|
| 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 """Main app that handles incoming mail and dispatches it to handlers.""" | |
| 6 | |
| 7 import logging | |
| 8 import re | |
| 9 | |
| 10 import webapp2 | |
| 11 import webob.exc | |
| 12 | |
| 13 from google.appengine.api import app_identity | |
| 14 from google.appengine.api import mail | |
| 15 | |
| 16 import third_party # pylint: disable=W0611 | |
| 17 | |
| 18 import handlers.policy_checklist | |
| 19 from review import Review | |
| 20 from rietveld import Rietveld | |
| 21 import util | |
| 22 | |
| 23 | |
| 24 # Lists the handlers served by the review bot app. The local part of email | |
| 25 # addresses the app receives email on is used to as a key into the handler map. | |
|
agable
2013/08/20 08:42:41
nit: ...is used as a key...
Mattias Nissler (ping if slow)
2013/08/22 12:44:23
Done.
| |
| 26 # Each handler is just a function that gets called with the email address, the | |
| 27 # email message, a Review object and a Rietveld interface. | |
| 28 # | |
| 29 # New handlers can be added by adding the code in handlers/<handler_name>.py, | |
| 30 # importing the module and adding an entry to the HANDLERS map. | |
| 31 HANDLERS = { | |
| 32 'policy_checklist': handlers.policy_checklist.process | |
| 33 } | |
| 34 | |
| 35 | |
| 36 # Regular expression that matches email addresses belonging to the review bot | |
| 37 # app and extracts the handler name. | |
| 38 REVIEW_BOT_RECIPIENT_RE = re.compile('^([^@]+)@%s.appspotmail.com$' % | |
| 39 app_identity.get_application_id()) | |
| 40 | |
| 41 | |
| 42 # This is the regular expression that rietveld uses to extract the issue number | |
| 43 # from the mail subject at the time of writing this. This code needs to be kept | |
| 44 # up-to-date with the mechanism rietveld use the tools don't confuse issues. | |
|
agable
2013/08/20 08:42:41
nit: ...uses so the tools...
Mattias Nissler (ping if slow)
2013/08/22 12:44:23
Done.
| |
| 45 RIETVELD_ISSUE_NUMBER_RE = re.compile(r'\(issue *(?P<id>\d+)\)$') | |
| 46 | |
| 47 | |
| 48 class MailDispatcher(webapp2.RequestHandler): | |
| 49 """Dispatches mail to handlers as indicated by email addresses.""" | |
| 50 | |
| 51 def post(self): | |
| 52 """Handles POST requests. | |
| 53 | |
| 54 Parses the incoming mail message. Dispatches to interested handlers based on | |
| 55 the list of mail recipients. | |
| 56 """ | |
| 57 | |
| 58 # Singleton Rietveld interface for this request. | |
| 59 rietveld = Rietveld() | |
| 60 | |
| 61 # Parse the message and instantiate the review interface. | |
| 62 message = mail.InboundEmailMessage(self.request.body) | |
| 63 match = RIETVELD_ISSUE_NUMBER_RE.search(message.subject) | |
| 64 if match is None: | |
| 65 raise webob.exc.HTTPBadRequest('Failed to parse issue id: %s' % | |
| 66 message.subject) | |
| 67 review = Review(rietveld, match.groupdict()['id']) | |
| 68 | |
| 69 # Determine recipients and run the handlers one by one. | |
| 70 recipients = set(util.get_emails(getattr(message, 'to', '')) + | |
| 71 util.get_emails(getattr(message, 'cc', ''))) | |
| 72 for addr in recipients: | |
| 73 match = REVIEW_BOT_RECIPIENT_RE.match(addr) | |
| 74 if not match: | |
| 75 continue | |
| 76 | |
| 77 try: | |
| 78 handler = HANDLERS[match.group(1)] | |
| 79 except KeyError: | |
| 80 continue | |
| 81 | |
| 82 try: | |
| 83 handler(addr, message, review, rietveld) | |
| 84 except: # pylint: disable=W0702 | |
| 85 logging.exception('Handler %s failed!', match.group(1)) | |
| 86 | |
| 87 def handle_exception(self, exception, debug): | |
| 88 """Handles exceptions to print HTTP error details. | |
| 89 | |
| 90 Args: | |
| 91 exception: The exception. | |
| 92 debug: Whether we're in debug mode. | |
| 93 """ | |
| 94 if isinstance(exception, webob.exc.HTTPException): | |
| 95 logging.warning('Request %s failed: %d - %s', | |
| 96 self.request.url, exception.code, exception.detail) | |
| 97 | |
| 98 webapp2.RequestHandler.handle_exception(self, exception, debug) | |
| 99 | |
| 100 | |
| 101 app = webapp2.WSGIApplication([('/_ah/mail/.*', MailDispatcher)]) | |
| OLD | NEW |