hgext/bugzilla.py
author Martin Geisler <mg@lazybytes.net>
Tue, 07 Jul 2009 23:54:42 +0200
changeset 9054 84be8a8cc8b0
parent 8935 f4f0e902b750
child 9203 e4dbd49b88a9
permissions -rw-r--r--
bugzilla: wrapped docstrings at 78 characters
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     1
# bugzilla.py - bugzilla integration for mercurial
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     2
#
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     3
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     4
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8209
diff changeset
     5
# This software may be used and distributed according to the terms of the
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8209
diff changeset
     6
# GNU General Public License version 2, incorporated herein by reference.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
     7
8935
f4f0e902b750 extensions: change descriptions for hook-providing extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8894
diff changeset
     8
'''hooks for integrating with the Bugzilla bug tracker
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
     9
9054
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    10
This hook extension adds comments on bugs in Bugzilla when changesets that
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    11
refer to bugs by Bugzilla ID are seen. The hook does not change bug status.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    12
9054
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    13
The hook updates the Bugzilla database directly. Only Bugzilla installations
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    14
using MySQL are supported.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    15
9054
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    16
The hook relies on a Bugzilla script to send bug change notification emails.
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    17
That script changes between Bugzilla versions; the 'processmail' script used
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    18
prior to 2.18 is replaced in 2.18 and subsequent versions by
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    19
'config/sendbugmail.pl'. Note that these will be run by Mercurial as the user
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    20
pushing the change; you will need to ensure the Bugzilla install file
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    21
permissions are set appropriately.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    22
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    23
Configuring the extension:
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    24
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    25
    [bugzilla]
7985
0edca606c0f1 bugzilla: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7762
diff changeset
    26
9054
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    27
    host       Hostname of the MySQL server holding the Bugzilla database.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    28
    db         Name of the Bugzilla database in MySQL. Default 'bugs'.
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    29
    user       Username to use to access MySQL server. Default 'bugs'.
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    30
    password   Password to use to access MySQL server.
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    31
    timeout    Database connection timeout (seconds). Default 5.
9054
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    32
    version    Bugzilla version. Specify '3.0' for Bugzilla versions 3.0 and
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    33
               later, '2.18' for Bugzilla versions from 2.18 and '2.16' for
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    34
               versions prior to 2.18.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    35
    bzuser     Fallback Bugzilla user name to record comments with, if
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    36
               changeset committer cannot be found as a Bugzilla user.
9054
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    37
    bzdir      Bugzilla install directory. Used by default notify. Default
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    38
               '/var/www/html/bugzilla'.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    39
    notify     The command to run to get Bugzilla to send bug change
9054
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    40
               notification emails. Substitutes from a map with 3 keys,
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    41
               'bzdir', 'id' (bug id) and 'user' (committer bugzilla email).
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    42
               Default depends on version; from 2.18 it is "cd %(bzdir)s &&
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    43
               perl -T contrib/sendbugmail.pl %(id)s %(user)s".
7985
0edca606c0f1 bugzilla: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7762
diff changeset
    44
    regexp     Regular expression to match bug IDs in changeset commit
9054
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    45
               message. Must contain one "()" group. The default expression
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    46
               matches 'Bug 1234', 'Bug no. 1234', 'Bug number 1234', 'Bugs
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    47
               1234,5678', 'Bug 1234 and 5678' and variations thereof.
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    48
               Matching is case insensitive.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    49
    style      The style file to use when formatting comments.
9054
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    50
    template   Template to use when formatting comments. Overrides style if
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    51
               specified. In addition to the usual Mercurial keywords, the
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    52
               extension specifies:
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    53
                   {bug}       The Bugzilla bug ID.
9054
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    54
                   {root}      The full pathname of the Mercurial repository.
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    55
                   {webroot}   Stripped pathname of the Mercurial repository.
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    56
                   {hgweb}     Base URL for browsing Mercurial repositories.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    57
               Default 'changeset {node|short} in repo {root} refers '
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    58
                       'to bug {bug}.\\ndetails:\\n\\t{desc|tabindent}'
9054
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    59
    strip      The number of slashes to strip from the front of {root} to
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    60
               produce {webroot}. Default 0.
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    61
    usermap    Path of file containing Mercurial committer ID to Bugzilla user
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    62
               ID mappings. If specified, the file should contain one mapping
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    63
               per line, "committer"="Bugzilla user". See also the [usermap]
7985
0edca606c0f1 bugzilla: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7762
diff changeset
    64
               section.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    65
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    66
    [usermap]
9054
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    67
    Any entries in this section specify mappings of Mercurial committer ID to
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    68
    Bugzilla user ID. See also [bugzilla].usermap. "committer"="Bugzilla user"
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    69
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    70
    [web]
9054
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    71
    baseurl    Base URL for browsing Mercurial repositories. Reference from
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    72
               templates as {hgweb}.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    73
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    74
Activating the extension:
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    75
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    76
    [extensions]
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    77
    hgext.bugzilla =
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    78
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    79
    [hooks]
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    80
    # run bugzilla hook on every change pulled or pushed in here
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    81
    incoming.bugzilla = python:hgext.bugzilla.hook
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    82
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    83
Example configuration:
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    84
9054
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    85
This example configuration is for a collection of Mercurial repositories in
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    86
/var/local/hg/repos/ used with a local Bugzilla 3.2 installation in
84be8a8cc8b0 bugzilla: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
    87
/opt/bugzilla-3.2.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    88
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    89
    [bugzilla]
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    90
    host=localhost
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    91
    password=XYZZY
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    92
    version=3.0
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    93
    bzuser=unknown@domain.com
7618
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
    94
    bzdir=/opt/bugzilla-3.2
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    95
    template=Changeset {node|short} in {root|basename}.\\n{hgweb}/{webroot}/rev/{node|short}\\n\\n{desc}\\n
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    96
    strip=5
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    97
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    98
    [web]
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    99
    baseurl=http://dev.domain.com/hg
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   100
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   101
    [usermap]
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   102
    user@emaildomain.com=user.name@bugzilladomain.com
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   103
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   104
Commits add a comment to the Bugzilla bug record of the form:
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   105
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   106
    Changeset 3b16791d6642 in repository-name.
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   107
    http://dev.domain.com/hg/repository-name/rev/3b16791d6642
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   108
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   109
    Changeset commit comment. Bug 1234.
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   110
'''
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   111
3891
6b4127c7d52a Simplify i18n imports
Matt Mackall <mpm@selenic.com>
parents: 3877
diff changeset
   112
