-
Notifications
You must be signed in to change notification settings - Fork 10
/
gitlab-hook-postcommit-coloremail
executable file
·317 lines (287 loc) · 11 KB
/
gitlab-hook-postcommit-coloremail
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python
# gitlab-hook-postcommit-coloremail (part of ossobv/vcutil)
# // wdoekes/2013-2016 // Public Domain
#
# Sends out a nice colored e-mail with the committed changeset for
# each committed revision. It uses the awesome vim(1) syntax
# highlighting to colorize the patch files.
#
# Installation:
#
# Read about web hooks here: http://YOUR_GITLAB/help/web_hooks
# Basically, it sends out a JSON blob as HTTP POST data to your web
# hook. You can configure this script to run on a CGI webserver
# on the local machine.
#
# Then, go to: https://YOUR_GITLAB/YOUR_PROJECT/hooks
# Add the URL to the local CGI webserver, e.g.:
# http://127.0.0.1:81/hooks/gitlab-hook-postcommit-coloremail
#
# Example lighttpd config:
#
# server.pid-file = "/var/run/lighttpd.pid"
# server.username = "www-data"
# # Observe that we need gitlab read powers!
# server.groupname = "git"
# server.bind = "127.0.0.1"
# server.port = 81
# server.errorlog = "/var/log/lighttpd/error.log"
# server.breakagelog = "/var/log/lighttpd/breakage.log"
# server.document-root = "/srv/lighttpd-cgi"
# server.modules = (
# "mod_cgi",
# "mod_setenv"
# )
# cgi.execute-x-only = "enable"
# # (Add .py suffix to this script.)
# cgi.assign = (".py" => "/usr/bin/python")
# setenv.add-environment = (
# "GITLAB_HOOK_RC" => "/srv/lighttpd-cgi/gitlab-hook-rc.py"
# )
#
# Example usage:
#
# ./gitlab-hook-postcommit-coloremail \
# < gitlab-hook-postcommit-coloremail.example
#
# Todo:
#
# * Allow MAIL_TO to be passed as QUERY_STRING.
#
from __future__ import print_function
import json
import os
import smtplib
import string
import tempfile
import traceback
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
from subprocess import check_output # python2.7+
# Settings:
if 'PATH' not in os.environ: # lighttpd unsets it: we lose /usr/local/bin
os.environ['PATH'] = '/usr/local/bin:/usr/bin:/bin'
REPOSITORIES_ROOT = '/home/git/repositories/'
MAIL_SERVER = '127.0.0.1'
MAIL_FROM = '[email protected]'
MAIL_TO = ['[email protected]'] # list of recipients
SUBJECT = '[%(project)s] %(shortid)s %(author)s: %(summary)s'
BODY_PREFIX = 'URL: %(url)s\n\n'
BODY_SUFFIX = (u'diff ENDS HERE\n' + (72 * u'=') + u'\n'
u'D\u20acBUG: %(gitlab_json)s\n')
def update_globals_from_file(filename):
"""
Read a python file with updated globals.
# Example gitlab-hook-rc.py
MAIL_FROM = '[email protected]
MAIL_SERVER = 'smtp.mycompany.com'
"""
globals_ = {}
locals_ = {}
try:
with open(filename, 'r') as source_file:
code = compile(source_file.read(), filename, 'exec')
except IOError:
return
else:
exec(code, globals_, locals_)
uppercase_values = dict(
(key, value) for (key, value) in locals_.items()
if key[0].isupper())
globals().update(uppercase_values)
def is_daemon_child():
pid = os.fork()
if pid:
# Reap the zombie by fetching the return value. The second fork ensures
# that that's done quickly.
os.waitpid(pid, 0)
return False
# For a process to be truly daemonized (ran in the background) we should
# ensure that the session leader is killed so that there is no possibility
# of the session ever taking control of the TTY.
os.setsid()
# Second fork, do it as soon as possible.
if os.fork():
os._exit(0)
# Begin cleanup after ourselves. Release mount points and close fd's.
os.chdir('/')
os.closerange(0, 4095)
# We're daemonized.
return True
def usernameify(full_name):
"""
Take initials of full name, join and convert to lowercase.
>>> usernameify('Walter Jakob Doekes')
'wjdoekes'
"""
names = [i.strip() for i in full_name.split(' ') if i.strip()]
surname = names.pop(-1)
username = ''.join(i[0] for i in names) + surname
username = ''.join(i for i in username.lower()
if i in string.ascii_lowercase + string.digits)
return username
def diff_to_html(diff):
"""
Ask vim(1) to colorize the diff and return it as html.
"""
txttmp = tempfile.mktemp() # vim will write to txttmp+'.html'(!)
htmltmp = txttmp + '.html'
try:
# Write text to txttmp.
with open(txttmp, 'w') as txt:
txt.write(diff.encode('UTF-8'))
# Call vim(1) on it.
DEVZERO = open('/dev/zero', 'r') # yes(1) is not happy without stdin
DEVNULL = open('/dev/null', 'w')
output = check_output(
'yes | vim -n -T builtin_ansi -c '
"'syn on|set syn=diff|set enc=utf8|set bg=dark|runtime "
"syntax/2html.vim|wqa' "
'"%(txttmp)s" 2>&1 >/dev/null | '
"grep -v '^Vim: Warning: '; true" % {'txttmp': txttmp},
stdin=DEVZERO, stderr=DEVNULL, shell=True
)
DEVZERO.close()
DEVNULL.close()
if output:
raise ValueError('vim returned this: %r' % (output,))
# Read htmltmp.
with open(htmltmp, 'r') as html:
diff_html = html.read()
# Should not fail, but let's just handle the case when some crap
# does enter the output.
diff_html = diff_html.decode('UTF-8', 'replace')
finally:
try:
os.unlink(txttmp)
except Exception:
pass
try:
os.unlink(htmltmp)
except Exception:
pass
return diff_html
def send_mails(decoded):
# Init vars.
pushvars = {}
pushvars['project'] = decoded['repository']['name']
pushvars['pusher'] = decoded['user_name']
pushvars['homepage'] = decoded['repository']['homepage']
pushvars['commit_count'] = decoded['total_commits_count']
pushvars['gitlab_json'] = json.dumps(decoded, indent=4)
# Fetch changeset and colorize it.
os.chdir(os.path.join(REPOSITORIES_ROOT,
decoded['repository']['url'].split(':', 1)[-1]))
for commit in decoded['commits']:
# Get local vars.
commitvars = pushvars.copy()
commitvars['id'] = commit['id']
commitvars['shortid'] = commit['id'][0:7]
commitvars['author'] = usernameify(commit['author']['name'])
commitvars['summary'] = commit['message'].split('\n')[0].strip()
commitvars['url'] = commit['url']
# `git show -p` output already provides enough info to populate a body.
changes = check_output(['git', 'show', '-p', commit['id']],
stdin=None, stderr=None, shell=False)
# `echo "$changes" | censored-for-email` to censor #CENSORED\# bits.
with tempfile.NamedTemporaryFile() as changes_file:
changes_file.write(changes)
changes_file.flush()
changes_file.seek(0)
try:
# Why do we call an external app? Because we don't want to
# duplicate the censorship code. Recoding the censored-for-
# email app into python is easy, but less flexible.
changes = check_output(['censored-for-email'],
stdin=changes_file, stderr=None,
shell=False)
except OSError:
# The censored-for-email(1) program wasn't found. Never mind.
pass
# Translate to unicode so we can recode non-UTF-8 to UTF-8.
try:
changes = changes.decode('UTF-8')
except UnicodeDecodeError:
# From: WIKIPEDIA/Windows-1252
# > Most modern web browsers and e-mail clients treat the MIME
# > charset ISO-8859-1 as Windows-1252 to accommodate such
# > mislabeling. This is now standard behavior in the draft
# > HTML 5 specification, which requires that documents
# > advertised as ISO-8859-1 actually be parsed with the
# > Windows-1252 encoding.
# This is probably the most common 8-bit encoding.
changes = changes.decode('CP1252', 'replace')
# Trim changeset to 256KB, which would become about the double
# in colored html. That should be low enough to pass through
# most mail exchanges -- the common low limit being 2MB.
if len(changes) > (256 * 1024):
extra_prefix = ('NOTE: this changeset was truncated from '
'%d KB to 256 KB for e-mail transport\n\n' %
(len(changes) / 1024,))
extra_suffix = '\n... truncated ...\n'
changes = changes[0:(256 * 1024)]
else:
extra_prefix = extra_suffix = ''
# Prefix the changes with a bit of extra body.
changes = ''.join([BODY_PREFIX % commitvars,
extra_prefix,
changes,
extra_suffix,
BODY_SUFFIX % commitvars])
# Colorize it using vim.
changes_html = diff_to_html(changes)
# Compile a mail and send.
msg = MIMEMultipart('alternative')
msg['Subject'] = SUBJECT % commitvars
msg['From'] = MAIL_FROM
msg['To'] = ', '.join(MAIL_TO)
msg['Date'] = formatdate(localtime=True)
msg.attach(MIMEText(changes, 'plain', _charset='UTF-8'))
msg.attach(MIMEText(changes_html, 'html', _charset='UTF-8'))
s = smtplib.SMTP(MAIL_SERVER)
s.sendmail(MAIL_FROM, MAIL_TO, msg.as_string())
s.quit()
def get_stdin():
data = []
try:
while True:
in_ = input()
if not in_:
break
data.append(in_)
except EOFError:
pass
return '\n'.join(data)
# Optionally update settings
GITLAB_HOOK_RC = os.environ.get(
'GITLAB_HOOK_RC', '/etc/gitlab-hook-rc.py')
update_globals_from_file(GITLAB_HOOK_RC)
# If this is a POST request, daemonize and send OK to caller. If we're
# ran from the CLI, don't daemonize and don't print any HTTP status.
is_cgi = bool(os.environ.get('REQUEST_METHOD')) # GET/POST/HEAD
# Get data, must be done in the foreground.
stdin = get_stdin()
if not is_cgi:
# Do everything in the foreground (testing mode).
decoded = json.loads(stdin.decode('utf-8'))
send_mails(decoded)
elif is_daemon_child():
# Do that mailing as a background job.
try:
decoded = json.loads(stdin.decode('utf-8'))
send_mails(decoded)
except Exception:
backtrace = traceback.format_exc()
msg = MIMEText(backtrace + '\n\n\n' + stdin)
msg['Subject'] = 'ERROR in gitlab-hook-postcommit-coloremail'
msg['From'] = MAIL_FROM
msg['To'] = ', '.join(MAIL_TO)
msg['Date'] = formatdate(localtime=True)
s = smtplib.SMTP(MAIL_SERVER)
s.sendmail(MAIL_FROM, MAIL_TO, msg.as_string())
s.quit()
else:
# Output back to webserver.
print(b"Content-Type: text/plain\r\n\r\nOK")
# vim: set ts=8 sw=4 sts=4 et ai tw=79: