hgext/bugzilla.py
author Jim Hague <jim.hague@acm.org>
Wed, 30 Mar 2011 09:49:45 +0100
changeset 13800 c2ef8159dabe
parent 13799 d04fc5582772
child 13801 60256f7f30c1
permissions -rw-r--r--
bugzilla: localise all MySQL direct access inside access class. Prepare for the addition of other Bugzilla access methods by localising direct MySQL database access inside an access class. Provide a base access class largely to document the methods required for a class implementing a particular access method. Rename the 'bugzilla_<version>' classes to 'bzmysql_<version>' to emphasise that they are doing access via direct manipulation of a MySQL database.
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
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10112
diff changeset
     6
# GNU General Public License version 2 or any later version.
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
9252
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    10
This hook extension adds comments on bugs in Bugzilla when changesets
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    11
that refer to bugs by Bugzilla ID are seen. The hook does not change
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    12
bug status.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    13
9252
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    14
The hook updates the Bugzilla database directly. Only Bugzilla
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    15
installations using MySQL are supported.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    16
9252
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    17
The hook relies on a Bugzilla script to send bug change notification
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    18
emails. That script changes between Bugzilla versions; the
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    19
'processmail' script used prior to 2.18 is replaced in 2.18 and
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    20
subsequent versions by 'config/sendbugmail.pl'. Note that these will
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    21
be run by Mercurial as the user pushing the change; you will need to
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    22
ensure the Bugzilla install file permissions are set appropriately.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    23
9252
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    24
The extension is configured through three different configuration
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    25
sections. These keys are recognized in the [bugzilla] section:
9203
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    26
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    27
host
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    28
  Hostname of the MySQL server holding the Bugzilla database.
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    29
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    30
db
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    31
  Name of the Bugzilla database in MySQL. Default 'bugs'.
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    32
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    33
user
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    34
  Username to use to access MySQL server. Default 'bugs'.
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    35
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    36
password
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    37
  Password to use to access MySQL server.
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    38
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    39
timeout
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    40
  Database connection timeout (seconds). Default 5.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    41
9203
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    42
version
9252
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    43
  Bugzilla version. Specify '3.0' for Bugzilla versions 3.0 and later,
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    44
  '2.18' for Bugzilla versions from 2.18 and '2.16' for versions prior
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    45
  to 2.18.
9203
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    46
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    47
bzuser
9252
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    48
  Fallback Bugzilla user name to record comments with, if changeset
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    49
  committer cannot be found as a Bugzilla user.
9203
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    50
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    51
bzdir
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    52
   Bugzilla install directory. Used by default notify. Default
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    53
   '/var/www/html/bugzilla'.
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    54
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    55
notify
9252
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    56
  The command to run to get Bugzilla to send bug change notification
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    57
  emails. Substitutes from a map with 3 keys, 'bzdir', 'id' (bug id)
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    58
  and 'user' (committer bugzilla email). Default depends on version;
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    59
  from 2.18 it is "cd %(bzdir)s && perl -T contrib/sendbugmail.pl
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    60
  %(id)s %(user)s".
7985
0edca606c0f1 bugzilla: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7762
diff changeset
    61
9203
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    62
regexp
9252
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    63
  Regular expression to match bug IDs in changeset commit message.
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    64
  Must contain one "()" group. The default expression matches 'Bug
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    65
  1234', 'Bug no. 1234', 'Bug number 1234', 'Bugs 1234,5678', 'Bug
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    66
  1234 and 5678' and variations thereof. Matching is case insensitive.
9203
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    67
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    68
style
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    69
  The style file to use when formatting comments.
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    70
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    71
template
9252
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    72
  Template to use when formatting comments. Overrides style if
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    73
  specified. In addition to the usual Mercurial keywords, the
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    74
  extension specifies::
