|
ds@1
|
1 |
# bugzilla.py - bugzilla integration for mercurial |
|
ds@1
|
2 |
# |
|
ds@1
|
3 |
# Copyright 2006 Vadim Gelfer <[email protected]> |
|
ds@1
|
4 |
# |
|
ds@1
|
5 |
# This software may be used and distributed according to the terms of the |
|
ds@1
|
6 |
# GNU General Public License version 2, incorporated herein by reference. |
|
ds@1
|
7 |
|
|
ds@1
|
8 |
'''hooks for integrating with the Bugzilla bug tracker |
|
ds@1
|
9 |
|
|
ds@1
|
10 |
This hook extension adds comments on bugs in Bugzilla when changesets |
|
ds@1
|
11 |
that refer to bugs by Bugzilla ID are seen. The hook does not change |
|
ds@1
|
12 |
bug status. |
|
ds@1
|
13 |
|
|
ds@1
|
14 |
The hook updates the Bugzilla database directly. Only Bugzilla |
|
ds@1
|
15 |
installations using MySQL are supported. |
|
ds@1
|
16 |
|
|
ds@1
|
17 |
The hook relies on a Bugzilla script to send bug change notification |
|
ds@1
|
18 |
emails. That script changes between Bugzilla versions; the |
|
ds@1
|
19 |
'processmail' script used prior to 2.18 is replaced in 2.18 and |
|
ds@1
|
20 |
subsequent versions by 'config/sendbugmail.pl'. Note that these will |
|
ds@1
|
21 |
be run by Mercurial as the user pushing the change; you will need to |
|
ds@1
|
22 |
ensure the Bugzilla install file permissions are set appropriately. |
|
ds@1
|
23 |
|
|
ds@1
|
24 |
Configuring the extension: |
|
ds@1
|
25 |
|
|
ds@1
|
26 |
[bugzilla] |
|
ds@1
|
27 |
|
|
ds@1
|
28 |
host Hostname of the MySQL server holding the Bugzilla |
|
ds@1
|
29 |
database. |
|
ds@1
|
30 |
db Name of the Bugzilla database in MySQL. Default 'bugs'. |
|
ds@1
|
31 |
user Username to use to access MySQL server. Default 'bugs'. |
|
ds@1
|
32 |
password Password to use to access MySQL server. |
|
ds@1
|
33 |
timeout Database connection timeout (seconds). Default 5. |
|
ds@1
|
34 |
version Bugzilla version. Specify '3.0' for Bugzilla versions |
|
ds@1
|
35 |
3.0 and later, '2.18' for Bugzilla versions from 2.18 |
|
ds@1
|
36 |
and '2.16' for versions prior to 2.18. |
|
ds@1
|
37 |
bzuser Fallback Bugzilla user name to record comments with, if |
|
ds@1
|
38 |
changeset committer cannot be found as a Bugzilla user. |
|
ds@1
|
39 |
bzdir Bugzilla install directory. Used by default notify. |
|
ds@1
|
40 |
Default '/var/www/html/bugzilla'. |
|
ds@1
|
41 |
notify The command to run to get Bugzilla to send bug change |
|
ds@1
|
42 |
notification emails. Substitutes from a map with 3 |
|
ds@1
|
43 |
keys, 'bzdir', 'id' (bug id) and 'user' (committer |
|
ds@1
|
44 |
bugzilla email). Default depends on version; from 2.18 |
|
ds@1
|
45 |
it is "cd %(bzdir)s && perl -T contrib/sendbugmail.pl |
|
ds@1
|
46 |
%(id)s %(user)s". |
|
ds@1
|
47 |
regexp Regular expression to match bug IDs in changeset commit |
|
ds@1
|
48 |
message. Must contain one "()" group. The default |
|
ds@1
|
49 |
expression matches 'Bug 1234', 'Bug no. 1234', 'Bug |
|
ds@1
|
50 |
number 1234', 'Bugs 1234,5678', 'Bug 1234 and 5678' and |
|
ds@1
|
51 |
variations thereof. Matching is case insensitive. |
|
ds@1
|
52 |
style The style file to use when formatting comments. |
|
ds@1
|
53 |
template Template to use when formatting comments. Overrides |
|
ds@1
|
54 |
style if specified. In addition to the usual Mercurial |
|
ds@1
|
55 |
keywords, the extension specifies: |
|
ds@1
|
56 |
{bug} The Bugzilla bug ID. |
|
ds@1
|
57 |
{root} The full pathname of the Mercurial |
|
ds@1
|
58 |
repository. |
|
ds@1
|
59 |
{webroot} Stripped pathname of the Mercurial |
|
ds@1
|
60 |
repository. |
|
ds@1
|
61 |
{hgweb} Base URL for browsing Mercurial |
|
ds@1
|
62 |
repositories. |
|
ds@1
|
63 |
Default 'changeset {node|short} in repo {root} refers ' |
|
ds@1
|
64 |
'to bug {bug}.\\ndetails:\\n\\t{desc|tabindent}' |
|
ds@1
|
65 |
strip The number of slashes to strip from the front of {root} |
|
ds@1
|
66 |
to produce {webroot}. Default 0. |
|
ds@1
|
67 |
usermap Path of file containing Mercurial committer ID to |
|
ds@1
|
68 |
Bugzilla user ID mappings. If specified, the file |
|
ds@1
|
69 |
should contain one mapping per line, |
|
ds@1
|
70 |
"committer"="Bugzilla user". See also the [usermap] |
|
ds@1
|
71 |
section. |
|
ds@1
|
72 |
|
|
ds@1
|
73 |
[usermap] |
|
ds@1
|
74 |
Any entries in this section specify mappings of Mercurial |
|
ds@1
|
75 |
committer ID to Bugzilla user ID. See also [bugzilla].usermap. |
|
ds@1
|
76 |
"committer"="Bugzilla user" |
|
ds@1
|
77 |
|
|
ds@1
|
78 |
[web] |
|
ds@1
|
79 |
baseurl Base URL for browsing Mercurial repositories. Reference |
|
ds@1
|
80 |
from templates as {hgweb}. |
|
ds@1
|
81 |
|
|
ds@1
|
82 |
Activating the extension: |
|
ds@1
|
83 |
|
|
ds@1
|
84 |
[extensions] |
|
ds@1
|
85 |
hgext.bugzilla = |
|
ds@1
|
86 |
|
|
ds@1
|
87 |
[hooks] |
|
ds@1
|
88 |
# run bugzilla hook on every change pulled or pushed in here |
|
ds@1
|
89 |
incoming.bugzilla = python:hgext.bugzilla.hook |
|
ds@1
|
90 |
|
|
ds@1
|
91 |
Example configuration: |
|
ds@1
|
92 |
|
|
ds@1
|
93 |
This example configuration is for a collection of Mercurial |
|
ds@1
|
94 |
repositories in /var/local/hg/repos/ used with a local Bugzilla 3.2 |
|
ds@1
|
95 |
installation in /opt/bugzilla-3.2. |
|
ds@1
|
96 |
|
|
ds@1
|
97 |
[bugzilla] |
|
ds@1
|
98 |
host=localhost |
|
ds@1
|
99 |
password=XYZZY |
|
ds@1
|
100 |
version=3.0 |
|
ds@1
|
101 |
[email protected] |
|
ds@1
|
102 |
bzdir=/opt/bugzilla-3.2 |
|
ds@1
|
103 |
template=Changeset {node|short} in {root|basename}.\\n{hgweb}/{webroot}/rev/{node|short}\\n\\n{desc}\\n |
|
ds@1
|
104 |
strip=5 |
|
ds@1
|
105 |
|
|
ds@1
|
106 |
[web] |
|
ds@1
|
107 |
baseurl=http://dev.domain.com/hg |
|
ds@1
|
108 |
|
|
ds@1
|
109 |
[usermap] |
|
ds@1
|
110 |
[email protected][email protected] |
|
ds@1
|
111 |
|
|
ds@1
|
112 |
Commits add a comment to the Bugzilla bug record of the form: |
|
ds@1
|
113 |
|
|
ds@1
|
114 |
Changeset 3b16791d6642 in repository-name. |
|
ds@1
|
115 |
http://dev.domain.com/hg/repository-name/rev/3b16791d6642 |
|
ds@1
|
116 |
|
|
ds@1
|
117 |
Changeset commit comment. Bug 1234. |
|
ds@1
|
118 |
''' |
|
ds@1
|
119 |
|
|
ds@1
|
120 |
from mercurial.i18n import _ |
|
ds@1
|
121 |
from mercurial.node import short |
|
ds@1
|
122 |
from mercurial import cmdutil, templater, util |
|
ds@2
|
123 |
import os, re, time |
|
ds@1
|
124 |
|
|
ds@1
|
125 |
MySQLdb = None |
|
ds@1
|
126 |
|
|
ds@1
|
127 |
def buglist(ids): |
|
ds@1
|
128 |
return '(' + ','.join(map(str, ids)) + ')' |
|
ds@1
|
129 |
|
|
ds@2
|
130 |
class bugzilla_comment(object): |
|
ds@2
|
131 |
def __init__(self, user, text): |
|
ds@2
|
132 |
self.user = user |
|
ds@2
|
133 |
self.text = text |
|
ds@2
|
134 |
|
|
ds@1
|
135 |
class bugzilla_2_16(object): |
|
ds@1
|
136 |
'''support for bugzilla version 2.16.''' |
|
ds@1
|
137 |
|
|
ds@1
|
138 |
def __init__(self, ui): |
|
ds@1
|
139 |
self.ui = ui |
|
ds@1
|
140 |
host = self.ui.config('bugzilla', 'host', 'localhost') |
|
ds@1
|
141 |
user = self.ui.config('bugzilla', 'user', 'bugs') |
|
ds@1
|
142 |
passwd = self.ui.config('bugzilla', 'password') |
|
ds@1
|
143 |
db = self.ui.config('bugzilla', 'db', 'bugs') |
|
ds@1
|
144 |
timeout = int(self.ui.config('bugzilla', 'timeout', 5)) |
|
ds@1
|
145 |
usermap = self.ui.config('bugzilla', 'usermap') |
|
ds@1
|
146 |
if usermap: |
|
ds@1
|
147 |
self.ui.readconfig(usermap, sections=['usermap']) |
|
ds@1
|
148 |
self.ui.note(_('connecting to %s:%s as %s, password %s\n') % |
|
ds@1
|
149 |
(host, db, user, '*' * len(passwd))) |
|
ds@1
|
150 |
self.conn = MySQLdb.connect(host=host, user=user, passwd=passwd, |
|
ds@1
|
151 |
db=db, connect_timeout=timeout) |
|
ds@1
|
152 |
self.cursor = self.conn.cursor() |
|
ds@1
|
153 |
self.longdesc_id = self.get_longdesc_id() |
|
ds@1
|
154 |
self.user_ids = {} |
|
ds@2
|
155 |
self.bugs = {} |
|
ds@1
|
156 |
self.default_notify = "cd %(bzdir)s && ./processmail %(id)s %(user)s" |
|
ds@1
|
157 |
|
|
ds@1
|
158 |
def run(self, *args, **kwargs): |
|
ds@1
|
159 |
'''run a query.''' |
|
ds@1
|
160 |
self.ui.note(_('query: %s %s\n') % (args, kwargs)) |
|
ds@1
|
161 |
try: |
|
ds@1
|
162 |
self.cursor.execute(*args, **kwargs) |
|
ds@1
|
163 |
except MySQLdb.MySQLError: |
|
ds@1
|
164 |
self.ui.note(_('failed query: %s %s\n') % (args, kwargs)) |
|
ds@1
|
165 |
raise |
|
ds@1
|
166 |
|
|
ds@1
|
167 |
def get_longdesc_id(self): |
|
ds@1
|
168 |
'''get identity of longdesc field''' |
|
ds@1
|
169 |
self.run('select fieldid from fielddefs where name = "longdesc"') |
|
ds@1
|
170 |
ids = self.cursor.fetchall() |
|
ds@1
|
171 |
if len(ids) != 1: |
|
ds@1
|
172 |
raise util.Abort(_('unknown database schema')) |
|
ds@1
|
173 |
return ids[0][0] |
|
ds@1
|
174 |
|
|
ds@1
|
175 |
def filter_real_bug_ids(self, ids): |
|
ds@1
|
176 |
'''filter not-existing bug ids from list.''' |
|
ds@1
|
177 |
self.run('select bug_id from bugs where bug_id in %s' % buglist(ids)) |
|
ds@1
|
178 |
return sorted([c[0] for c in self.cursor.fetchall()]) |
|
ds@1
|
179 |
|
|
ds@2
|
180 |
def filter_unknown_bug_ids(self, repo, node, ids): |
|
ds@1
|
181 |
'''filter bug ids from list that already refer to this changeset.''' |
|
ds@1
|
182 |
|
|
ds@2
|
183 |
reponame = repo.root |
|
ds@2
|
184 |
idx = reponame.rfind(os.path.sep) |
|
ds@2
|
185 |
if idx != -1: |
|
ds@2
|
186 |
reponame = reponame[idx+1:] |
|
ds@1
|
187 |
self.run('''select bug_id from longdescs where |
|
ds@2
|
188 |
bug_id in %s and thetext like "%%%s%%" and thetext like "%%%s%%"''' % |
|
ds@2
|
189 |
(buglist(ids), short(node), reponame)) |
|
ds@1
|
190 |
unknown = set(ids) |
|
ds@1
|
191 |
for (id,) in self.cursor.fetchall(): |
|
ds@1
|
192 |
self.ui.status(_('bug %d already knows about changeset %s\n') % |
|
ds@1
|
193 |
(id, short(node))) |
|
ds@1
|
194 |
unknown.discard(id) |
|
ds@1
|
195 |
return sorted(unknown) |
|
ds@1
|
196 |
|
|
ds@2
|
197 |
def notify(self): |
|
ds@1
|
198 |
'''tell bugzilla to send mail.''' |
|
ds@1
|
199 |
|
|
ds@1
|
200 |
self.ui.status(_('telling bugzilla to send mail:\n')) |
|
ds@2
|
201 |
for id, comments in self.bugs.iteritems(): |
|
ds@2
|
202 |
if len(comments) == 1: |
|
ds@2
|
203 |
(user, userid) = self.get_bugzilla_user(comments[0].user) |
|
ds@2
|
204 |
else: |
|
ds@2
|
205 |
(user, userid) = self.get_bugzilla_user('\0') |
|
ds@1
|
206 |
self.ui.status(_(' bug %s\n') % id) |
|
ds@1
|
207 |
cmdfmt = self.ui.config('bugzilla', 'notify', self.default_notify) |
|
ds@1
|
208 |
bzdir = self.ui.config('bugzilla', 'bzdir', '/var/www/html/bugzilla') |
|
ds@1
|
209 |
try: |
|
ds@1
|
210 |
# Backwards-compatible with old notify string, which |
|
ds@1
|
211 |
# took one string. This will throw with a new format |
|
ds@1
|
212 |
# string. |
|
ds@1
|
213 |
cmd = cmdfmt % id |
|
ds@1
|
214 |
except TypeError: |
|
ds@1
|
215 |
cmd = cmdfmt % {'bzdir': bzdir, 'id': id, 'user': user} |
|
ds@1
|
216 |
self.ui.note(_('running notify command %s\n') % cmd) |
|
ds@1
|
217 |
fp = util.popen('(%s) 2>&1' % cmd) |
|
ds@1
|
218 |
out = fp.read() |
|
ds@1
|
219 |
ret = fp.close() |
|
ds@1
|
220 |
if ret: |
|
ds@1
|
221 |
self.ui.warn(out) |
|
ds@1
|
222 |
raise util.Abort(_('bugzilla notify command %s') % |
|
ds@1
|
223 |
util.explain_exit(ret)[0]) |
|
ds@1
|
224 |
self.ui.status(_('done\n')) |
|
ds@1
|
225 |
|
|
ds@1
|
226 |
def get_user_id(self, user): |
|
ds@1
|
227 |
'''look up numeric bugzilla user id.''' |
|
ds@1
|
228 |
try: |
|
ds@1
|
229 |
return self.user_ids[user] |
|
ds@1
|
230 |
except KeyError: |
|
ds@1
|
231 |
try: |
|
ds@1
|
232 |
userid = int(user) |
|
ds@1
|
233 |
except ValueError: |
|
ds@1
|
234 |
self.ui.note(_('looking up user %s\n') % user) |
|
ds@1
|
235 |
self.run('''select userid from profiles |
|
ds@1
|
236 |
where login_name like %s''', user) |
|
ds@1
|
237 |
all = self.cursor.fetchall() |
|
ds@1
|
238 |
if len(all) != 1: |
|
ds@1
|
239 |
raise KeyError(user) |
|
ds@1
|
240 |
userid = int(all[0][0]) |
|
ds@1
|
241 |
self.user_ids[user] = userid |
|
ds@1
|
242 |
return userid |
|
ds@1
|
243 |
|
|
ds@1
|
244 |
def map_committer(self, user): |
|
ds@1
|
245 |
'''map name of committer to bugzilla user name.''' |
|
ds@1
|
246 |
for committer, bzuser in self.ui.configitems('usermap'): |
|
ds@1
|
247 |
if committer.lower() == user.lower(): |
|
ds@1
|
248 |
return bzuser |
|
ds@1
|
249 |
return user |
|
ds@1
|
250 |
|
|
ds@1
|
251 |
def get_bugzilla_user(self, committer): |
|
ds@1
|
252 |
'''see if committer is a registered bugzilla user. Return |
|
ds@1
|
253 |
bugzilla username and userid if so. If not, return default |
|
ds@1
|
254 |
bugzilla username and userid.''' |
|
ds@1
|
255 |
user = self.map_committer(committer) |
|
ds@1
|
256 |
try: |
|
ds@1
|
257 |
userid = self.get_user_id(user) |
|
ds@1
|
258 |
except KeyError: |
|
ds@1
|
259 |
try: |
|
ds@1
|
260 |
defaultuser = self.ui.config('bugzilla', 'bzuser') |
|
ds@1
|
261 |
if not defaultuser: |
|
ds@1
|
262 |
raise util.Abort(_('cannot find bugzilla user id for %s') % |
|
ds@1
|
263 |
user) |
|
ds@1
|
264 |
userid = self.get_user_id(defaultuser) |
|
ds@1
|
265 |
user = defaultuser |
|
ds@1
|
266 |
except KeyError: |
|
ds@1
|
267 |
raise util.Abort(_('cannot find bugzilla user id for %s or %s') % |
|
ds@1
|
268 |
(user, defaultuser)) |
|
ds@1
|
269 |
return (user, userid) |
|
ds@1
|
270 |
|
|
ds@1
|
271 |
def add_comment(self, bugid, text, committer): |
|
ds@1
|
272 |
'''add comment to bug. try adding comment as committer of |
|
ds@1
|
273 |
changeset, otherwise as default bugzilla user.''' |
|
ds@1
|
274 |
(user, userid) = self.get_bugzilla_user(committer) |
|
ds@1
|
275 |
now = time.strftime('%Y-%m-%d %H:%M:%S') |
|
ds@1
|
276 |
self.run('''insert into longdescs |
|
ds@1
|
277 |
(bug_id, who, bug_when, thetext) |
|
ds@1
|
278 |
values (%s, %s, %s, %s)''', |
|
ds@1
|
279 |
(bugid, userid, now, text)) |
|
ds@1
|
280 |
self.run('''insert into bugs_activity (bug_id, who, bug_when, fieldid) |
|
ds@1
|
281 |
values (%s, %s, %s, %s)''', |
|
ds@1
|
282 |
(bugid, userid, now, self.longdesc_id)) |
|
ds@1
|
283 |
self.conn.commit() |
|
ds@1
|
284 |
|
|
ds@1
|
285 |
class bugzilla_2_18(bugzilla_2_16): |
|
ds@1
|
286 |
'''support for bugzilla 2.18 series.''' |
|
ds@1
|
287 |
|
|
ds@1
|
288 |
def __init__(self, ui): |
|
ds@1
|
289 |
bugzilla_2_16.__init__(self, ui) |
|
ds@1
|
290 |
self.default_notify = "cd %(bzdir)s && perl -T contrib/sendbugmail.pl %(id)s %(user)s" |
|
ds@1
|
291 |
|
|
ds@1
|
292 |
class bugzilla_3_0(bugzilla_2_18): |
|
ds@1
|
293 |
'''support for bugzilla 3.0 series.''' |
|
ds@1
|
294 |
|
|
ds@1
|
295 |
def __init__(self, ui): |
|
ds@1
|
296 |
bugzilla_2_18.__init__(self, ui) |
|
ds@1
|
297 |
|
|
ds@1
|
298 |
def get_longdesc_id(self): |
|
ds@1
|
299 |
'''get identity of longdesc field''' |
|
ds@1
|
300 |
self.run('select id from fielddefs where name = "longdesc"') |
|
ds@1
|
301 |
ids = self.cursor.fetchall() |
|
ds@1
|
302 |
if len(ids) != 1: |
|
ds@1
|
303 |
raise util.Abort(_('unknown database schema')) |
|
ds@1
|
304 |
return ids[0][0] |
|
ds@1
|
305 |
|
|
ds@1
|
306 |
class bugzilla(object): |
|
ds@1
|
307 |
# supported versions of bugzilla. different versions have |
|
ds@1
|
308 |
# different schemas. |
|
ds@1
|
309 |
_versions = { |
|
ds@1
|
310 |
'2.16': bugzilla_2_16, |
|
ds@1
|
311 |
'2.18': bugzilla_2_18, |
|
ds@1
|
312 |
'3.0': bugzilla_3_0 |
|
ds@1
|
313 |
} |
|
ds@1
|
314 |
|
|
ds@1
|
315 |
_default_bug_re = (r'bugs?\s*,?\s*(?:#|nos?\.?|num(?:ber)?s?)?\s*' |
|
ds@1
|
316 |
r'((?:\d+\s*(?:,?\s*(?:and)?)?\s*)+)') |
|
ds@1
|
317 |
|
|
ds@1
|
318 |
_bz = None |
|
ds@1
|
319 |
|
|
ds@1
|
320 |
def __init__(self, ui, repo): |
|
ds@1
|
321 |
self.ui = ui |
|
ds@1
|
322 |
self.repo = repo |
|
ds@1
|
323 |
|
|
ds@1
|
324 |
def bz(self): |
|
ds@1
|
325 |
'''return object that knows how to talk to bugzilla version in |
|
ds@1
|
326 |
use.''' |
|
ds@1
|
327 |
|
|
ds@1
|
328 |
if bugzilla._bz is None: |
|
ds@1
|
329 |
bzversion = self.ui.config('bugzilla', 'version') |
|
ds@1
|
330 |
try: |
|
ds@1
|
331 |
bzclass = bugzilla._versions[bzversion] |
|
ds@1
|
332 |
except KeyError: |
|
ds@1
|
333 |
raise util.Abort(_('bugzilla version %s not supported') % |
|
ds@1
|
334 |
bzversion) |
|
ds@1
|
335 |
bugzilla._bz = bzclass(self.ui) |
|
ds@1
|
336 |
return bugzilla._bz |
|
ds@1
|
337 |
|
|
ds@1
|
338 |
def __getattr__(self, key): |
|
ds@1
|
339 |
return getattr(self.bz(), key) |
|
ds@1
|
340 |
|
|
ds@1
|
341 |
_bug_re = None |
|
ds@1
|
342 |
_split_re = None |
|
ds@1
|
343 |
|
|
ds@1
|
344 |
def find_bug_ids(self, ctx): |
|
ds@1
|
345 |
'''find valid bug ids that are referred to in changeset |
|
ds@1
|
346 |
comments and that do not already have references to this |
|
ds@1
|
347 |
changeset.''' |
|
ds@1
|
348 |
|
|
ds@1
|
349 |
if bugzilla._bug_re is None: |
|
ds@1
|
350 |
bugzilla._bug_re = re.compile( |
|
ds@1
|
351 |
self.ui.config('bugzilla', 'regexp', bugzilla._default_bug_re), |
|
ds@1
|
352 |
re.IGNORECASE) |
|
ds@1
|
353 |
bugzilla._split_re = re.compile(r'\D+') |
|
ds@1
|
354 |
start = 0 |
|
ds@1
|
355 |
ids = set() |
|
ds@1
|
356 |
while True: |
|
ds@1
|
357 |
m = bugzilla._bug_re.search(ctx.description(), start) |
|
ds@1
|
358 |
if not m: |
|
ds@1
|
359 |
break |
|
ds@1
|
360 |
start = m.end() |
|
ds@1
|
361 |
for id in bugzilla._split_re.split(m.group(1)): |
|
ds@1
|
362 |
if not id: continue |
|
ds@1
|
363 |
ids.add(int(id)) |
|
ds@1
|
364 |
if ids: |
|
ds@1
|
365 |
ids = self.filter_real_bug_ids(ids) |
|
ds@1
|
366 |
if ids: |
|
ds@2
|
367 |
ids = self.filter_unknown_bug_ids(self.repo, ctx.node(), ids) |
|
ds@1
|
368 |
return ids |
|
ds@1
|
369 |
|
|
ds@2
|
370 |
def build_comment(self, bugid, ctx): |
|
ds@2
|
371 |
'''format a bugzilla comment with reference to changeset.''' |
|
ds@1
|
372 |
|
|
ds@1
|
373 |
def webroot(root): |
|
ds@1
|
374 |
'''strip leading prefix of repo root and turn into |
|
ds@1
|
375 |
url-safe path.''' |
|
ds@1
|
376 |
count = int(self.ui.config('bugzilla', 'strip', 0)) |
|
ds@1
|
377 |
root = util.pconvert(root) |
|
ds@1
|
378 |
while count > 0: |
|
ds@1
|
379 |
c = root.find('/') |
|
ds@1
|
380 |
if c == -1: |
|
ds@1
|
381 |
break |
|
ds@1
|
382 |
root = root[c+1:] |
|
ds@1
|
383 |
count -= 1 |
|
ds@1
|
384 |
return root |
|
ds@1
|
385 |
|
|
ds@1
|
386 |
mapfile = self.ui.config('bugzilla', 'style') |
|
ds@1
|
387 |
tmpl = self.ui.config('bugzilla', 'template') |
|
ds@1
|
388 |
t = cmdutil.changeset_templater(self.ui, self.repo, |
|
ds@1
|
389 |
False, None, mapfile, False) |
|
ds@1
|
390 |
if not mapfile and not tmpl: |
|
ds@1
|
391 |
tmpl = _('changeset {node|short} in repo {root} refers ' |
|
ds@1
|
392 |
'to bug {bug}.\ndetails:\n\t{desc|tabindent}') |
|
ds@1
|
393 |
if tmpl: |
|
ds@1
|
394 |
tmpl = templater.parsestring(tmpl, quoted=False) |
|
ds@1
|
395 |
t.use_template(tmpl) |
|
ds@1
|
396 |
self.ui.pushbuffer() |
|
ds@1
|
397 |
t.show(ctx, changes=ctx.changeset(), |
|
ds@1
|
398 |
bug=str(bugid), |
|
ds@1
|
399 |
hgweb=self.ui.config('web', 'baseurl'), |
|
ds@1
|
400 |
root=self.repo.root, |
|
ds@1
|
401 |
webroot=webroot(self.repo.root)) |
|
ds@1
|
402 |
data = self.ui.popbuffer() |
|
ds@2
|
403 |
return data |
|
ds@2
|
404 |
|
|
ds@2
|
405 |
def add_update(self, bugid, ctx): |
|
ds@2
|
406 |
text = self.build_comment(bugid, ctx) |
|
ds@2
|
407 |
user = util.email(ctx.user()) |
|
ds@2
|
408 |
if not self.bugs.has_key(bugid): |
|
ds@2
|
409 |
self.bugs[bugid] = [bugzilla_comment(user, text)] |
|
ds@2
|
410 |
else: |
|
ds@2
|
411 |
comments = self.bugs[bugid] |
|
ds@2
|
412 |
for comment in comments: |
|
ds@2
|
413 |
if comment.user == user: |
|
ds@2
|
414 |
idx = comments.index(comment) |
|
ds@2
|
415 |
self.bugs[bugid][idx].text += '\n\n' + text |
|
ds@2
|
416 |
return |
|
ds@2
|
417 |
self.bugs[bugid].append(bugzilla_comment(user, text)) |
|
ds@2
|
418 |
|
|
ds@2
|
419 |
def update(self): |
|
ds@2
|
420 |
for bugid, comments in self.bugs.iteritems(): |
|
ds@2
|
421 |
for comment in comments: |
|
ds@2
|
422 |
self.add_comment(bugid, comment.text, comment.user) |
|
ds@1
|
423 |
|
|
ds@1
|
424 |
def hook(ui, repo, hooktype, node=None, **kwargs): |
|
ds@1
|
425 |
'''add comment to bugzilla for each changeset that refers to a |
|
ds@1
|
426 |
bugzilla bug id. only add a comment once per bug, so same change |
|
ds@1
|
427 |
seen multiple times does not fill bug with duplicate data.''' |
|
ds@1
|
428 |
try: |
|
ds@1
|
429 |
import MySQLdb as mysql |
|
ds@1
|
430 |
global MySQLdb |
|
ds@1
|
431 |
MySQLdb = mysql |
|
ds@1
|
432 |
except ImportError, err: |
|
ds@1
|
433 |
raise util.Abort(_('python mysql support not available: %s') % err) |
|
ds@1
|
434 |
|
|
ds@1
|
435 |
if node is None: |
|
ds@1
|
436 |
raise util.Abort(_('hook type %s does not pass a changeset id') % |
|
ds@1
|
437 |
hooktype) |
|
ds@1
|
438 |
try: |
|
ds@1
|
439 |
bz = bugzilla(ui, repo) |
|
ds@2
|
440 |
if hooktype == 'incoming': |
|
ds@2
|
441 |
ctx = repo[node] |
|
ds@2
|
442 |
ids = bz.find_bug_ids(ctx) |
|
ds@2
|
443 |
if ids: |
|
ds@2
|
444 |
for id in ids: |
|
ds@2
|
445 |
bz.add_update(id, ctx) |
|
ds@2
|
446 |
bz.update() |
|
ds@2
|
447 |
bz.notify() |
|
ds@2
|
448 |
elif hooktype == 'changegroup': |
|
ds@2
|
449 |
start = repo[node].rev() |
|
ds@2
|
450 |
end = len(repo) |
|
ds@2
|
451 |
for rev in xrange(start, end): |
|
ds@2
|
452 |
node = repo.changelog.node(rev) |
|
ds@2
|
453 |
ctx = repo[node] |
|
ds@2
|
454 |
ids = bz.find_bug_ids(ctx) |
|
ds@2
|
455 |
if ids: |
|
ds@2
|
456 |
for id in ids: |
|
ds@2
|
457 |
bz.add_update(id, ctx) |
|
ds@2
|
458 |
bz.update() |
|
ds@2
|
459 |
bz.notify() |
|
ds@2
|
460 |
else: |
|
ds@2
|
461 |
raise util.Abort(_('hook type %s not supported') % hooktype) |
|
ds@1
|
462 |
except MySQLdb.MySQLError, err: |
|
ds@1
|
463 |
raise util.Abort(_('database error: %s') % err[1]) |
|
ds@1
|
464 |
|