from mercurial.i18n import _
6211
f89fd07fc51d Expand import * to allow Pyflakes to find problems
Joel Rosdahl <joel@rosdahl.net>
parents: 5975
diff changeset
   113
from mercurial.node import short
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents: 3876
diff changeset
   114
from mercurial import cmdutil, templater, util
6548
962eb403165b replace usage of os.popen() with util.popen()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6211
diff changeset
   115
import re, time
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   116
2218
afe24f5b7a9e only import mysql module if hook used.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2192
diff changeset
   117
MySQLdb = None
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   118
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   119
def buglist(ids):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   120
    return '(' + ','.join(map(str, ids)) + ')'
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   121
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   122
class bugzilla_2_16(object):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   123
    '''support for bugzilla version 2.16.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   124
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   125
    def __init__(self, ui):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   126
        self.ui = ui
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   127
        host = self.ui.config('bugzilla', 'host', 'localhost')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   128
        user = self.ui.config('bugzilla', 'user', 'bugs')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   129
        passwd = self.ui.config('bugzilla', 'password')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   130
        db = self.ui.config('bugzilla', 'db', 'bugs')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   131
        timeout = int(self.ui.config('bugzilla', 'timeout', 5))
2306
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   132
        usermap = self.ui.config('bugzilla', 'usermap')
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   133
        if usermap:
8195
1750510251d0 bugzilla: fix section reading
Matt Mackall <mpm@selenic.com>
parents: 8152
diff changeset
   134
            self.ui.readconfig(usermap, sections=['usermap'])
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   135
        self.ui.note(_('connecting to %s:%s as %s, password %s\n') %
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   136
                     (host, db, user, '*' * len(passwd)))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   137
        self.conn = MySQLdb.connect(host=host, user=user, passwd=passwd,
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   138
                                    db=db, connect_timeout=timeout)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   139
        self.cursor = self.conn.cursor()
7019
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   140
        self.longdesc_id = self.get_longdesc_id()
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   141
        self.user_ids = {}
7618
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   142
        self.default_notify = "cd %(bzdir)s && ./processmail %(id)s %(user)s"
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   143
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   144
    def run(self, *args, **kwargs):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   145
        '''run a query.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   146
        self.ui.note(_('query: %s %s\n') % (args, kwargs))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   147
        try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   148
            self.cursor.execute(*args, **kwargs)
