1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/bugzilla.py Mon Oct 26 01:02:28 2009 -0500
1.3 @@ -0,0 +1,416 @@
1.4 +# bugzilla.py - bugzilla integration for mercurial
1.5 +#
1.6 +# Copyright 2006 Vadim Gelfer <[email protected]>
1.7 +#
1.8 +# This software may be used and distributed according to the terms of the
1.9 +# GNU General Public License version 2, incorporated herein by reference.
1.10 +
1.11 +'''hooks for integrating with the Bugzilla bug tracker
1.12 +
1.13 +This hook extension adds comments on bugs in Bugzilla when changesets
1.14 +that refer to bugs by Bugzilla ID are seen. The hook does not change
1.15 +bug status.
1.16 +
1.17 +The hook updates the Bugzilla database directly. Only Bugzilla
1.18 +installations using MySQL are supported.
1.19 +
1.20 +The hook relies on a Bugzilla script to send bug change notification
1.21 +emails. That script changes between Bugzilla versions; the
1.22 +'processmail' script used prior to 2.18 is replaced in 2.18 and
1.23 +subsequent versions by 'config/sendbugmail.pl'. Note that these will
1.24 +be run by Mercurial as the user pushing the change; you will need to
1.25 +ensure the Bugzilla install file permissions are set appropriately.
1.26 +
1.27 +Configuring the extension:
1.28 +
1.29 + [bugzilla]
1.30 +
1.31 + host Hostname of the MySQL server holding the Bugzilla
1.32 + database.
1.33 + db Name of the Bugzilla database in MySQL. Default 'bugs'.
1.34 + user Username to use to access MySQL server. Default 'bugs'.
1.35 + password Password to use to access MySQL server.
1.36 + timeout Database connection timeout (seconds). Default 5.
1.37 + version Bugzilla version. Specify '3.0' for Bugzilla versions
1.38 + 3.0 and later, '2.18' for Bugzilla versions from 2.18
1.39 + and '2.16' for versions prior to 2.18.
1.40 + bzuser Fallback Bugzilla user name to record comments with, if
1.41 + changeset committer cannot be found as a Bugzilla user.
1.42 + bzdir Bugzilla install directory. Used by default notify.
1.43 + Default '/var/www/html/bugzilla'.
1.44 + notify The command to run to get Bugzilla to send bug change
1.45 + notification emails. Substitutes from a map with 3
1.46 + keys, 'bzdir', 'id' (bug id) and 'user' (committer
1.47 + bugzilla email). Default depends on version; from 2.18
1.48 + it is "cd %(bzdir)s && perl -T contrib/sendbugmail.pl
1.49 + %(id)s %(user)s".
1.50 + regexp Regular expression to match bug IDs in changeset commit
1.51 + message. Must contain one "()" group. The default
1.52 + expression matches 'Bug 1234', 'Bug no. 1234', 'Bug
1.53 + number 1234', 'Bugs 1234,5678', 'Bug 1234 and 5678' and
1.54 + variations thereof. Matching is case insensitive.
1.55 + style The style file to use when formatting comments.
1.56 + template Template to use when formatting comments. Overrides
1.57 + style if specified. In addition to the usual Mercurial
1.58 + keywords, the extension specifies:
1.59 + {bug} The Bugzilla bug ID.
1.60 + {root} The full pathname of the Mercurial
1.61 + repository.
1.62 + {webroot} Stripped pathname of the Mercurial
1.63 + repository.
1.64 + {hgweb} Base URL for browsing Mercurial
1.65 + repositories.
1.66 + Default 'changeset {node|short} in repo {root} refers '
1.67 + 'to bug {bug}.\\ndetails:\\n\\t{desc|tabindent}'
1.68 + strip The number of slashes to strip from the front of {root}
1.69 + to produce {webroot}. Default 0.
1.70 + usermap Path of file containing Mercurial committer ID to
1.71 + Bugzilla user ID mappings. If specified, the file
1.72 + should contain one mapping per line,
1.73 + "committer"="Bugzilla user". See also the [usermap]
1.74 + section.
1.75 +
1.76 + [usermap]
1.77 + Any entries in this section specify mappings of Mercurial
1.78 + committer ID to Bugzilla user ID. See also [bugzilla].usermap.
1.79 + "committer"="Bugzilla user"
1.80 +
1.81 + [web]
1.82 + baseurl Base URL for browsing Mercurial repositories. Reference
1.83 + from templates as {hgweb}.
1.84 +
1.85 +Activating the extension:
1.86 +
1.87 + [extensions]
1.88 + hgext.bugzilla =
1.89 +
1.90 + [hooks]
1.91 + # run bugzilla hook on every change pulled or pushed in here
1.92 + incoming.bugzilla = python:hgext.bugzilla.hook
1.93 +
1.94 +Example configuration:
1.95 +
1.96 +This example configuration is for a collection of Mercurial
1.97 +repositories in /var/local/hg/repos/ used with a local Bugzilla 3.2
1.98 +installation in /opt/bugzilla-3.2.
1.99 +
1.100 + [bugzilla]
1.101 + host=localhost
1.102 + password=XYZZY
1.103 + version=3.0
1.104 + [email protected]
1.105 + bzdir=/opt/bugzilla-3.2
1.106 + template=Changeset {node|short} in {root|basename}.\\n{hgweb}/{webroot}/rev/{node|short}\\n\\n{desc}\\n
1.107 + strip=5
1.108 +
1.109 + [web]
1.110 + baseurl=http://dev.domain.com/hg
1.111 +
1.112 + [usermap]
1.113 + [email protected][email protected]
1.114 +
1.115 +Commits add a comment to the Bugzilla bug record of the form:
1.116 +
1.117 + Changeset 3b16791d6642 in repository-name.
1.118 + http://dev.domain.com/hg/repository-name/rev/3b16791d6642
1.119 +
1.120 + Changeset commit comment. Bug 1234.
1.121 +'''
1.122 +
1.123 +from mercurial.i18n import _
1.124 +from mercurial.node import short
1.125 +from mercurial import cmdutil, templater, util
1.126 +import re, time
1.127 +
1.128 +MySQLdb = None
1.129 +
1.130 +def buglist(ids):
1.131 + return '(' + ','.join(map(str, ids)) + ')'
1.132 +
1.133 +class bugzilla_2_16(object):
1.134 + '''support for bugzilla version 2.16.'''
1.135 +
1.136 + def __init__(self, ui):
1.137 + self.ui = ui
1.138 + host = self.ui.config('bugzilla', 'host', 'localhost')
1.139 + user = self.ui.config('bugzilla', 'user', 'bugs')
1.140 + passwd = self.ui.config('bugzilla', 'password')
1.141 + db = self.ui.config('bugzilla', 'db', 'bugs')
1.142 + timeout = int(self.ui.config('bugzilla', 'timeout', 5))
1.143 + usermap = self.ui.config('bugzilla', 'usermap')
1.144 + if usermap:
1.145 + self.ui.readconfig(usermap, sections=['usermap'])
1.146 + self.ui.note(_('connecting to %s:%s as %s, password %s\n') %
1.147 + (host, db, user, '*' * len(passwd)))
1.148 + self.conn = MySQLdb.connect(host=host, user=user, passwd=passwd,
1.149 + db=db, connect_timeout=timeout)
1.150 + self.cursor = self.conn.cursor()
1.151 + self.longdesc_id = self.get_longdesc_id()
1.152 + self.user_ids = {}
1.153 + self.default_notify = "cd %(bzdir)s && ./processmail %(id)s %(user)s"
1.154 +
1.155 + def run(self, *args, **kwargs):
1.156 + '''run a query.'''
1.157 + self.ui.note(_('query: %s %s\n') % (args, kwargs))
1.158 + try:
1.159 + self.cursor.execute(*args, **kwargs)
1.160 + except MySQLdb.MySQLError:
1.161 + self.ui.note(_('failed query: %s %s\n') % (args, kwargs))
1.162 + raise
1.163 +
1.164 + def get_longdesc_id(self):
1.165 + '''get identity of longdesc field'''
1.166 + self.run('select fieldid from fielddefs where name = "longdesc"')
1.167 + ids = self.cursor.fetchall()
1.168 + if len(ids) != 1:
1.169 + raise util.Abort(_('unknown database schema'))
1.170 + return ids[0][0]
1.171 +
1.172 + def filter_real_bug_ids(self, ids):
1.173 + '''filter not-existing bug ids from list.'''
1.174 + self.run('select bug_id from bugs where bug_id in %s' % buglist(ids))
1.175 + return sorted([c[0] for c in self.cursor.fetchall()])
1.176 +
1.177 + def filter_unknown_bug_ids(self, node, ids):
1.178 + '''filter bug ids from list that already refer to this changeset.'''
1.179 +
1.180 + self.run('''select bug_id from longdescs where
1.181 + bug_id in %s and thetext like "%%%s%%"''' %
1.182 + (buglist(ids), short(node)))
1.183 + unknown = set(ids)
1.184 + for (id,) in self.cursor.fetchall():
1.185 + self.ui.status(_('bug %d already knows about changeset %s\n') %
1.186 + (id, short(node)))
1.187 + unknown.discard(id)
1.188 + return sorted(unknown)
1.189 +
1.190 + def notify(self, ids, committer):
1.191 + '''tell bugzilla to send mail.'''
1.192 +
1.193 + self.ui.status(_('telling bugzilla to send mail:\n'))
1.194 + (user, userid) = self.get_bugzilla_user(committer)
1.195 + for id in ids:
1.196 + self.ui.status(_(' bug %s\n') % id)
1.197 + cmdfmt = self.ui.config('bugzilla', 'notify', self.default_notify)
1.198 + bzdir = self.ui.config('bugzilla', 'bzdir', '/var/www/html/bugzilla')
1.199 + try:
1.200 + # Backwards-compatible with old notify string, which
1.201 + # took one string. This will throw with a new format
1.202 + # string.
1.203 + cmd = cmdfmt % id
1.204 + except TypeError:
1.205 + cmd = cmdfmt % {'bzdir': bzdir, 'id': id, 'user': user}
1.206 + self.ui.note(_('running notify command %s\n') % cmd)
1.207 + fp = util.popen('(%s) 2>&1' % cmd)
1.208 + out = fp.read()
1.209 + ret = fp.close()
1.210 + if ret:
1.211 + self.ui.warn(out)
1.212 + raise util.Abort(_('bugzilla notify command %s') %
1.213 + util.explain_exit(ret)[0])
1.214 + self.ui.status(_('done\n'))
1.215 +
1.216 + def get_user_id(self, user):
1.217 + '''look up numeric bugzilla user id.'''
1.218 + try:
1.219 + return self.user_ids[user]
1.220 + except KeyError:
1.221 + try:
1.222 + userid = int(user)
1.223 + except ValueError:
1.224 + self.ui.note(_('looking up user %s\n') % user)
1.225 + self.run('''select userid from profiles
1.226 + where login_name like %s''', user)
1.227 + all = self.cursor.fetchall()
1.228 + if len(all) != 1:
1.229 + raise KeyError(user)
1.230 + userid = int(all[0][0])
1.231 + self.user_ids[user] = userid
1.232 + return userid
1.233 +
1.234 + def map_committer(self, user):
1.235 + '''map name of committer to bugzilla user name.'''
1.236 + for committer, bzuser in self.ui.configitems('usermap'):
1.237 + if committer.lower() == user.lower():
1.238 + return bzuser
1.239 + return user
1.240 +
1.241 + def get_bugzilla_user(self, committer):
1.242 + '''see if committer is a registered bugzilla user. Return
1.243 + bugzilla username and userid if so. If not, return default
1.244 + bugzilla username and userid.'''
1.245 + user = self.map_committer(committer)
1.246 + try:
1.247 + userid = self.get_user_id(user)
1.248 + except KeyError:
1.249 + try:
1.250 + defaultuser = self.ui.config('bugzilla', 'bzuser')
1.251 + if not defaultuser:
1.252 + raise util.Abort(_('cannot find bugzilla user id for %s') %
1.253 + user)
1.254 + userid = self.get_user_id(defaultuser)
1.255 + user = defaultuser
1.256 + except KeyError:
1.257 + raise util.Abort(_('cannot find bugzilla user id for %s or %s') %
1.258 + (user, defaultuser))
1.259 + return (user, userid)
1.260 +
1.261 + def add_comment(self, bugid, text, committer):
1.262 + '''add comment to bug. try adding comment as committer of
1.263 + changeset, otherwise as default bugzilla user.'''
1.264 + (user, userid) = self.get_bugzilla_user(committer)
1.265 + now = time.strftime('%Y-%m-%d %H:%M:%S')
1.266 + self.run('''insert into longdescs
1.267 + (bug_id, who, bug_when, thetext)
1.268 + values (%s, %s, %s, %s)''',
1.269 + (bugid, userid, now, text))
1.270 + self.run('''insert into bugs_activity (bug_id, who, bug_when, fieldid)
1.271 + values (%s, %s, %s, %s)''',
1.272 + (bugid, userid, now, self.longdesc_id))
1.273 + self.conn.commit()
1.274 +
1.275 +class bugzilla_2_18(bugzilla_2_16):
1.276 + '''support for bugzilla 2.18 series.'''
1.277 +
1.278 + def __init__(self, ui):
1.279 + bugzilla_2_16.__init__(self, ui)
1.280 + self.default_notify = "cd %(bzdir)s && perl -T contrib/sendbugmail.pl %(id)s %(user)s"
1.281 +
1.282 +class bugzilla_3_0(bugzilla_2_18):
1.283 + '''support for bugzilla 3.0 series.'''
1.284 +
1.285 + def __init__(self, ui):
1.286 + bugzilla_2_18.__init__(self, ui)
1.287 +
1.288 + def get_longdesc_id(self):
1.289 + '''get identity of longdesc field'''
1.290 + self.run('select id from fielddefs where name = "longdesc"')
1.291 + ids = self.cursor.fetchall()
1.292 + if len(ids) != 1:
1.293 + raise util.Abort(_('unknown database schema'))
1.294 + return ids[0][0]
1.295 +
1.296 +class bugzilla(object):
1.297 + # supported versions of bugzilla. different versions have
1.298 + # different schemas.
1.299 + _versions = {
1.300 + '2.16': bugzilla_2_16,
1.301 + '2.18': bugzilla_2_18,
1.302 + '3.0': bugzilla_3_0
1.303 + }
1.304 +
1.305 + _default_bug_re = (r'bugs?\s*,?\s*(?:#|nos?\.?|num(?:ber)?s?)?\s*'
1.306 + r'((?:\d+\s*(?:,?\s*(?:and)?)?\s*)+)')
1.307 +
1.308 + _bz = None
1.309 +
1.310 + def __init__(self, ui, repo):
1.311 + self.ui = ui
1.312 + self.repo = repo
1.313 +
1.314 + def bz(self):
1.315 + '''return object that knows how to talk to bugzilla version in
1.316 + use.'''
1.317 +
1.318 + if bugzilla._bz is None:
1.319 + bzversion = self.ui.config('bugzilla', 'version')
1.320 + try:
1.321 + bzclass = bugzilla._versions[bzversion]
1.322 + except KeyError:
1.323 + raise util.Abort(_('bugzilla version %s not supported') %
1.324 + bzversion)
1.325 + bugzilla._bz = bzclass(self.ui)
1.326 + return bugzilla._bz
1.327 +
1.328 + def __getattr__(self, key):
1.329 + return getattr(self.bz(), key)
1.330 +
1.331 + _bug_re = None
1.332 + _split_re = None
1.333 +
1.334 + def find_bug_ids(self, ctx):
1.335 + '''find valid bug ids that are referred to in changeset
1.336 + comments and that do not already have references to this
1.337 + changeset.'''
1.338 +
1.339 + if bugzilla._bug_re is None:
1.340 + bugzilla._bug_re = re.compile(
1.341 + self.ui.config('bugzilla', 'regexp', bugzilla._default_bug_re),
1.342 + re.IGNORECASE)
1.343 + bugzilla._split_re = re.compile(r'\D+')
1.344 + start = 0
1.345 + ids = set()
1.346 + while True:
1.347 + m = bugzilla._bug_re.search(ctx.description(), start)
1.348 + if not m:
1.349 + break
1.350 + start = m.end()
1.351 + for id in bugzilla._split_re.split(m.group(1)):
1.352 + if not id: continue
1.353 + ids.add(int(id))
1.354 + if ids:
1.355 + ids = self.filter_real_bug_ids(ids)
1.356 + if ids:
1.357 + ids = self.filter_unknown_bug_ids(ctx.node(), ids)
1.358 + return ids
1.359 +
1.360 + def update(self, bugid, ctx):
1.361 + '''update bugzilla bug with reference to changeset.'''
1.362 +
1.363 + def webroot(root):
1.364 + '''strip leading prefix of repo root and turn into
1.365 + url-safe path.'''
1.366 + count = int(self.ui.config('bugzilla', 'strip', 0))
1.367 + root = util.pconvert(root)
1.368 + while count > 0:
1.369 + c = root.find('/')
1.370 + if c == -1:
1.371 + break
1.372 + root = root[c+1:]
1.373 + count -= 1
1.374 + return root
1.375 +
1.376 + mapfile = self.ui.config('bugzilla', 'style')
1.377 + tmpl = self.ui.config('bugzilla', 'template')
1.378 + t = cmdutil.changeset_templater(self.ui, self.repo,
1.379 + False, None, mapfile, False)
1.380 + if not mapfile and not tmpl:
1.381 + tmpl = _('changeset {node|short} in repo {root} refers '
1.382 + 'to bug {bug}.\ndetails:\n\t{desc|tabindent}')
1.383 + if tmpl:
1.384 + tmpl = templater.parsestring(tmpl, quoted=False)
1.385 + t.use_template(tmpl)
1.386 + self.ui.pushbuffer()
1.387 + t.show(ctx, changes=ctx.changeset(),
1.388 + bug=str(bugid),
1.389 + hgweb=self.ui.config('web', 'baseurl'),
1.390 + root=self.repo.root,
1.391 + webroot=webroot(self.repo.root))
1.392 + data = self.ui.popbuffer()
1.393 + self.add_comment(bugid, data, util.email(ctx.user()))
1.394 +
1.395 +def hook(ui, repo, hooktype, node=None, **kwargs):
1.396 + '''add comment to bugzilla for each changeset that refers to a
1.397 + bugzilla bug id. only add a comment once per bug, so same change
1.398 + seen multiple times does not fill bug with duplicate data.'''
1.399 + try:
1.400 + import MySQLdb as mysql
1.401 + global MySQLdb
1.402 + MySQLdb = mysql
1.403 + except ImportError, err:
1.404 + raise util.Abort(_('python mysql support not available: %s') % err)
1.405 +
1.406 + if node is None:
1.407 + raise util.Abort(_('hook type %s does not pass a changeset id') %
1.408 + hooktype)
1.409 + try:
1.410 + bz = bugzilla(ui, repo)
1.411 + ctx = repo[node]
1.412 + ids = bz.find_bug_ids(ctx)
1.413 + if ids:
1.414 + for id in ids:
1.415 + bz.update(id, ctx)
1.416 + bz.notify(ids, util.email(ctx.user()))
1.417 + except MySQLdb.MySQLError, err:
1.418 + raise util.Abort(_('database error: %s') % err[1])
1.419 +