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
|
#!/usr/bin/python2
import os
import re
import sys
LIBDIR = '<%= @gitolite_commonhooksdir %>'
sys.path.insert(0, LIBDIR)
import git_multimail
import xmlrpclib
from bugz.bugzilla import BugzillaProxy
import urllib2
# When editing this list, remember to edit the same list in
# modules/cgit/templates/filter.commit-links.sh
BUG_REFS = {
'Mageia': { 're': re.compile('mga#([0-9]+)'), 'replace': 'https://bugs.mageia.org/%s' },
'Red Hat': { 're': re.compile('rhbz#([0-9]+)'), 'replace': 'https://bugzilla.redhat.com/show_bug.cgi?id=%s' },
'Free Desktop': { 're': re.compile('fdo#([0-9]+)'), 'replace': 'https://bugs.freedesktop.org/show_bug.cgi?id=%s' },
'KDE': { 're': re.compile('(?:bko|kde)#([0-9]+)'), 'replace': 'https://bugs.kde.org/show_bug.cgi?id=%s' },
'GNOME': { 're': re.compile('(?:bgo|gnome)#([0-9]+)'), 'replace': 'https://bugzilla.gnome.org/show_bug.cgi?id=%s' },
'Launchpad': { 're': re.compile('lp#([0-9]+)'), 'replace': 'https://launchpad.net/bugs/%s' },
}
COMMIT_RE = re.compile('^commit ([a-f0-9]{40})')
COMMIT_REPLACE = 'https://gitweb.mageia.org/%s/commit/?id=%s'
MAGEIA_BUGZILLA_URL = 'https://bugs.mageia.org/xmlrpc.cgi'
MAGEIA_BUGZILLA_PASSWORD_FILE = '.gitzilla-password'
MAGEIA_BUGZILLA_AUTHTOKEN_FILE = '.gitzilla-authtoken'
git_multimail.FOOTER_TEMPLATE = """\
-- \n\
Mageia Git Monkeys.
"""
git_multimail.REVISION_FOOTER_TEMPLATE = git_multimail.FOOTER_TEMPLATE
I18N_REVISION_HEADER_TEMPLATE = """\
Date: %(send_date)s
To: %(recipients)s
Subject: %(emailprefix)s%(oneline)s
MIME-Version: 1.0
Content-Type: text/plain; charset=%(charset)s
Content-Transfer-Encoding: 8bit
From: %(fromaddr)s
Reply-To: %(reply_to)s
In-Reply-To: %(reply_to_msgid)s
References: %(reply_to_msgid)s
X-Git-Host: %(fqdn)s
X-Git-Repo: %(repo_shortname)s
X-Git-Refname: %(refname)s
X-Git-Reftype: %(refname_type)s
X-Git-Rev: %(rev)s
Auto-Submitted: auto-generated
"""
REPO_NAME_RE = re.compile(r'^/git/(?P<name>.+?)(?:\.git)?$')
def repo_shortname():
basename = os.path.abspath(git_multimail.get_git_dir())
m = REPO_NAME_RE.match(basename)
if m:
return m.group('name')
else:
return basename
# Override the Environment class to generate an apporpriate short name which is
# used in git links and as an email prefix
class MageiaEnvironment(git_multimail.Environment):
def get_repo_shortname(self):
return repo_shortname()
git_multimail.Environment = MageiaEnvironment
# Override the Revision class to inject gitweb/cgit links and any referenced
# bug URLs
class MageiaLinksRevision(git_multimail.Revision):
bz = None
tokenfile = None
token = None
def bugzilla_init(self):
if self.bz is None:
self.tokenfile = os.path.join(os.environ['HOME'], MAGEIA_BUGZILLA_AUTHTOKEN_FILE)
try:
token = open(self.tokenfile, 'r').readline().rstrip()
if token:
self.token = token
except IOError:
pass
self.bz = BugzillaProxy(MAGEIA_BUGZILLA_URL)
return self.bz
def bugzilla_login(self):
params = {
'login': 'bot',
'password': open(os.path.join(os.environ['HOME'], MAGEIA_BUGZILLA_PASSWORD_FILE), 'r').readline().rstrip(),
'remember': True
}
result = self.bz.User.login(params)
if 'token' in result:
self.token = result['token']
if self.tokenfile is not None:
fd = open(self.tokenfile, 'w')
fd.write(self.token)
fd.write('\n')
fd.close()
os.chmod(self.tokenfile, 0600)
return True
return False
def bugzilla_call(self, method, *args):
"""Attempt to call method with args. Log in if authentication is required.
"""
try:
if self.token is not None:
args[0]['token'] = self.token
return method(*args)
except xmlrpclib.Fault, fault:
# Fault code 410 means login required
if fault.faultCode == 410 and self.bugzilla_login():
args[0]['token'] = self.token
return method(*args)
raise
def generate_email_body(self, push):
"""Show this revision."""
output = git_multimail.read_git_lines(
['log'] + self.environment.commitlogopts + ['-1', self.rev.sha1],
keepends=True,
)
bugs = {}
commit = None
idx = 0
for line in output:
idx += 1
if line == "---\n":
if commit and COMMIT_REPLACE:
output.insert(idx, "\n")
output.insert(idx, " %s\n" % (COMMIT_REPLACE % (self.environment.get_repo_shortname(), commit)))
output.insert(idx, " Commit Link:\n")
idx += 3
if bugs:
output.insert(idx, " Bug links:\n")
idx += 1
for tracker, bugnos in bugs.items():
output.insert(idx, " %s\n" % tracker)
idx += 1
for bugno in bugnos:
output.insert(idx, " %s\n" % (BUG_REFS[tracker]['replace'] % bugno))
idx += 1
output.insert(idx, "\n")
idx += 1
# Attempt to modify bugzilla
if "Mageia" in bugs:
try:
bz = self.bugzilla_init()
# Mask email address
comment = None
# Suppress the "Bug links:" section if only one bug
# is referenced
if len(bugs) == 1 and len(bugs['Mageia']) == 1:
comment = output[0:idx-4]
else:
comment = output[0:idx]
comment[1] = re.sub(r'^(Author: [^@]*)@.*(>)?', r'\1@...>', comment[1])
comment = "".join(comment)
params = {}
params['ids'] = bugs['Mageia']
params['comment'] = { 'body': comment }
self.bugzilla_call(bz.Bug.update, params)
print "Updated bugzilla bugs: %s" % ", ".join(bugs['Mageia'])
except:
print "Unable to post to bugzilla bugs: %s :(" % ", ".join(bugs['Mageia'])
print sys.exc_info()[1]
break
m = COMMIT_RE.search(line)
if m:
commit = m.group(1)
for tracker in BUG_REFS.keys():
foundbugs = BUG_REFS[tracker]['re'].findall(line)
if len(foundbugs):
if not tracker in bugs:
bugs[tracker] = foundbugs
else:
bugs[tracker] = list(set(bugs[tracker] + foundbugs))
return output
# Override the Revision class to inject gitweb/cgit links and any referenced
# bug URLs
class MageiaI18NRevision(git_multimail.Revision):
"""A Change consisting of a single git commit."""
def __init__(self, reference_change, rev, num, tot):
git_multimail.Change.__init__(self, reference_change.environment)
self.reference_change = reference_change
self.rev = rev
self.change_type = self.reference_change.change_type
self.refname = self.reference_change.refname
self.num = num
self.tot = tot
self.author = git_multimail.read_git_output(['log', '--no-walk', '--format=%aN <%%aE>', self.rev.sha1])
self.recipients = False
self.output = []
# -s is short for --no-patch, but -s works on older git's (e.g. 1.7)
self.parents = git_multimail.read_git_lines(['show', '-s', '--format=%P', self.rev.sha1])[0].split()
self.cc_recipients = ''
i18n_folders = []
# Check files and find i18n folders
for line in git_multimail.read_git_lines(['ls-tree', '-rd', self.rev.sha1]):
(modetypesha1, name) = line.split("\t", 1)
if name.endswith("/.tx"):
i18n_folders.append(os.path.dirname(name))
if len(i18n_folders):
self.output = git_multimail.read_git_lines(
['log', '-C', '--stat', '-p', '--no-walk', self.rev.sha1, '--'] + i18n_folders,
keepends=True,
)
if len(self.output):
# We have some output so let's send the mail...
self.recipients = 'i18n-reports@ml.mageia.org'
def generate_email_body(self, push):
"""Show this revision."""
return self.output
if __name__ == '__main__':
# Attempt to write a last-updated file for cgit cosmetics
try:
git_dir = git_multimail.get_git_dir()
infowebdir = os.path.join(git_dir, 'info', 'web')
if not os.path.exists(infowebdir):
os.makedirs(infowebdir)
lastupdated = git_multimail.read_git_output(
['for-each-ref', '--sort=-committerdate', "--format=%(committerdate:iso8601)", '--count=1', 'refs/heads'],
)
modfile = open(os.path.join(infowebdir, 'last-modified'), 'w')
modfile.write(lastupdated)
modfile.close()
except Exception:
pass
try:
req = urllib2.Request('https://gitweb.mageia.org:8000', repo_shortname() + '.git')
req.add_header('Content-Type', 'x-git/repo')
fp = urllib2.urlopen(req, timeout=5)
if (fp):
fp.close()
except Exception:
pass
config = git_multimail.Config('multimailhook')
try:
environment = git_multimail.choose_environment(
config, osenv=os.environ,
)
mailer = git_multimail.choose_mailer(config, environment)
# For testing...
#mailer = git_multimail.OutputMailer(sys.stdout)
changes = []
for line in sys.stdin:
(oldrev, newrev, refname) = line.strip().split(' ', 2)
changes.append(
git_multimail.ReferenceChange.create(environment, oldrev, newrev, refname)
)
push = git_multimail.Push(environment, changes)
# First pass - regular commit mails
git_multimail.Revision = MageiaLinksRevision
push.send_emails(mailer, body_filter=environment.filter_body)
# Second pass - i18n commit mails
git_multimail.REVISION_HEADER_TEMPLATE = I18N_REVISION_HEADER_TEMPLATE
git_multimail.Revision = MageiaI18NRevision
# Don't send the summary email, so nuke the change recipients
for change in push.changes:
change.recipients = False
push.send_emails(mailer, body_filter=environment.filter_body)
except git_multimail.ConfigurationException, e:
sys.exit(str(e))
|