7280
810ca383da9c remove unused variables
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7019
diff changeset
   149
        except MySQLdb.MySQLError:
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   150
            self.ui.note(_('failed query: %s %s\n') % (args, kwargs))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   151
            raise
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   152
7019
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   153
    def get_longdesc_id(self):
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   154
        '''get identity of longdesc field'''
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   155
        self.run('select fieldid from fielddefs where name = "longdesc"')
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   156
        ids = self.cursor.fetchall()
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   157
        if len(ids) != 1:
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   158
            raise util.Abort(_('unknown database schema'))
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   159
        return ids[0][0]
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   160
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   161
    def filter_real_bug_ids(self, ids):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   162
        '''filter not-existing bug ids from list.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   163
        self.run('select bug_id from bugs where bug_id in %s' % buglist(ids))
8209
a1a5a57efe90 replace util.sort with sorted built-in
Matt Mackall <mpm@selenic.com>
parents: 8195
diff changeset
   164
        return sorted([c[0] for c in self.cursor.fetchall()])
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   165
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   166
    def filter_unknown_bug_ids(self, node, ids):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   167
        '''filter bug ids from list that already refer to this changeset.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   168
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   169
        self.run('''select bug_id from longdescs where
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   170
                    bug_id in %s and thetext like "%%%s%%"''' %
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   171
                 (buglist(ids), short(node)))
8152
08e1baf924ca replace set-like dictionaries with real sets
Martin Geisler <mg@lazybytes.net>
parents: 8142
diff changeset
   172
        unknown = set(ids)
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   173
        for (id,) in self.cursor.fetchall():
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   174
            self.ui.status(_('bug %d already knows about changeset %s\n') %
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   175
                           (id, short(node)))
8152
08e1baf924ca replace set-like dictionaries with real sets
Martin Geisler <mg@lazybytes.net>
parents: 8142
diff changeset
   176
            unknown.discard(id)