9203
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    75
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    76
    {bug}       The Bugzilla bug ID.
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    77
    {root}      The full pathname of the Mercurial repository.
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    78
    {webroot}   Stripped pathname of the Mercurial repository.
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    79
    {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
    80
9203
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    81
  Default 'changeset {node|short} in repo {root} refers '
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    82
          'to bug {bug}.\\ndetails:\\n\\t{desc|tabindent}'
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    83
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    84
strip
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    85
  The number of slashes to strip from the front of {root} to produce
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    86
  {webroot}. Default 0.
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    87
9203
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    88
usermap
9252
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    89
  Path of file containing Mercurial committer ID to Bugzilla user ID
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    90
  mappings. If specified, the file should contain one mapping per
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    91
  line, "committer"="Bugzilla user". See also the [usermap] section.
9203
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    92
9252
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    93
The [usermap] section is used to specify mappings of Mercurial
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    94
committer ID to Bugzilla user ID. See also [bugzilla].usermap.
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
    95
"committer"="Bugzilla user"
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
    96
9203
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    97
Finally, the [web] section supports one entry:
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    98
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
    99
baseurl
9252
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
   100
  Base URL for browsing Mercurial repositories. Reference from
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
   101
  templates as {hgweb}.
9203
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
   102
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
   103
Activating the extension::
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   104
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   105
    [extensions]
10112
703db37d186b hgext: enable extensions without "hgext." prefix in help texts
Martin Geisler <mg@lazybytes.net>
parents: 9252
diff changeset
   106
    bugzilla =
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   107
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   108
    [hooks]
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   109
    # 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
   110
    incoming.bugzilla = python:hgext.bugzilla.hook
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   111
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   112
Example configuration:
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   113
9252
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
   114
This example configuration is for a collection of Mercurial
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
   115
repositories in /var/local/hg/repos/ used with a local Bugzilla 3.2
b03aa86f4c10 bugzilla: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9204
diff changeset
   116
installation in /opt/bugzilla-3.2. ::
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   117
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   118
    [bugzilla]
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   119
    host=localhost
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   120
    password=XYZZY
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   121
    version=3.0
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   122
    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
   123
    bzdir=/opt/bugzilla-3.2
9204
8d59e43854d9 bugzilla: wrap example template line
Martin Geisler <mg@lazybytes.net>
parents: 9203
diff changeset
   124
    template=Changeset {node|short} in {root|basename}.
8d59e43854d9 bugzilla: wrap example template line
Martin Geisler <mg@lazybytes.net>
parents: 9203
diff changeset
   125
             {hgweb}/{webroot}/rev/{node|short}\\n
8d59e43854d9 bugzilla: wrap example template line
Martin Geisler <mg@lazybytes.net>
parents: 9203
diff changeset
   126
             {desc}\\n
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   127
    strip=5
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   128
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   129
    [web]
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   130
    baseurl=http://dev.domain.com/hg
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   131
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   132
    [usermap]
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   133
    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
   134
9203
e4dbd49b88a9 bugzilla: reformat list of configuration options
Martin Geisler <mg@lazybytes.net>
parents: 9054
diff changeset
   135
Commits add a comment to the Bugzilla bug record of the form::
7504
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   136
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   137
    Changeset 3b16791d6642 in repository-name.
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   138
    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
   139
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   140
    Changeset commit comment. Bug 1234.
d8cd79fbed3c Revise Bugzilla module comments into extension help.
Jim Hague <jim.hague@acm.org>
parents: 7493
diff changeset
   141
'''
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   142
3891
6b4127c7d52a Simplify i18n imports
Matt Mackall <mpm@selenic.com>
parents: 3877
diff changeset
   143
from mercurial.i18n import _
6211
f89fd07fc51d Expand import * to allow Pyflakes to find problems
Joel Rosdahl <joel@rosdahl.net>
parents: 5975
diff changeset
   144
from mercurial.node import short
3877
abaee83ce0a6 Replace demandload with new demandimport
Matt Mackall <mpm@selenic.com>
parents: 3876
diff changeset
   145
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
   146
import re, time
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   147
13800
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   148
class bzaccess(object):
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   149
    '''Base class for access to Bugzilla.'''
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   150
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   151
    def __init__(self, ui):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   152
        self.ui = ui
13800
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   153
        usermap = self.ui.config('bugzilla', 'usermap')
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   154
        if usermap:
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   155
            self.ui.readconfig(usermap, sections=['usermap'])
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   156
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   157
    def map_committer(self, user):
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   158
        '''map name of committer to Bugzilla user name.'''
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   159
        for committer, bzuser in self.ui.configitems('usermap'):
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   160
            if committer.lower() == user.lower():
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   161
                return bzuser
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   162
        return user
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   163
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   164
    # Methods to be implemented by access classes.
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   165
    def filter_real_bug_ids(self, ids):
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   166
        '''remove bug IDs that do not exist in Bugzilla from set.'''
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   167
        pass
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   168
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   169
    def filter_cset_known_bug_ids(self, node, ids):
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   170
        '''remove bug IDs where node occurs in comment text from set.'''
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   171
        pass
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   172
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   173
    def add_comment(self, bugid, text, committer):
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   174
        '''add comment to bug.
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   175
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   176
        If possible add the comment as being from the committer of
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   177
        the changeset. Otherwise use the default Bugzilla user.
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   178
        '''
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   179
        pass
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   180
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   181
    def notify(self, ids, committer):
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   182
        '''Force sending of Bugzilla notification emails.'''
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   183
        pass
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   184
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   185
# Bugzilla via direct access to MySQL database.
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   186
class bzmysql(bzaccess):
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   187
    '''Support for direct MySQL access to Bugzilla.
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   188
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   189
    The earliest Bugzilla version this is tested with is version 2.16.
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   190
    '''
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   191
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   192
    @staticmethod
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   193
    def sql_buglist(ids):
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   194
        '''return SQL-friendly list of bug ids'''
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   195
        return '(' + ','.join(map(str, ids)) + ')'
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   196
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   197
    _MySQLdb = None
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   198
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   199
    def __init__(self, ui):
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   200
        try:
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   201
            import MySQLdb as mysql
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   202
            bzmysql._MySQLdb = mysql
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   203
        except ImportError, err:
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   204
            raise util.Abort(_('python mysql support not available: %s') % err)
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   205
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   206
        bzaccess.__init__(self, ui)
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   207
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   208
        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
   209
        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
   210
        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
   211
        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
   212
        timeout = int(self.ui.config('bugzilla', 'timeout', 5))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   213
        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
   214
                     (host, db, user, '*' * len(passwd)))
13800
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   215
        self.conn = bzmysql._MySQLdb.connect(host=host,
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   216
                                                   user=user, passwd=passwd,
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   217
                                                   db=db,
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   218
                                                   connect_timeout=timeout)
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   219
        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
   220
        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
   221
        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
   222
        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
   223
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   224
    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
   225
        '''run a query.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   226
        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
   227
        try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   228
            self.cursor.execute(*args, **kwargs)
13800
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   229
        except bzmysql._MySQLdb.MySQLError:
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   230
            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
   231
            raise
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   232
7019
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   233
    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
   234
        '''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
   235
        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
   236
        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
   237
        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
   238
            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
   239
        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
   240
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   241
    def filter_real_bug_ids(self, ids):
13799
d04fc5582772 bugzilla: keep bug IDs in set.
Jim Hague <jim.hague@acm.org>
parents: 13798
diff changeset
   242
        '''filter not-existing bug ids from set.'''
13800
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   243
        self.run('select bug_id from bugs where bug_id in %s' %
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   244
                 bzmysql.sql_buglist(ids))
13799
d04fc5582772 bugzilla: keep bug IDs in set.
Jim Hague <jim.hague@acm.org>
parents: 13798
diff changeset
   245
        return set([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
   246
13798
9c9fa78f4e2d bugzilla: rename filter_unknown_bug_ids to reflect its actual purpose.
Jim Hague <jim.hague@acm.org>
parents: 11567
diff changeset
   247
    def filter_cset_known_bug_ids(self, node, ids):
13799
d04fc5582772 bugzilla: keep bug IDs in set.
Jim Hague <jim.hague@acm.org>
parents: 13798
diff changeset
   248
        '''filter bug ids that already refer to this changeset from set.'''
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   249
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   250
        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
   251
                    bug_id in %s and thetext like "%%%s%%"''' %
13800
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   252
                 (bzmysql.sql_buglist(ids), short(node)))
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   253
        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
   254
            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
   255
                           (id, short(node)))
