1 |
colin |
3359 |
#!/usr/bin/python |
2 |
colin |
3354 |
|
3 |
|
|
import sys |
4 |
|
|
import os |
5 |
|
|
import re |
6 |
|
|
|
7 |
colin |
3358 |
LIBDIR = '<%= @gitolite_commonhooksdir %>' |
8 |
colin |
3354 |
sys.path.insert(0, LIBDIR) |
9 |
|
|
|
10 |
|
|
import git_multimail |
11 |
|
|
|
12 |
colin |
3380 |
import xmlrpclib |
13 |
colin |
3387 |
from cookielib import LWPCookieJar |
14 |
colin |
3380 |
from bugz.bugzilla import BugzillaProxy |
15 |
|
|
|
16 |
colin |
3412 |
import urllib2 |
17 |
|
|
|
18 |
colin |
3354 |
# When editing this list, remember to edit the same list in |
19 |
|
|
# modules/cgit/templates/filter.commit-links.sh |
20 |
|
|
BUG_REFS = { |
21 |
|
|
'Mageia': { 're': re.compile('mga#([0-9]+)'), 'replace': 'https://bugs.mageia.org/show_bug.cgi?id=%s' }, |
22 |
|
|
'Red Hat': { 're': re.compile('rhbz#([0-9]+)'), 'replace': 'https://bugzilla.redhat.com/show_bug.cgi?id=%s' }, |
23 |
|
|
'Free Desktop': { 're': re.compile('fdo#([0-9]+)'), 'replace': 'https://bugs.freedesktop.org/show_bug.cgi?id=%s' }, |
24 |
|
|
'KDE': { 're': re.compile('(?:bko|kde)#([0-9]+)'), 'replace': 'https://bugs.kde.org/show_bug.cgi?id=%s' }, |
25 |
|
|
'GNOME': { 're': re.compile('(?:bgo|gnome)#([0-9]+)'), 'replace': 'https://bugzilla.gnome.org/show_bug.cgi?id=%s' }, |
26 |
|
|
'Launchpad': { 're': re.compile('lp#([0-9]+)'), 'replace': 'https://launchpad.net/bugs/%s' }, |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
COMMIT_RE = re.compile('^commit ([a-f0-9]{40})') |
30 |
|
|
COMMIT_REPLACE = 'http://gitweb.mageia.org/%s/commit/?id=%s' |
31 |
|
|
|
32 |
colin |
3380 |
MAGEIA_BUGZILLA_URL = 'https://bugs.mageia.org/xmlrpc.cgi' |
33 |
|
|
MAGEIA_BUGZILLA_PASSWORD_FILE = '.gitzilla-password' |
34 |
|
|
MAGEIA_BUGZILLA_COOKIE_FILE = '.gitzilla-cookie' |
35 |
colin |
3354 |
|
36 |
|
|
|
37 |
|
|
git_multimail.FOOTER_TEMPLATE = """\ |
38 |
|
|
|
39 |
|
|
-- \n\ |
40 |
|
|
Mageia Git Monkeys. |
41 |
|
|
""" |
42 |
|
|
git_multimail.REVISION_FOOTER_TEMPLATE = git_multimail.FOOTER_TEMPLATE |
43 |
|
|
|
44 |
colin |
3412 |
REPO_NAME_RE = re.compile(r'^/git/(?P<name>.+?)(?:\.git)?$') |
45 |
|
|
def repo_shortname(): |
46 |
|
|
basename = os.path.abspath(git_multimail.get_git_dir()) |
47 |
|
|
m = REPO_NAME_RE.match(basename) |
48 |
|
|
if m: |
49 |
|
|
return m.group('name') |
50 |
|
|
else: |
51 |
|
|
return basename |
52 |
|
|
|
53 |
|
|
|
54 |
colin |
3354 |
# Override the Environment class to generate an apporpriate short name which is |
55 |
|
|
# used in git links and as an email prefix |
56 |
|
|
class LinksEnvironment(git_multimail.Environment): |
57 |
|
|
def get_repo_shortname(self): |
58 |
colin |
3412 |
return repo_shortname() |
59 |
colin |
3354 |
git_multimail.Environment = LinksEnvironment |
60 |
|
|
|
61 |
|
|
# Override the Reviesion class to inject gitweb/cgit links and any referenced |
62 |
|
|
# bug URLs |
63 |
|
|
class LinksRevision(git_multimail.Revision): |
64 |
colin |
3380 |
bz = None |
65 |
|
|
|
66 |
|
|
def bugzilla_init(self): |
67 |
|
|
if self.bz is None: |
68 |
|
|
cookie_file = os.path.join(os.environ['HOME'], MAGEIA_BUGZILLA_COOKIE_FILE) |
69 |
|
|
self.cookiejar = LWPCookieJar(cookie_file) |
70 |
|
|
try: |
71 |
|
|
self.cookiejar.load() |
72 |
|
|
except IOError: |
73 |
|
|
pass |
74 |
|
|
|
75 |
|
|
self.bz = BugzillaProxy(MAGEIA_BUGZILLA_URL, cookiejar=self.cookiejar) |
76 |
|
|
return self.bz |
77 |
|
|
|
78 |
|
|
def bugzilla_login(self): |
79 |
|
|
params = { |
80 |
|
|
'login': 'bot', |
81 |
|
|
'password': open(os.path.join(os.environ['HOME'], MAGEIA_BUGZILLA_PASSWORD_FILE), 'r').readline().rstrip(), |
82 |
|
|
'remember': True |
83 |
|
|
} |
84 |
|
|
self.bz.User.login(params) |
85 |
|
|
self.cookiejar.save() |
86 |
|
|
os.chmod(self.cookiejar.filename, 0600) |
87 |
colin |
3387 |
|
88 |
colin |
3380 |
def bugzilla_call(self, method, *args): |
89 |
|
|
"""Attempt to call method with args. Log in if authentication is required. |
90 |
|
|
""" |
91 |
|
|
try: |
92 |
|
|
return method(*args) |
93 |
|
|
except xmlrpclib.Fault, fault: |
94 |
|
|
# Fault code 410 means login required |
95 |
|
|
if fault.faultCode == 410: |
96 |
|
|
self.bugzilla_login() |
97 |
|
|
return method(*args) |
98 |
|
|
raise |
99 |
|
|
|
100 |
colin |
3354 |
def generate_email_body(self, push): |
101 |
|
|
"""Show this revision.""" |
102 |
|
|
|
103 |
|
|
output = git_multimail.read_git_lines( |
104 |
|
|
['log'] + self.environment.commitlogopts + ['-1', self.rev.sha1], |
105 |
|
|
keepends=True, |
106 |
|
|
) |
107 |
|
|
bugs = {} |
108 |
|
|
commit = None |
109 |
|
|
idx = 0 |
110 |
|
|
for line in output: |
111 |
colin |
3387 |
idx += 1 |
112 |
colin |
3354 |
if line == "---\n": |
113 |
|
|
if commit and COMMIT_REPLACE: |
114 |
|
|
output.insert(idx, "\n") |
115 |
|
|
output.insert(idx, " %s\n" % (COMMIT_REPLACE % (self.environment.get_repo_shortname(), commit))) |
116 |
|
|
output.insert(idx, " Commit Link:\n") |
117 |
colin |
3387 |
idx += 3 |
118 |
colin |
3354 |
if bugs: |
119 |
|
|
output.insert(idx, " Bug links:\n") |
120 |
colin |
3387 |
idx += 1 |
121 |
|
|
for tracker, bugnos in bugs.items(): |
122 |
colin |
3354 |
output.insert(idx, " %s\n" % tracker) |
123 |
colin |
3387 |
idx += 1 |
124 |
colin |
3354 |
for bugno in bugnos: |
125 |
|
|
output.insert(idx, " %s\n" % (BUG_REFS[tracker]['replace'] % bugno)) |
126 |
colin |
3387 |
idx += 1 |
127 |
colin |
3354 |
output.insert(idx, "\n") |
128 |
colin |
3387 |
idx += 1 |
129 |
colin |
3380 |
|
130 |
|
|
# Attempt to modify bugzilla |
131 |
|
|
if "Mageia" in bugs: |
132 |
|
|
try: |
133 |
|
|
bz = self.bugzilla_init() |
134 |
|
|
|
135 |
|
|
# Mask email address |
136 |
colin |
3382 |
comment = None |
137 |
|
|
# Suppress the "Bug links:" section if only one bug |
138 |
|
|
# is referenced |
139 |
|
|
if len(bugs) == 1 and len(bugs['Mageia']) == 1: |
140 |
|
|
comment = output[0:idx-4] |
141 |
|
|
else: |
142 |
|
|
comment = output[0:idx] |
143 |
colin |
3380 |
comment[1] = re.sub(r'^(Author: [^@]*)@.*(>)?', r'\1@...>', comment[1]) |
144 |
|
|
comment = "".join(comment) |
145 |
|
|
|
146 |
|
|
params = {} |
147 |
|
|
params['ids'] = bugs['Mageia'] |
148 |
|
|
params['comment'] = { 'body': comment } |
149 |
|
|
self.bugzilla_call(bz.Bug.update, params) |
150 |
|
|
print "Updated bugzilla bugs: %s" % ", ".join(bugs['Mageia']) |
151 |
|
|
except: |
152 |
|
|
print "Unable to post to bugzilla bugs: %s :(" % ", ".join(bugs['Mageia']) |
153 |
|
|
pass |
154 |
|
|
|
155 |
colin |
3354 |
break |
156 |
|
|
m = COMMIT_RE.search(line) |
157 |
|
|
if m: |
158 |
colin |
3387 |
commit = m.group(1) |
159 |
colin |
3354 |
for tracker in BUG_REFS.keys(): |
160 |
colin |
3384 |
foundbugs = BUG_REFS[tracker]['re'].findall(line) |
161 |
|
|
if len(foundbugs): |
162 |
colin |
3354 |
if not tracker in bugs: |
163 |
colin |
3384 |
bugs[tracker] = foundbugs |
164 |
|
|
else: |
165 |
|
|
bugs[tracker] = list(set(bugs[tracker] + foundbugs)) |
166 |
colin |
3354 |
|
167 |
|
|
return output |
168 |
|
|
|
169 |
|
|
git_multimail.Revision = LinksRevision |
170 |
|
|
|
171 |
|
|
if __name__ == '__main__': |
172 |
colin |
3386 |
# Attempt to write a last-updated file for cgit cosmetics |
173 |
|
|
try: |
174 |
|
|
git_dir = git_multimail.get_git_dir() |
175 |
|
|
infowebdir = os.path.join(git_dir, 'info', 'web') |
176 |
|
|
if not os.path.exists(infowebdir): |
177 |
|
|
os.makedirs(infowebdir) |
178 |
|
|
lastupdated = git_multimail.read_git_output( |
179 |
colin |
3408 |
['for-each-ref', '--sort=-committerdate', "--format=%(committerdate:iso8601)", '--count=1', 'refs/heads'], |
180 |
colin |
3386 |
) |
181 |
colin |
3388 |
modfile = open(os.path.join(infowebdir, 'last-modified'), 'w') |
182 |
colin |
3408 |
modfile.write(lastupdated) |
183 |
colin |
3388 |
modfile.close() |
184 |
|
|
except Exception: |
185 |
colin |
3386 |
pass |
186 |
|
|
|
187 |
colin |
3412 |
try: |
188 |
colin |
3414 |
req = urllib2.Request('http://alamut.mageia.org:8000', repo_shortname() + '.git') |
189 |
colin |
3412 |
req.add_header('Content-Type', 'x-git/repo') |
190 |
|
|
fp = urllib2.urlopen(req) |
191 |
colin |
3417 |
if (fp): |
192 |
colin |
3412 |
fp.close() |
193 |
|
|
except Exception: |
194 |
|
|
pass |
195 |
|
|
|
196 |
colin |
3354 |
git_multimail.main(sys.argv[1:]) |