8209
a1a5a57efe90 replace util.sort with sorted built-in
Matt Mackall <mpm@selenic.com>
parents: 8195
diff changeset
   177
        return sorted(unknown)
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   178
7618
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   179
    def notify(self, ids, committer):
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   180
        '''tell bugzilla to send mail.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   181
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   182
        self.ui.status(_('telling bugzilla to send mail:\n'))
7618
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   183
        (user, userid) = self.get_bugzilla_user(committer)
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   184
        for id in ids:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   185
            self.ui.status(_('  bug %s\n') % id)
7618
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   186
            cmdfmt = self.ui.config('bugzilla', 'notify', self.default_notify)
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   187
            bzdir = self.ui.config('bugzilla', 'bzdir', '/var/www/html/bugzilla')
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   188
            try:
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   189
                # Backwards-compatible with old notify string, which
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   190
                # took one string. This will throw with a new format
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   191
                # string.
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   192
                cmd = cmdfmt % id
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   193
            except TypeError:
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   194
                cmd = cmdfmt % {'bzdir': bzdir, 'id': id, 'user': user}
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   195
            self.ui.note(_('running notify command %s\n') % cmd)
6548
962eb403165b replace usage of os.popen() with util.popen()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6211
diff changeset
   196
            fp = util.popen('(%s) 2>&1' % cmd)
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   197
            out = fp.read()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   198
            ret = fp.close()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   199
            if ret:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   200
                self.ui.warn(out)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   201
                raise util.Abort(_('bugzilla notify command %s') %
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   202
                                 util.explain_exit(ret)[0])
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   203
        self.ui.status(_('done\n'))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   204
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   205
    def get_user_id(self, user):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   206
        '''look up numeric bugzilla user id.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   207
        try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   208
            return self.user_ids[user]
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   209
        except KeyError:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   210
            try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   211
                userid = int(user)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   212
            except ValueError:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   213
                self.ui.note(_('looking up user %s\n') % user)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   214
                self.run('''select userid from profiles
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   215
                            where login_name like %s''', user)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   216
                all = self.cursor.fetchall()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   217
                if len(all) != 1:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   218
                    raise KeyError(user)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   219
                userid = int(all[0][0])
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   220
            self.user_ids[user] = userid
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   221
            return userid
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   222
2306
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   223
    def map_committer(self, user):
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   224
        '''map name of committer to bugzilla user name.'''
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   225
        for committer, bzuser in self.ui.configitems('usermap'):
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   226
            if committer.lower() == user.lower():
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   227
                return bzuser
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   228
        return user
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   229
7618
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   230
    def get_bugzilla_user(self, committer):
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   231
        '''see if committer is a registered bugzilla user. Return
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   232
        bugzilla username and userid if so. If not, return default
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   233
        bugzilla username and userid.'''
2306
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   234
        user = self.map_committer(committer)
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   235
        try:
2306
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   236
            userid = self.get_user_id(user)
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   237
        except KeyError:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   238
            try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   239
                defaultuser = self.ui.config('bugzilla', 'bzuser')
2306
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   240
                if not defaultuser:
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   241
                    raise util.Abort(_('cannot find bugzilla user id for %s') %
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   242
                                     user)
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   243
                userid = self.get_user_id(defaultuser)
7618
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   244
                user = defaultuser
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   245
            except KeyError:
2306
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   246
                raise util.Abort(_('cannot find bugzilla user id for %s or %s') %
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   247
                                 (user, defaultuser))
7618
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   248
        return (user, userid)
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   249
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   250
    def add_comment(self, bugid, text, committer):
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   251
        '''add comment to bug. try adding comment as committer of
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   252
        changeset, otherwise as default bugzilla user.'''
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   253
        (user, userid) = self.get_bugzilla_user(committer)
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   254
        now = time.strftime('%Y-%m-%d %H:%M:%S')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   255
        self.run('''insert into longdescs
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   256
                    (bug_id, who, bug_when, thetext)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   257
                    values (%s, %s, %s, %s)''',
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   258
                 (bugid, userid, now, text))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   259
        self.run('''insert into bugs_activity (bug_id, who, bug_when, fieldid)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   260
                    values (%s, %s, %s, %s)''',
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   261
                 (bugid, userid, now, self.longdesc_id))
7493
518afef5e350 Fix Bugzilla integration to work with new Bugzilla 3.2.
Jim Hague <jim.hague@acm.org>
parents: 7369
diff changeset
   262
        self.conn.commit()
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   263
7618
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   264
class bugzilla_2_18(bugzilla_2_16):
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   265
    '''support for bugzilla 2.18 series.'''
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   266
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   267
    def __init__(self, ui):
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   268
        bugzilla_2_16.__init__(self, ui)
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   269
        self.default_notify = "cd %(bzdir)s && perl -T contrib/sendbugmail.pl %(id)s %(user)s"
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   270
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   271
class bugzilla_3_0(bugzilla_2_18):
7019
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   272
    '''support for bugzilla 3.0 series.'''
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   273
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   274
    def __init__(self, ui):