13799
d04fc5582772 bugzilla: keep bug IDs in set.
Jim Hague <jim.hague@acm.org>
parents: 13798
diff changeset
   256
            ids.discard(id)
d04fc5582772 bugzilla: keep bug IDs in set.
Jim Hague <jim.hague@acm.org>
parents: 13798
diff changeset
   257
        return ids
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   258
7618
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   259
    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
   260
        '''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
   261
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   262
        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
   263
        (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
   264
        for id in ids:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   265
            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
   266
            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
   267
            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
   268
            try:
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   269
                # 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
   270
                # 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
   271
                # string.
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   272
                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
   273
            except TypeError:
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   274
                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
   275
            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
   276
            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
   277
            out = fp.read()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   278
            ret = fp.close()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   279
            if ret:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   280
                self.ui.warn(out)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   281
                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
   282
                                 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
   283
        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
   284
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   285
    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
   286
        '''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
   287
        try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   288
            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
   289
        except KeyError:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   290
            try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   291
                userid = int(user)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   292
            except ValueError:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   293
                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
   294
                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
   295
                            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
   296
                all = self.cursor.fetchall()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   297
                if len(all) != 1:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   298
                    raise KeyError(user)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   299
                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
   300
            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
   301
            return userid
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   302
7618
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   303
    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
   304
        '''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
   305
        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
   306
        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
   307
        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
   308
        try:
2306
4c67ba93560b bugzilla: allow to map between committer email and bugzilla user name.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2239
diff changeset
   309
            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
   310
        except KeyError:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   311
            try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   312
                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
   313
                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
   314
                    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
   315
                                     user)
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   316
                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
   317
                user = defaultuser
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   318
            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
   319
                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
   320
                                 (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
   321
        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
   322
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   323
    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
   324
        '''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
   325
        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
   326
        (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
   327
        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
   328
        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
   329
                    (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
   330
                    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
   331
                 (bugid, userid, now, text))
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   332
        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
   333
                    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
   334
                 (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
   335
        self.conn.commit()
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   336
13800
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   337
class bzmysql_2_18(bzmysql):
7618
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   338
    '''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
   339
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   340
    def __init__(self, ui):
13800
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   341
        bzmysql.__init__(self, ui)
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   342
        self.default_notify = \
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   343
            "cd %(bzdir)s && perl -T contrib/sendbugmail.pl %(id)s %(user)s"
7618
6c89dd0a7797 Bugzilla 2.18 and on use contrib/sendbugmail.pl, not processmail.
Jim Hague <jim.hague@acm.org>
parents: 7504
diff changeset
   344
13800
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   345
class bzmysql_3_0(bzmysql_2_18):
7019
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   346
    '''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
   347
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   348
    def __init__(self, ui):
13800
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   349
        bzmysql_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
   350
6b1ece890f9a Add support for Bugzilla 3.0 series to bugzilla hook.
Jim Hague <jim.hague@acm.org>
parents: 6762
diff changeset
   351
    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
   352
        '''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
   353
        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
   354
        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
   355
        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
   356
            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
   357
        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
   358
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   359
class bugzilla(object):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   360
    # 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
   361
    # different schemas.
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   362
    _versions = {
13800
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   363
        '2.16': bzmysql,
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   364
        '2.18': bzmysql_2_18,
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   365
        '3.0':  bzmysql_3_0
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   366
        }
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   367
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   368
    _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
   369
                       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
   370
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   371
    _bz = None
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   372
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   373
    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
   374
        self.ui = ui
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   375
        self.repo = repo
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   376
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   377
    def bz(self):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   378
        '''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
   379
        use.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   380
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   381
        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
   382
            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
   383
            try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   384
                bzclass = bugzilla._versions[bzversion]
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   385
            except KeyError:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   386
                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
   387
                                 bzversion)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   388
            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
   389
        return bugzilla._bz
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   390
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   391
    def __getattr__(self, key):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   392
        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
   393
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   394
    _bug_re = None
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   395
    _split_re = None
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   396
3976
f8849648b0e2 bugzilla: use contexts, simplify
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3891
diff changeset
   397
    def find_bug_ids(self, ctx):
13799
d04fc5582772 bugzilla: keep bug IDs in set.
Jim Hague <jim.hague@acm.org>
parents: 13798
diff changeset
   398
        '''return set of integer bug IDs from commit comment.
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   399
13799
d04fc5582772 bugzilla: keep bug IDs in set.
Jim Hague <jim.hague@acm.org>
parents: 13798
diff changeset
   400
        Extract bug IDs from changeset comments. Filter out any that are
d04fc5582772 bugzilla: keep bug IDs in set.
Jim Hague <jim.hague@acm.org>
parents: 13798
diff changeset
   401
        not known to Bugzilla, and any that already have a reference to
d04fc5582772 bugzilla: keep bug IDs in set.
Jim Hague <jim.hague@acm.org>
parents: 13798
diff changeset
   402
        the given changeset in their comments.
d04fc5582772 bugzilla: keep bug IDs in set.
Jim Hague <jim.hague@acm.org>
parents: 13798
diff changeset
   403
        '''
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   404
        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
   405
            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
   406
                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
   407
                re.IGNORECASE)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   408
            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
   409
        start = 0
