This repository has been archived by the owner on Sep 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathemailer.py
204 lines (161 loc) · 6.32 KB
/
emailer.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import envelopes
import envelopes.connstack
from flask import Flask
import flask
import hmac
import json
import logging
import os
import os.path
import rollbar
import rollbar.contrib.flask
import sha
app = Flask(__name__)
logging.basicConfig(level=logging.INFO)
@app.before_first_request
def init_rollbar():
"""Configure rollbar to capture exceptions."""
if app.config.get('TESTING', False):
logging.warn(
'Skipping rollbar init because TESTING flag is set on flask app.')
return
rollbar.init(
# throw KeyError if env var is not set.
os.environ['ROLLBAR_ACCESS_TOKEN'],
os.environ.get('GITHUB_COMMIT_EMAILER_ROLLBAR_ENV',
'github-email-notifications'),
root=os.path.dirname(os.path.realpath(__file__)),
allow_logging_basic_config=False
)
flask.got_request_exception.connect(
rollbar.contrib.flask.report_exception, app)
@app.before_request
def app_before_request():
envelopes.connstack.push_connection(
envelopes.SendGridSMTP(
login=os.environ.get('SENDGRID_USERNAME'),
password=os.environ.get('SENDGRID_PASSWORD'))
)
@app.after_request
def app_after_request(response):
envelopes.connstack.pop_connection()
return response
@app.route('/')
def index():
"""Redirect to chapel homepage."""
return flask.redirect('http://chapel-lang.org/', code=301)
@app.route('/commit-email', methods=['POST'])
def commit_email():
"""Receive web hook from github and generate email."""
# Only look at push events. Ignore the rest.
event = flask.request.headers['x-github-event']
logging.info('Received "{0}" event from github.'.format(event))
if event != 'push':
logging.info('Skipping "{0}" event.'.format(event))
return 'nope'
# Verify signature.
secret = _get_secret()
gh_signature = flask.request.headers.get('x-hub-signature', '')
if not _valid_signature(gh_signature, flask.request.data, secret):
logging.warn('Invalid signature, skipping request.')
return 'nope'
json_dict = flask.request.get_json()
logging.info('json body: {0}'.format(json_dict))
if json_dict['deleted']:
logging.info('Branch was deleted, skipping email.')
return 'nope'
added = '\n'.join(map(lambda f: 'A {0}'.format(f),
json_dict['head_commit']['added']))
removed = '\n'.join(map(lambda f: 'R {0}'.format(f),
json_dict['head_commit']['removed']))
modified = '\n'.join(map(lambda f: 'M {0}'.format(f),
json_dict['head_commit']['modified']))
changes = '\n'.join(filter(lambda i: bool(i), [added, removed, modified]))
pusher_email = '{0} <{1}>'.format(json_dict['pusher']['name'],
json_dict['pusher']['email'])
msg_info = {
'repo': json_dict['repository']['full_name'],
'branch': json_dict['ref'],
'revision': json_dict['head_commit']['id'][:7],
'message': json_dict['head_commit']['message'],
'changed_files': changes,
'pusher': json_dict['pusher']['name'],
'pusher_email': pusher_email,
'compare_url': json_dict['compare'],
}
_send_email(msg_info)
return 'yep'
def _get_secret():
"""Returns secret from environment. Raises ValueError if not set
in environment."""
if 'GITHUB_COMMIT_EMAILER_SECRET' not in os.environ:
logging.error('No secret configured in environment.')
raise ValueError('No secret configured in environment.')
return os.environ.get('GITHUB_COMMIT_EMAILER_SECRET')
def _send_email(msg_info):
"""Create and send commit notification email."""
sender = _get_sender(msg_info['pusher_email'])
recipient = os.environ.get('GITHUB_COMMIT_EMAILER_RECIPIENT')
if sender is None or recipient is None:
logging.error('sender and recipient config vars must be set.')
raise ValueError('sender and recipient config vars must be set.')
reply_to = os.environ.get('GITHUB_COMMIT_EMAILER_REPLY_TO', None)
approved = os.environ.get('GITHUB_COMMIT_EMAILER_APPROVED_HEADER', None)
subject = _get_subject(msg_info['repo'], msg_info['message'])
body = """Branch: {branch}
Revision: {revision}
Author: {pusher}
Log Message:
------------
{message}
Modified Files:
---------------
{changed_files}
Compare: {compare_url}
""".format(**msg_info)
msg = envelopes.Envelope(
to_addr=recipient,
from_addr=sender,
subject=subject,
text_body=body
)
if reply_to is not None:
msg.add_header('Reply-To', reply_to)
if approved is not None:
msg.add_header('Approved', approved)
# Disable SendGrid click tracking.
send_grid_disable_click_tracking = json.dumps(
{'filters': {'clicktrack': {'settings': {'enable': 0}}}})
msg.add_header('X-SMTPAPI', send_grid_disable_click_tracking)
smtp = envelopes.connstack.get_current_connection()
logging.info('Sending email: {0}'.format(msg))
smtp.send(msg)
def _get_sender(pusher_email):
"""Returns "From" address based on env config and default from."""
use_author = 'GITHUB_COMMIT_EMAILER_SEND_FROM_AUTHOR' in os.environ
if use_author:
sender = pusher_email
else:
sender = os.environ.get('GITHUB_COMMIT_EMAILER_SENDER')
return sender
def _get_subject(repo, message):
"""Returns subject line from repo name and commit message."""
message_lines = message.splitlines()
# For github merge commit messages, the first line is "Merged pull request
# #blah ...", followed by two line breaks. The third line is where the
# author's commit message starts. So, if a third line is available, use
# it. Otherwise, just use the first line.
if len(message_lines) >= 3:
subject_msg = message_lines[2]
else:
subject_msg = message_lines[0]
subject_msg = subject_msg[:50]
subject = '[{0}] {1}'.format(repo, subject_msg)
return subject
def _valid_signature(gh_signature, body, secret):
"""Returns True if GitHub signature is valid. False, otherwise."""
if isinstance(gh_signature, unicode):
gh_signature = str(gh_signature)
expected_hmac = hmac.new(secret, body, sha)
expected_signature = 'sha1=' + expected_hmac.hexdigest()
return hmac.compare_digest(expected_signature, gh_signature)