7618
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   275
        bugzilla_2_18.__init__(self, ui)
7019
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   276
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   277
    def get_longdesc_id(self):
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   278
        '''get identity of longdesc field'''
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   279
        self.run('select id from fielddefs where name = "longdesc"')
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   280
        ids = self.cursor.fetchall()
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   281
        if len(ids) != 1:
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   282
            raise util.Abort(_('unknown database schema'))
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   283
        return ids[0][0]
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   284
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   285
class bugzilla(object):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   286
    # supported versions of bugzilla. different versions have
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   287
    # different schemas.
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   288
    _versions = {
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   289
        '2.16': bugzilla_2_16,
7618
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   290
        '2.18': bugzilla_2_18,
7019
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   291
        '3.0':  bugzilla_3_0
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   292
        }
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   293
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   294
    _default_bug_re = (r'bugs?\s*,?\s*(?:#|nos?\.?|num(?:ber)?s?)?\s*'
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   295
                       r'((?:\d+\s*(?:,?\s*(?:and)?)?\s*)+)')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   296
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   297
    _bz = None
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   298
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   299
    def __init__(self, ui, repo):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   300
        self.ui = ui
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   301
        self.repo = repo
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   302
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   303
    def bz(self):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   304
        '''return object that knows how to talk to bugzilla version in
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   305
        use.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   306
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   307
        if bugzilla._bz is None:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   308
            bzversion = self.ui.config('bugzilla', 'version')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   309
            try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   310
                bzclass = bugzilla._versions[bzversion]
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   311
            except KeyError:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   312
                raise util.Abort(_('bugzilla version %s not supported') %
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   313
                                 bzversion)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   314
            bugzilla._bz = bzclass(self.ui)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   315
        return bugzilla._bz
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   316
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   317
    def __getattr__(self, key):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   318
        return getattr(self.bz(), key)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   319
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   320
    _bug_re = None
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   321
    _split_re = None
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   322
3976
f8849648b0e2 bugzilla: use contexts, simplify
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3891
diff changeset
   323
    def find_bug_ids(self, ctx):
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   324
        '''find valid bug ids that are referred to in changeset
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   325
        comments and that do not already have references to this
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   326
        changeset.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   327
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   328
        if bugzilla._bug_re is None:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   329
            bugzilla._bug_re = re.compile(
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   330
                self.ui.config('bugzilla', 'regexp', bugzilla._default_bug_re),
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   331
                re.IGNORECASE)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   332
            bugzilla._split_re = re.compile(r'\D+')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   333
        start = 0
8455
a858b54d072b bugzilla: use set instead of dict
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8225
diff changeset
   334
        ids = set()
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   335
        while True:
3976
f8849648b0e2 bugzilla: use contexts, simplify
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3891
diff changeset
   336
            m = bugzilla._bug_re.search(ctx.description(), start)
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   337
            if not m:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   338
                break
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   339
            start = m.end()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   340
            for id in bugzilla._split_re.split(m.group(1)):
2239
5e5adc1910ed bugzilla hook: skip empty groups.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2221
diff changeset
   341
                if not id: continue
8455
a858b54d072b bugzilla: use set instead of dict
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8225
diff changeset
   342
                ids.add(int(id))
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   343
        if ids:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   344
            ids = self.filter_real_bug_ids(ids)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   345
        if ids:
3976
f8849648b0e2 bugzilla: use contexts, simplify
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3891
diff changeset
   346
            ids = self.filter_unknown_bug_ids(ctx.node(), ids)
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   347
        return ids
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   348
3976
f8849648b0e2 bugzilla: use contexts, simplify
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3891
diff changeset
   349
    def update(self, bugid, ctx):
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   350
        '''update bugzilla bug with reference to changeset.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   351
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   352
        def webroot(root):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   353
            '''strip leading prefix of repo root and turn into
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   354
            url-safe path.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   355
            count = int(self.ui.config('bugzilla', 'strip', 0))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   356
            root = util.pconvert(root)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   357
            while count > 0:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   358
                c = root.find('/')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   359
                if c == -1:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   360
                    break
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   361
                root = root[c+1:]
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   362
                count -= 1
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   363
            return root
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   364
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   365
        mapfile = self.ui.config('bugzilla', 'style')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   366
        tmpl = self.ui.config('bugzilla', 'template')