8455
a858b54d072b bugzilla: use set instead of dict
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8225
diff changeset
   410
        ids = set()
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   411
        while True:
3976
f8849648b0e2 bugzilla: use contexts, simplify
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3891
diff changeset
   412
            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
   413
            if not m:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   414
                break
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   415
            start = m.end()
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   416
            for id in bugzilla._split_re.split(m.group(1)):
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   417
                if not id:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   418
                    continue
8455
a858b54d072b bugzilla: use set instead of dict
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8225
diff changeset
   419
                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
   420
        if ids:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   421
            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
   422
        if ids:
13798
9c9fa78f4e2d bugzilla: rename filter_unknown_bug_ids to reflect its actual purpose.
Jim Hague <jim.hague@acm.org>
parents: 11567
diff changeset
   423
            ids = self.filter_cset_known_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
   424
        return ids
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   425
3976
f8849648b0e2 bugzilla: use contexts, simplify
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3891
diff changeset
   426
    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
   427
        '''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
   428
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   429
        def webroot(root):
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   430
            '''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
   431
            url-safe path.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   432
            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
   433
            root = util.pconvert(root)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   434
            while count > 0:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   435
                c = root.find('/')
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   436
                if c == -1:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   437
                    break
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   438
                root = root[c + 1:]
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   439
                count -= 1
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   440
            return root
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   441
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   442
        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
   443
        tmpl = self.ui.config('bugzilla', 'template')
