Updated bugzilla extension to allow for changegroup hooks.
Updated bugzilla extension to allow for changegroup hooks.
1.1 --- a/bugzilla.py Mon Oct 26 01:02:28 2009 -0500
1.2 +++ b/bugzilla.py Mon Oct 26 01:32:09 2009 -0500
1.3 @@ -120,13 +120,18 @@
1.4 from mercurial.i18n import _
1.5 from mercurial.node import short
1.6 from mercurial import cmdutil, templater, util
1.7 -import re, time
1.8 +import os, re, time
1.9
1.10 MySQLdb = None
1.11
1.12 def buglist(ids):
1.13 return '(' + ','.join(map(str, ids)) + ')'
1.14
1.15 +class bugzilla_comment(object):
1.16 + def __init__(self, user, text):
1.17 + self.user = user
1.18 + self.text = text
1.19 +
1.20 class bugzilla_2_16(object):
1.21 '''support for bugzilla version 2.16.'''
1.22
1.23 @@ -147,6 +152,7 @@
1.24 self.cursor = self.conn.cursor()
1.25 self.longdesc_id = self.get_longdesc_id()
1.26 self.user_ids = {}
1.27 + self.bugs = {}
1.28 self.default_notify = "cd %(bzdir)s && ./processmail %(id)s %(user)s"
1.29
1.30 def run(self, *args, **kwargs):
1.31 @@ -171,12 +177,16 @@
1.32 self.run('select bug_id from bugs where bug_id in %s' % buglist(ids))
1.33 return sorted([c[0] for c in self.cursor.fetchall()])
1.34
1.35 - def filter_unknown_bug_ids(self, node, ids):
1.36 + def filter_unknown_bug_ids(self, repo, node, ids):
1.37 '''filter bug ids from list that already refer to this changeset.'''
1.38
1.39 + reponame = repo.root
1.40 + idx = reponame.rfind(os.path.sep)
1.41 + if idx != -1:
1.42 + reponame = reponame[idx+1:]
1.43 self.run('''select bug_id from longdescs where
1.44 - bug_id in %s and thetext like "%%%s%%"''' %
1.45 - (buglist(ids), short(node)))
1.46 + bug_id in %s and thetext like "%%%s%%" and thetext like "%%%s%%"''' %
1.47 + (buglist(ids), short(node), reponame))
1.48 unknown = set(ids)
1.49 for (id,) in self.cursor.fetchall():
1.50 self.ui.status(_('bug %d already knows about changeset %s\n') %
1.51 @@ -184,12 +194,15 @@
1.52 unknown.discard(id)
1.53 return sorted(unknown)
1.54
1.55 - def notify(self, ids, committer):
1.56 + def notify(self):
1.57 '''tell bugzilla to send mail.'''
1.58
1.59 self.ui.status(_('telling bugzilla to send mail:\n'))
1.60 - (user, userid) = self.get_bugzilla_user(committer)
1.61 - for id in ids:
1.62 + for id, comments in self.bugs.iteritems():
1.63 + if len(comments) == 1:
1.64 + (user, userid) = self.get_bugzilla_user(comments[0].user)
1.65 + else:
1.66 + (user, userid) = self.get_bugzilla_user('\0')
1.67 self.ui.status(_(' bug %s\n') % id)
1.68 cmdfmt = self.ui.config('bugzilla', 'notify', self.default_notify)
1.69 bzdir = self.ui.config('bugzilla', 'bzdir', '/var/www/html/bugzilla')
1.70 @@ -351,11 +364,11 @@
1.71 if ids:
1.72 ids = self.filter_real_bug_ids(ids)
1.73 if ids:
1.74 - ids = self.filter_unknown_bug_ids(ctx.node(), ids)
1.75 + ids = self.filter_unknown_bug_ids(self.repo, ctx.node(), ids)
1.76 return ids
1.77
1.78 - def update(self, bugid, ctx):
1.79 - '''update bugzilla bug with reference to changeset.'''
1.80 + def build_comment(self, bugid, ctx):
1.81 + '''format a bugzilla comment with reference to changeset.'''
1.82
1.83 def webroot(root):
1.84 '''strip leading prefix of repo root and turn into
1.85 @@ -387,7 +400,26 @@
1.86 root=self.repo.root,
1.87 webroot=webroot(self.repo.root))
1.88 data = self.ui.popbuffer()
1.89 - self.add_comment(bugid, data, util.email(ctx.user()))
1.90 + return data
1.91 +
1.92 + def add_update(self, bugid, ctx):
1.93 + text = self.build_comment(bugid, ctx)
1.94 + user = util.email(ctx.user())
1.95 + if not self.bugs.has_key(bugid):
1.96 + self.bugs[bugid] = [bugzilla_comment(user, text)]
1.97 + else:
1.98 + comments = self.bugs[bugid]
1.99 + for comment in comments:
1.100 + if comment.user == user:
1.101 + idx = comments.index(comment)
1.102 + self.bugs[bugid][idx].text += '\n\n' + text
1.103 + return
1.104 + self.bugs[bugid].append(bugzilla_comment(user, text))
1.105 +
1.106 + def update(self):
1.107 + for bugid, comments in self.bugs.iteritems():
1.108 + for comment in comments:
1.109 + self.add_comment(bugid, comment.text, comment.user)
1.110
1.111 def hook(ui, repo, hooktype, node=None, **kwargs):
1.112 '''add comment to bugzilla for each changeset that refers to a
1.113 @@ -405,12 +437,28 @@
1.114 hooktype)
1.115 try:
1.116 bz = bugzilla(ui, repo)
1.117 - ctx = repo[node]
1.118 - ids = bz.find_bug_ids(ctx)
1.119 - if ids:
1.120 - for id in ids:
1.121 - bz.update(id, ctx)
1.122 - bz.notify(ids, util.email(ctx.user()))
1.123 + if hooktype == 'incoming':
1.124 + ctx = repo[node]
1.125 + ids = bz.find_bug_ids(ctx)
1.126 + if ids:
1.127 + for id in ids:
1.128 + bz.add_update(id, ctx)
1.129 + bz.update()
1.130 + bz.notify()
1.131 + elif hooktype == 'changegroup':
1.132 + start = repo[node].rev()
1.133 + end = len(repo)
1.134 + for rev in xrange(start, end):
1.135 + node = repo.changelog.node(rev)
1.136 + ctx = repo[node]
1.137 + ids = bz.find_bug_ids(ctx)
1.138 + if ids:
1.139 + for id in ids:
1.140 + bz.add_update(id, ctx)
1.141 + bz.update()
1.142 + bz.notify()
1.143 + else:
1.144 + raise util.Abort(_('hook type %s not supported') % hooktype)
1.145 except MySQLdb.MySQLError, err:
1.146 raise util.Abort(_('database error: %s') % err[1])
1.147