3741
0897bf8d54c7 update bugzilla extension to use ui buffers
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   367
        t = cmdutil.changeset_templater(self.ui, self.repo,
7762
fece056bf240 add --git option to commands supporting --patch (log, incoming, history, tip)
Jim Correia <jim.correia@pobox.com>
parents: 7618
diff changeset
   368
                                        False, None, mapfile, False)
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   369
        if not mapfile and not tmpl:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   370
            tmpl = _('changeset {node|short} in repo {root} refers '
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   371
                     'to bug {bug}.\ndetails:\n\t{desc|tabindent}')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   372
        if tmpl:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   373
            tmpl = templater.parsestring(tmpl, quoted=False)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   374
            t.use_template(tmpl)
3741
0897bf8d54c7 update bugzilla extension to use ui buffers
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   375
        self.ui.pushbuffer()
7369
87158be081b8 cmdutil: use change contexts for cset-printer and cset-templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7280
diff changeset
   376
        t.show(ctx, changes=ctx.changeset(),
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   377
               bug=str(bugid),
2197
5de8b44f0446 define standard name for base url to use when printing hgweb urls.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2192
diff changeset
   378
               hgweb=self.ui.config('web', 'baseurl'),
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   379
               root=self.repo.root,
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   380
               webroot=webroot(self.repo.root))
3741
0897bf8d54c7 update bugzilla extension to use ui buffers
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   381
        data = self.ui.popbuffer()
5975
75d9fe70c654 templater: move email function to util
Matt Mackall <mpm@selenic.com>
parents: 4431
diff changeset
   382
        self.add_comment(bugid, data, util.email(ctx.user()))
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   383
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   384
def hook(ui, repo, hooktype, node=None, **kwargs):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   385
    '''add comment to bugzilla for each changeset that refers to a
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   386
    bugzilla bug id. only add a comment once per bug, so same change
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   387
    seen multiple times does not fill bug with duplicate data.'''
2218
afe24f5b7a9e only import mysql module if hook used.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2192
diff changeset
   388
    try:
afe24f5b7a9e only import mysql module if hook used.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2192
diff changeset
   389
        import MySQLdb as mysql
afe24f5b7a9e only import mysql module if hook used.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2192
diff changeset
   390
        global MySQLdb
afe24f5b7a9e only import mysql module if hook used.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2192
diff changeset
   391
        MySQLdb = mysql
afe24f5b7a9e only import mysql module if hook used.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2192
diff changeset
   392
    except ImportError, err:
afe24f5b7a9e only import mysql module if hook used.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2192
diff changeset
   393
        raise util.Abort(_('python mysql support not available: %s') % err)
afe24f5b7a9e only import mysql module if hook used.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2192
diff changeset
   394
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   395
    if node is None:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   396
        raise util.Abort(_('hook type %s does not pass a changeset id') %
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   397
                         hooktype)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   398
    try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   399
        bz = bugzilla(ui, repo)
6747
f6c00b17387c use repo[changeid] to get a changectx
Matt Mackall <mpm@selenic.com>
parents: 6548
diff changeset
   400
        ctx = repo[node]
3976
f8849648b0e2 bugzilla: use contexts, simplify
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3891
diff changeset
   401
        ids = bz.find_bug_ids(ctx)
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   402
        if ids:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   403
            for id in ids:
3976
f8849648b0e2 bugzilla: use contexts, simplify
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3891
diff changeset
   404
                bz.update(id, ctx)
7618
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   405
            bz.notify(ids, util.email(ctx.user()))
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   406
    except MySQLdb.MySQLError, err:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   407
        raise util.Abort(_('database error: %s') % err[1])
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   408