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