3741
0897bf8d54c7 update bugzilla extension to use ui buffers
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   444
        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
   445
                                        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
   446
        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
   447
            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
   448
                     '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
   449
        if tmpl:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   450
            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
   451
            t.use_template(tmpl)
3741
0897bf8d54c7 update bugzilla extension to use ui buffers
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   452
        self.ui.pushbuffer()
7369
87158be081b8 cmdutil: use change contexts for cset-printer and cset-templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7280
diff changeset
   453
        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
   454
               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
   455
               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
   456
               root=self.repo.root,
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   457
               webroot=webroot(self.repo.root))
3741
0897bf8d54c7 update bugzilla extension to use ui buffers
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   458
        data = self.ui.popbuffer()
5975
75d9fe70c654 templater: move email function to util
Matt Mackall <mpm@selenic.com>
parents: 4431
diff changeset
   459
        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
   460
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   461
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
   462
    '''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
   463
    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
   464
    seen multiple times does not fill bug with duplicate data.'''
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   465
    if node is None:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   466
        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
   467
                         hooktype)
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   468
    try:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   469
        bz = bugzilla(ui, repo)
6747
f6c00b17387c use repo[changeid] to get a changectx
Matt Mackall <mpm@selenic.com>
parents: 6548
diff changeset
   470
        ctx = repo[node]
3976
f8849648b0e2 bugzilla: use contexts, simplify
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3891
diff changeset
   471
        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
   472
        if ids:
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   473
            for id in ids:
3976
f8849648b0e2 bugzilla: use contexts, simplify
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 3891
diff changeset
   474
                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
   475
            bz.notify(ids, util.email(ctx.user()))
13800
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   476
    except Exception, e:
c2ef8159dabe bugzilla: localise all MySQL direct access inside access class.
Jim Hague <jim.hague@acm.org>
parents: 13799
diff changeset
   477
        raise util.Abort(_('Bugzilla error: %s') % e)
2192
2be3ac7abc21 add bugzilla integration hook. example of writing hook in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   478