forked from luci/luci-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifications.py
164 lines (133 loc) · 4.54 KB
/
notifications.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Copyright 2015 The LUCI Authors. All rights reserved.
# Use of this source code is governed under the Apache License, Version 2.0
# that can be found in the LICENSE file.
"""Notifies interested parties about rejection of a config set revision."""
import logging
import re
import sys
from components import auth
from components import template
from components import utils
from google.appengine.api import app_identity
from google.appengine.api import mail
from google.appengine.api import mail_errors
from google.appengine.ext import ndb
from google.appengine.ext.webapp import mail_handlers
import storage
CC_GROUP = 'config-validation-cc'
RE_GIT_HASH = re.compile('^[0-9a-f]{40}$')
class FailedToNotify(Exception):
"""Raised when failed to send a notification."""
class Notification(ndb.Model):
"""Entity exists if a notification was sent.
Entity key:
Root entity. Entity id is a unique id of notification, for example
URL of a bad commit.
"""
def get_recipients(commit):
"""Returns a list of recipients for |commit|.
If committer and author have same email, returns only author.
"""
for r in (commit.author, commit.committer):
try:
mail.CheckEmailValid(r.email, 'to')
except mail.InvalidEmailError as ex:
raise FailedToNotify(
('Failed to notify %s, invalid email %s: %s' %
(r.name, r.email, ex)))
names={
commit.committer.email: commit.committer.name,
commit.author.email: commit.author.name,
}
return [
'%s <%s>' % (name or email, email)
for email, name in names.iteritems()
]
def notify_gitiles_rejection(config_set, location, validation_result):
"""Notifies interested parties about an error in a config set revision.
Sends a notification per location only once.
Args:
location (gitiles.Location): an absolute gitiles location of the config set
that could not be imported.
validation_result (components.config.validation_context.Result).
"""
assert RE_GIT_HASH.match(location.treeish), location
if Notification.get_by_id(str(location)):
logging.debug('Notification was already sent.')
return
log = location.get_log(limit=1)
if not log or not log.commits:
logging.error('could not load commit %s', location)
return
commit = log.commits[0]
app_id = app_identity.get_application_id()
rev = location.treeish[:7]
try:
template_params = {
'author': commit.author.name or commit.author.email,
'messages': [
{
'severity': logging.getLevelName(msg.severity),
'text': msg.text
}
for msg in validation_result.messages
],
'rev_link': location,
'rev_hash': rev,
'rev_repo': location.project,
'cur_rev_hash': None,
'cur_rev_link': None,
}
cs = storage.ConfigSet.get_by_id(config_set)
if cs and cs.latest_revision:
template_params.update(
cur_rev_hash=cs.latest_revision[:7],
cur_rev_link=cs.latest_revision_url,
)
msg = mail.EmailMessage(
sender=(
'%s.appspot.com <%[email protected]>' %
(app_id, app_id)),
subject='Config revision %s is rejected' % rev,
to=get_recipients(commit),
html=template.render(
'templates/validation_notification.html', template_params))
cc = get_cc_recipients()
if cc:
msg.cc = cc
logging.info('Emailing %s', ', '.join(msg.to))
_send(msg)
except mail_errors.Error as ex:
raise FailedToNotify(ex.message), None, sys.exc_info()[2]
Notification(id=str(location)).put()
@utils.cache_with_expiration(10 * 60)
def get_cc_recipients():
"""Returns a set of emails in CC group."""
identities = auth.list_group(CC_GROUP)
recipients = set()
for ident in identities:
if ident.kind == auth.IDENTITY_USER:
try:
mail.CheckEmailValid(ident.name, 'to')
recipients.add(ident.name)
except mail.InvalidEmailError:
logging.error('invalid cc recipient %s', ident.name)
return recipients
class BounceHandler(mail_handlers.BounceNotificationHandler):
"""Logs bounce notifications."""
def receive(self, bounce_message):
def to_text(msg):
return 'Subject: %s\nTo: %s\n%s\n%s' % (
msg['subject'],
msg['to'],
'CC: %s\n' % msg['cc'] if msg['cc'] else '',
msg['text'],
)
logging.error(
'Bounce notification\n%s', to_text(bounce_message.notification))
logging.info(
'Original message\n%s', to_text(bounce_message.original)
)
def _send(email_message):
# Mockable
email_message.send()