bugzilla.py
912 lines
| 34.0 KiB
| text/x-python
|
PythonLexer
/ hgext / bugzilla.py
|
r2192 | # bugzilla.py - bugzilla integration for mercurial | ||
# | ||||
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> | ||||
|
r16221 | # Copyright 2011-2 Jim Hague <jim.hague@acm.org> | ||
|
r2192 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r7504 | |||
|
r8935 | '''hooks for integrating with the Bugzilla bug tracker | ||
|
r7504 | |||
|
r9252 | This hook extension adds comments on bugs in Bugzilla when changesets | ||
|
r13801 | that refer to bugs by Bugzilla ID are seen. The comment is formatted using | ||
the Mercurial template mechanism. | ||||
|
r7504 | |||
|
r16222 | The bug references can optionally include an update for Bugzilla of the | ||
|
r16223 | hours spent working on the bug. Bugs can also be marked fixed. | ||
|
r7504 | |||
|
r13802 | Three basic modes of access to Bugzilla are provided: | ||
|
r7504 | |||
|
r13802 | 1. Access via the Bugzilla XMLRPC interface. Requires Bugzilla 3.4 or later. | ||
2. Check data via the Bugzilla XMLRPC interface and submit bug change | ||||
via email to Bugzilla email interface. Requires Bugzilla 3.4 or later. | ||||
|
r9203 | |||
|
r13896 | 3. Writing directly to the Bugzilla database. Only Bugzilla installations | ||
|
r13801 | using MySQL are supported. Requires Python MySQLdb. | ||
|
r9203 | |||
|
r13801 | Writing directly to the database is susceptible to schema changes, and | ||
relies on a Bugzilla contrib script to send out bug change | ||||
notification emails. This script runs as the user running Mercurial, | ||||
must be run on the host with the Bugzilla install, and requires | ||||
permission to read Bugzilla configuration details and the necessary | ||||
MySQL user and password to have full access rights to the Bugzilla | ||||
database. For these reasons this access mode is now considered | ||||
deprecated, and will not be updated for new Bugzilla versions going | ||||
|
r16222 | forward. Only adding comments is supported in this access mode. | ||
|
r9203 | |||
|
r13801 | Access via XMLRPC needs a Bugzilla username and password to be specified | ||
in the configuration. Comments are added under that username. Since the | ||||
configuration must be readable by all Mercurial users, it is recommended | ||||
that the rights of that user are restricted in Bugzilla to the minimum | ||||
|
r16223 | necessary to add comments. Marking bugs fixed requires Bugzilla 4.0 and later. | ||
|
r13801 | |||
|
r13871 | Access via XMLRPC/email uses XMLRPC to query Bugzilla, but sends | ||
|
r13802 | email to the Bugzilla email interface to submit comments to bugs. | ||
The From: address in the email is set to the email address of the Mercurial | ||||
user, so the comment appears to come from the Mercurial user. In the event | ||||
that the Mercurial user email is not recognised by Bugzilla as a Bugzilla | ||||
|
r13871 | user, the email associated with the Bugzilla username used to log into | ||
|
r16223 | Bugzilla is used instead as the source of the comment. Marking bugs fixed | ||
works on all supported Bugzilla versions. | ||||
|
r13802 | |||
Configuration items common to all access modes: | ||||
|
r9203 | |||
|
r13833 | bugzilla.version | ||
|
r13883 | This access type to use. Values recognised are: | ||
|
r13871 | |||
|
r13883 | :``xmlrpc``: Bugzilla XMLRPC interface. | ||
:``xmlrpc+email``: Bugzilla XMLRPC and email interfaces. | ||||
:``3.0``: MySQL access, Bugzilla 3.0 and later. | ||||
:``2.18``: MySQL access, Bugzilla 2.18 and up to but not | ||||
including 3.0. | ||||
:``2.16``: MySQL access, Bugzilla 2.16 and up to but not | ||||
including 2.18. | ||||
|
r7985 | |||
|
r13833 | bugzilla.regexp | ||
|
r16223 | Regular expression to match bug IDs for update in changeset commit message. | ||
|
r16222 | It must contain one "()" named group ``<ids>`` containing the bug | ||
IDs separated by non-digit characters. It may also contain | ||||
a named group ``<hours>`` with a floating-point number giving the | ||||
hours worked on the bug. If no named groups are present, the first | ||||
"()" group is assumed to contain the bug IDs, and work time is not | ||||
updated. The default expression matches ``Bug 1234``, ``Bug no. 1234``, | ||||
``Bug number 1234``, ``Bugs 1234,5678``, ``Bug 1234 and 5678`` and | ||||
variations thereof, followed by an hours number prefixed by ``h`` or | ||||
``hours``, e.g. ``hours 1.5``. Matching is case insensitive. | ||||
|
r9203 | |||
|
r16223 | bugzilla.fixregexp | ||
Regular expression to match bug IDs for marking fixed in changeset | ||||
commit message. This must contain a "()" named group ``<ids>` containing | ||||
the bug IDs separated by non-digit characters. It may also contain | ||||
a named group ``<hours>`` with a floating-point number giving the | ||||
hours worked on the bug. If no named groups are present, the first | ||||
"()" group is assumed to contain the bug IDs, and work time is not | ||||
updated. The default expression matches ``Fixes 1234``, ``Fixes bug 1234``, | ||||
``Fixes bugs 1234,5678``, ``Fixes 1234 and 5678`` and | ||||
variations thereof, followed by an hours number prefixed by ``h`` or | ||||
``hours``, e.g. ``hours 1.5``. Matching is case insensitive. | ||||
bugzilla.fixstatus | ||||
The status to set a bug to when marking fixed. Default ``RESOLVED``. | ||||
bugzilla.fixresolution | ||||
The resolution to set a bug to when marking fixed. Default ``FIXED``. | ||||
|
r9203 | |||
|
r13833 | bugzilla.style | ||
|
r9203 | The style file to use when formatting comments. | ||
|
r13833 | bugzilla.template | ||
|
r9252 | Template to use when formatting comments. Overrides style if | ||
specified. In addition to the usual Mercurial keywords, the | ||||
|
r13884 | extension specifies: | ||
|
r9203 | |||
|
r13884 | :``{bug}``: The Bugzilla bug ID. | ||
:``{root}``: The full pathname of the Mercurial repository. | ||||
:``{webroot}``: Stripped pathname of the Mercurial repository. | ||||
:``{hgweb}``: Base URL for browsing Mercurial repositories. | ||||
|
r7504 | |||
|
r13842 | Default ``changeset {node|short} in repo {root} refers to bug | ||
{bug}.\\ndetails:\\n\\t{desc|tabindent}`` | ||||
|
r9203 | |||
|
r13833 | bugzilla.strip | ||
|
r13841 | The number of path separator characters to strip from the front of | ||
the Mercurial repository path (``{root}`` in templates) to produce | ||||
``{webroot}``. For example, a repository with ``{root}`` | ||||
``/var/local/my-project`` with a strip of 2 gives a value for | ||||
``{webroot}`` of ``my-project``. Default 0. | ||||
|
r13801 | |||
|
r13833 | web.baseurl | ||
|
r13801 | Base URL for browsing Mercurial repositories. Referenced from | ||
|
r13896 | templates as ``{hgweb}``. | ||
|
r13801 | |||
|
r13802 | Configuration items common to XMLRPC+email and MySQL access modes: | ||
|
r13833 | bugzilla.usermap | ||
|
r13802 | Path of file containing Mercurial committer email to Bugzilla user email | ||
mappings. If specified, the file should contain one mapping per | ||||
|
r13835 | line:: | ||
committer = Bugzilla user | ||||
|
r13896 | See also the ``[usermap]`` section. | ||
|
r13802 | |||
|
r13836 | The ``[usermap]`` section is used to specify mappings of Mercurial | ||
|
r13834 | committer email to Bugzilla user email. See also ``bugzilla.usermap``. | ||
|
r13835 | Contains entries of the form ``committer = Bugzilla user``. | ||
|
r13802 | |||
|
r13801 | XMLRPC access mode configuration: | ||
|
r13833 | bugzilla.bzurl | ||
|
r13801 | The base URL for the Bugzilla installation. | ||
|
r13841 | Default ``http://localhost/bugzilla``. | ||
|
r13801 | |||
|
r13833 | bugzilla.user | ||
|
r13841 | The username to use to log into Bugzilla via XMLRPC. Default | ||
``bugs``. | ||||
|
r13801 | |||
|
r13833 | bugzilla.password | ||
|
r13801 | The password for Bugzilla login. | ||
|
r13802 | XMLRPC+email access mode uses the XMLRPC access mode configuration items, | ||
and also: | ||||
|
r13833 | bugzilla.bzemail | ||
|
r13802 | The Bugzilla email address. | ||
In addition, the Mercurial email settings must be configured. See the | ||||
|
r13837 | documentation in hgrc(5), sections ``[email]`` and ``[smtp]``. | ||
|
r13802 | |||
|
r13801 | MySQL access mode configuration: | ||
|
r13833 | bugzilla.host | ||
|
r13801 | Hostname of the MySQL server holding the Bugzilla database. | ||
|
r13841 | Default ``localhost``. | ||
|
r13801 | |||
|
r13833 | bugzilla.db | ||
|
r13841 | Name of the Bugzilla database in MySQL. Default ``bugs``. | ||
|
r13801 | |||
|
r13833 | bugzilla.user | ||
|
r13841 | Username to use to access MySQL server. Default ``bugs``. | ||
|
r13801 | |||
|
r13833 | bugzilla.password | ||
|
r13801 | Password to use to access MySQL server. | ||
|
r13833 | bugzilla.timeout | ||
|
r13801 | Database connection timeout (seconds). Default 5. | ||
|
r13833 | bugzilla.bzuser | ||
|
r13801 | Fallback Bugzilla user name to record comments with, if changeset | ||
committer cannot be found as a Bugzilla user. | ||||
|
r13833 | bugzilla.bzdir | ||
|
r13801 | Bugzilla install directory. Used by default notify. Default | ||
|
r13841 | ``/var/www/html/bugzilla``. | ||
|
r13801 | |||
|
r13833 | bugzilla.notify | ||
|
r13801 | The command to run to get Bugzilla to send bug change notification | ||
|
r13841 | emails. Substitutes from a map with 3 keys, ``bzdir``, ``id`` (bug | ||
id) and ``user`` (committer bugzilla email). Default depends on | ||||
version; from 2.18 it is "cd %(bzdir)s && perl -T | ||||
contrib/sendbugmail.pl %(id)s %(user)s". | ||||
|
r7504 | |||
|
r9203 | Activating the extension:: | ||
|
r7504 | |||
[extensions] | ||||
|
r10112 | bugzilla = | ||
|
r7504 | |||
[hooks] | ||||
# run bugzilla hook on every change pulled or pushed in here | ||||
incoming.bugzilla = python:hgext.bugzilla.hook | ||||
|
r13801 | Example configurations: | ||
XMLRPC example configuration. This uses the Bugzilla at | ||||
|
r13841 | ``http://my-project.org/bugzilla``, logging in as user | ||
``bugmail@my-project.org`` with password ``plugh``. It is used with a | ||||
|
r13870 | collection of Mercurial repositories in ``/var/local/hg/repos/``, | ||
with a web interface at ``http://my-project.org/hg``. :: | ||||
|
r7504 | |||
|
r13801 | [bugzilla] | ||
bzurl=http://my-project.org/bugzilla | ||||
user=bugmail@my-project.org | ||||
password=plugh | ||||
version=xmlrpc | ||||
|
r13870 | template=Changeset {node|short} in {root|basename}. | ||
{hgweb}/{webroot}/rev/{node|short}\\n | ||||
{desc}\\n | ||||
strip=5 | ||||
|
r13801 | |||
[web] | ||||
baseurl=http://my-project.org/hg | ||||
|
r13802 | XMLRPC+email example configuration. This uses the Bugzilla at | ||
|
r13841 | ``http://my-project.org/bugzilla``, logging in as user | ||
|
r14619 | ``bugmail@my-project.org`` with password ``plugh``. It is used with a | ||
|
r13870 | collection of Mercurial repositories in ``/var/local/hg/repos/``, | ||
with a web interface at ``http://my-project.org/hg``. Bug comments | ||||
are sent to the Bugzilla email address | ||||
|
r13854 | ``bugzilla@my-project.org``. :: | ||
|
r13802 | |||
[bugzilla] | ||||
|
r13870 | bzurl=http://my-project.org/bugzilla | ||
|
r13802 | user=bugmail@my-project.org | ||
password=plugh | ||||
version=xmlrpc | ||||
bzemail=bugzilla@my-project.org | ||||
|
r13870 | template=Changeset {node|short} in {root|basename}. | ||
{hgweb}/{webroot}/rev/{node|short}\\n | ||||
{desc}\\n | ||||
strip=5 | ||||
|
r13802 | |||
[web] | ||||
|
r13870 | baseurl=http://my-project.org/hg | ||
[usermap] | ||||
user@emaildomain.com=user.name@bugzilladomain.com | ||||
|
r13802 | |||
|
r13870 | MySQL example configuration. This has a local Bugzilla 3.2 installation | ||
in ``/opt/bugzilla-3.2``. The MySQL database is on ``localhost``, | ||||
the Bugzilla database name is ``bugs`` and MySQL is | ||||
accessed with MySQL username ``bugs`` password ``XYZZY``. It is used | ||||
with a collection of Mercurial repositories in ``/var/local/hg/repos/``, | ||||
with a web interface at ``http://my-project.org/hg``. :: | ||||
|
r7504 | |||
[bugzilla] | ||||
host=localhost | ||||
password=XYZZY | ||||
version=3.0 | ||||
bzuser=unknown@domain.com | ||||
|
r7618 | bzdir=/opt/bugzilla-3.2 | ||
|
r9204 | template=Changeset {node|short} in {root|basename}. | ||
{hgweb}/{webroot}/rev/{node|short}\\n | ||||
{desc}\\n | ||||
|
r7504 | strip=5 | ||
[web] | ||||
|
r13870 | baseurl=http://my-project.org/hg | ||
|
r7504 | |||
[usermap] | ||||
user@emaildomain.com=user.name@bugzilladomain.com | ||||
|
r13802 | All the above add a comment to the Bugzilla bug record of the form:: | ||
|
r7504 | |||
Changeset 3b16791d6642 in repository-name. | ||||
|
r13870 | http://my-project.org/hg/repository-name/rev/3b16791d6642 | ||
|
r7504 | |||
Changeset commit comment. Bug 1234. | ||||
''' | ||||
|
r2192 | |||
|
r3891 | from mercurial.i18n import _ | ||
|
r6211 | from mercurial.node import short | ||
|
r13802 | from mercurial import cmdutil, mail, templater, util | ||
|
r15870 | import re, time, urlparse, xmlrpclib | ||
|
r2192 | |||
|
r13800 | class bzaccess(object): | ||
'''Base class for access to Bugzilla.''' | ||||
|
r2192 | |||
def __init__(self, ui): | ||||
self.ui = ui | ||||
|
r13800 | usermap = self.ui.config('bugzilla', 'usermap') | ||
if usermap: | ||||
self.ui.readconfig(usermap, sections=['usermap']) | ||||
def map_committer(self, user): | ||||
'''map name of committer to Bugzilla user name.''' | ||||
for committer, bzuser in self.ui.configitems('usermap'): | ||||
if committer.lower() == user.lower(): | ||||
return bzuser | ||||
return user | ||||
# Methods to be implemented by access classes. | ||||
|
r16221 | # | ||
# 'bugs' is a dict keyed on bug id, where values are a dict holding | ||||
|
r16222 | # updates to bug state. Recognised dict keys are: | ||
# | ||||
# 'hours': Value, float containing work hours to be updated. | ||||
|
r16223 | # 'fix': If key present, bug is to be marked fixed. Value ignored. | ||
|
r16222 | |||
|
r16221 | def filter_real_bug_ids(self, bugs): | ||
'''remove bug IDs that do not exist in Bugzilla from bugs.''' | ||||
|
r13800 | pass | ||
|
r16221 | def filter_cset_known_bug_ids(self, node, bugs): | ||
'''remove bug IDs where node occurs in comment text from bugs.''' | ||||
|
r13800 | pass | ||
|
r16221 | def updatebug(self, bugid, newstate, text, committer): | ||
'''update the specified bug. Add comment text and set new states. | ||||
|
r13800 | |||
If possible add the comment as being from the committer of | ||||
the changeset. Otherwise use the default Bugzilla user. | ||||
''' | ||||
pass | ||||
|
r16221 | def notify(self, bugs, committer): | ||
'''Force sending of Bugzilla notification emails. | ||||
Only required if the access method does not trigger notification | ||||
emails automatically. | ||||
''' | ||||
|
r13800 | pass | ||
# Bugzilla via direct access to MySQL database. | ||||
class bzmysql(bzaccess): | ||||
'''Support for direct MySQL access to Bugzilla. | ||||
The earliest Bugzilla version this is tested with is version 2.16. | ||||
|
r13801 | |||
|
r16226 | If your Bugzilla is version 3.4 or above, you are strongly | ||
|
r13801 | recommended to use the XMLRPC access method instead. | ||
|
r13800 | ''' | ||
@staticmethod | ||||
def sql_buglist(ids): | ||||
'''return SQL-friendly list of bug ids''' | ||||
return '(' + ','.join(map(str, ids)) + ')' | ||||
_MySQLdb = None | ||||
def __init__(self, ui): | ||||
try: | ||||
import MySQLdb as mysql | ||||
bzmysql._MySQLdb = mysql | ||||
except ImportError, err: | ||||
raise util.Abort(_('python mysql support not available: %s') % err) | ||||
bzaccess.__init__(self, ui) | ||||
|
r2192 | host = self.ui.config('bugzilla', 'host', 'localhost') | ||
user = self.ui.config('bugzilla', 'user', 'bugs') | ||||
passwd = self.ui.config('bugzilla', 'password') | ||||
db = self.ui.config('bugzilla', 'db', 'bugs') | ||||
timeout = int(self.ui.config('bugzilla', 'timeout', 5)) | ||||
self.ui.note(_('connecting to %s:%s as %s, password %s\n') % | ||||
(host, db, user, '*' * len(passwd))) | ||||
|
r13800 | self.conn = bzmysql._MySQLdb.connect(host=host, | ||
user=user, passwd=passwd, | ||||
db=db, | ||||
connect_timeout=timeout) | ||||
|
r2192 | self.cursor = self.conn.cursor() | ||
|
r7019 | self.longdesc_id = self.get_longdesc_id() | ||
|
r2192 | self.user_ids = {} | ||
|
r7618 | self.default_notify = "cd %(bzdir)s && ./processmail %(id)s %(user)s" | ||
|
r2192 | |||
def run(self, *args, **kwargs): | ||||
'''run a query.''' | ||||
self.ui.note(_('query: %s %s\n') % (args, kwargs)) | ||||
try: | ||||
self.cursor.execute(*args, **kwargs) | ||||
|
r13800 | except bzmysql._MySQLdb.MySQLError: | ||
|
r2192 | self.ui.note(_('failed query: %s %s\n') % (args, kwargs)) | ||
raise | ||||
|
r7019 | def get_longdesc_id(self): | ||
'''get identity of longdesc field''' | ||||
self.run('select fieldid from fielddefs where name = "longdesc"') | ||||
ids = self.cursor.fetchall() | ||||
if len(ids) != 1: | ||||
raise util.Abort(_('unknown database schema')) | ||||
return ids[0][0] | ||||
|
r16221 | def filter_real_bug_ids(self, bugs): | ||
'''filter not-existing bugs from set.''' | ||||
|
r13800 | self.run('select bug_id from bugs where bug_id in %s' % | ||
|
r16221 | bzmysql.sql_buglist(bugs.keys())) | ||
existing = [id for (id,) in self.cursor.fetchall()] | ||||
for id in bugs.keys(): | ||||
if id not in existing: | ||||
self.ui.status(_('bug %d does not exist\n') % id) | ||||
del bugs[id] | ||||
|
r2192 | |||
|
r16221 | def filter_cset_known_bug_ids(self, node, bugs): | ||
|
r13799 | '''filter bug ids that already refer to this changeset from set.''' | ||
|
r2192 | self.run('''select bug_id from longdescs where | ||
bug_id in %s and thetext like "%%%s%%"''' % | ||||
|
r16221 | (bzmysql.sql_buglist(bugs.keys()), short(node))) | ||
|
r2192 | for (id,) in self.cursor.fetchall(): | ||
self.ui.status(_('bug %d already knows about changeset %s\n') % | ||||
(id, short(node))) | ||||
|
r16221 | del bugs[id] | ||
|
r2192 | |||
|
r16221 | def notify(self, bugs, committer): | ||
|
r2192 | '''tell bugzilla to send mail.''' | ||
self.ui.status(_('telling bugzilla to send mail:\n')) | ||||
|
r7618 | (user, userid) = self.get_bugzilla_user(committer) | ||
|
r16221 | for id in bugs.keys(): | ||
|
r2192 | self.ui.status(_(' bug %s\n') % id) | ||
|
r7618 | cmdfmt = self.ui.config('bugzilla', 'notify', self.default_notify) | ||
bzdir = self.ui.config('bugzilla', 'bzdir', '/var/www/html/bugzilla') | ||||
try: | ||||
# Backwards-compatible with old notify string, which | ||||
# took one string. This will throw with a new format | ||||
# string. | ||||
cmd = cmdfmt % id | ||||
except TypeError: | ||||
cmd = cmdfmt % {'bzdir': bzdir, 'id': id, 'user': user} | ||||
self.ui.note(_('running notify command %s\n') % cmd) | ||||
|
r6548 | fp = util.popen('(%s) 2>&1' % cmd) | ||
|
r2192 | out = fp.read() | ||
ret = fp.close() | ||||
if ret: | ||||
self.ui.warn(out) | ||||
raise util.Abort(_('bugzilla notify command %s') % | ||||
|
r14234 | util.explainexit(ret)[0]) | ||
|
r2192 | self.ui.status(_('done\n')) | ||
def get_user_id(self, user): | ||||
'''look up numeric bugzilla user id.''' | ||||
try: | ||||
return self.user_ids[user] | ||||
except KeyError: | ||||
try: | ||||
userid = int(user) | ||||
except ValueError: | ||||
self.ui.note(_('looking up user %s\n') % user) | ||||
self.run('''select userid from profiles | ||||
where login_name like %s''', user) | ||||
all = self.cursor.fetchall() | ||||
if len(all) != 1: | ||||
raise KeyError(user) | ||||
userid = int(all[0][0]) | ||||
self.user_ids[user] = userid | ||||
return userid | ||||
|
r7618 | def get_bugzilla_user(self, committer): | ||
|
r13801 | '''See if committer is a registered bugzilla user. Return | ||
|
r7618 | bugzilla username and userid if so. If not, return default | ||
bugzilla username and userid.''' | ||||
|
r2306 | user = self.map_committer(committer) | ||
|
r2192 | try: | ||
|
r2306 | userid = self.get_user_id(user) | ||
|
r2192 | except KeyError: | ||
try: | ||||
defaultuser = self.ui.config('bugzilla', 'bzuser') | ||||
|
r2306 | if not defaultuser: | ||
raise util.Abort(_('cannot find bugzilla user id for %s') % | ||||
user) | ||||
|
r2192 | userid = self.get_user_id(defaultuser) | ||
|
r7618 | user = defaultuser | ||
|
r2192 | except KeyError: | ||
|
r2306 | raise util.Abort(_('cannot find bugzilla user id for %s or %s') % | ||
(user, defaultuser)) | ||||
|
r7618 | return (user, userid) | ||
|
r16221 | def updatebug(self, bugid, newstate, text, committer): | ||
'''update bug state with comment text. | ||||
Try adding comment as committer of changeset, otherwise as | ||||
default bugzilla user.''' | ||||
|
r16222 | if len(newstate) > 0: | ||
self.ui.warn(_("Bugzilla/MySQL cannot update bug state\n")) | ||||
|
r7618 | (user, userid) = self.get_bugzilla_user(committer) | ||
|
r2192 | now = time.strftime('%Y-%m-%d %H:%M:%S') | ||
self.run('''insert into longdescs | ||||
(bug_id, who, bug_when, thetext) | ||||
values (%s, %s, %s, %s)''', | ||||
(bugid, userid, now, text)) | ||||
self.run('''insert into bugs_activity (bug_id, who, bug_when, fieldid) | ||||
values (%s, %s, %s, %s)''', | ||||
(bugid, userid, now, self.longdesc_id)) | ||||
|
r7493 | self.conn.commit() | ||
|
r2192 | |||
|
r13800 | class bzmysql_2_18(bzmysql): | ||
|
r7618 | '''support for bugzilla 2.18 series.''' | ||
def __init__(self, ui): | ||||
|
r13800 | bzmysql.__init__(self, ui) | ||
|
r10282 | self.default_notify = \ | ||
"cd %(bzdir)s && perl -T contrib/sendbugmail.pl %(id)s %(user)s" | ||||
|
r7618 | |||
|
r13800 | class bzmysql_3_0(bzmysql_2_18): | ||
|
r7019 | '''support for bugzilla 3.0 series.''' | ||
def __init__(self, ui): | ||||
|
r13800 | bzmysql_2_18.__init__(self, ui) | ||
|
r7019 | |||
def get_longdesc_id(self): | ||||
'''get identity of longdesc field''' | ||||
self.run('select id from fielddefs where name = "longdesc"') | ||||
ids = self.cursor.fetchall() | ||||
if len(ids) != 1: | ||||
raise util.Abort(_('unknown database schema')) | ||||
return ids[0][0] | ||||
|
r13801 | # Buzgilla via XMLRPC interface. | ||
|
r15870 | class cookietransportrequest(object): | ||
"""A Transport request method that retains cookies over its lifetime. | ||||
|
r13801 | |||
The regular xmlrpclib transports ignore cookies. Which causes | ||||
a bit of a problem when you need a cookie-based login, as with | ||||
the Bugzilla XMLRPC interface. | ||||
|
r15870 | So this is a helper for defining a Transport which looks for | ||
cookies being set in responses and saves them to add to all future | ||||
requests. | ||||
|
r13801 | """ | ||
# Inspiration drawn from | ||||
# http://blog.godson.in/2010/09/how-to-make-python-xmlrpclib-client.html | ||||
# http://www.itkovian.net/base/transport-class-for-pythons-xml-rpc-lib/ | ||||
cookies = [] | ||||
def send_cookies(self, connection): | ||||
if self.cookies: | ||||
for cookie in self.cookies: | ||||
connection.putheader("Cookie", cookie) | ||||
def request(self, host, handler, request_body, verbose=0): | ||||
self.verbose = verbose | ||||
|
r16193 | self.accept_gzip_encoding = False | ||
|
r13801 | |||
# issue XML-RPC request | ||||
h = self.make_connection(host) | ||||
if verbose: | ||||
h.set_debuglevel(1) | ||||
self.send_request(h, handler, request_body) | ||||
self.send_host(h, host) | ||||
self.send_cookies(h) | ||||
self.send_user_agent(h) | ||||
self.send_content(h, request_body) | ||||
# Deal with differences between Python 2.4-2.6 and 2.7. | ||||
# In the former h is a HTTP(S). In the latter it's a | ||||
# HTTP(S)Connection. Luckily, the 2.4-2.6 implementation of | ||||
# HTTP(S) has an underlying HTTP(S)Connection, so extract | ||||
# that and use it. | ||||
try: | ||||
response = h.getresponse() | ||||
except AttributeError: | ||||
response = h._conn.getresponse() | ||||
# Add any cookie definitions to our list. | ||||
for header in response.msg.getallmatchingheaders("Set-Cookie"): | ||||
val = header.split(": ", 1)[1] | ||||
cookie = val.split(";", 1)[0] | ||||
self.cookies.append(cookie) | ||||
if response.status != 200: | ||||
raise xmlrpclib.ProtocolError(host + handler, response.status, | ||||
response.reason, response.msg.headers) | ||||
payload = response.read() | ||||
parser, unmarshaller = self.getparser() | ||||
parser.feed(payload) | ||||
parser.close() | ||||
return unmarshaller.close() | ||||
|
r15870 | # The explicit calls to the underlying xmlrpclib __init__() methods are | ||
# necessary. The xmlrpclib.Transport classes are old-style classes, and | ||||
# it turns out their __init__() doesn't get called when doing multiple | ||||
# inheritance with a new-style class. | ||||
class cookietransport(cookietransportrequest, xmlrpclib.Transport): | ||||
def __init__(self, use_datetime=0): | ||||
|
r16649 | if util.safehasattr(xmlrpclib.Transport, "__init__"): | ||
xmlrpclib.Transport.__init__(self, use_datetime) | ||||
|
r15870 | |||
class cookiesafetransport(cookietransportrequest, xmlrpclib.SafeTransport): | ||||
def __init__(self, use_datetime=0): | ||||
|
r16649 | if util.safehasattr(xmlrpclib.Transport, "__init__"): | ||
xmlrpclib.SafeTransport.__init__(self, use_datetime) | ||||
|
r15870 | |||
|
r13801 | class bzxmlrpc(bzaccess): | ||
"""Support for access to Bugzilla via the Bugzilla XMLRPC API. | ||||
Requires a minimum Bugzilla version 3.4. | ||||
""" | ||||
def __init__(self, ui): | ||||
bzaccess.__init__(self, ui) | ||||
bzweb = self.ui.config('bugzilla', 'bzurl', | ||||
'http://localhost/bugzilla/') | ||||
bzweb = bzweb.rstrip("/") + "/xmlrpc.cgi" | ||||
user = self.ui.config('bugzilla', 'user', 'bugs') | ||||
passwd = self.ui.config('bugzilla', 'password') | ||||
|
r16223 | self.fixstatus = self.ui.config('bugzilla', 'fixstatus', 'RESOLVED') | ||
self.fixresolution = self.ui.config('bugzilla', 'fixresolution', | ||||
'FIXED') | ||||
|
r15870 | self.bzproxy = xmlrpclib.ServerProxy(bzweb, self.transport(bzweb)) | ||
|
r16224 | ver = self.bzproxy.Bugzilla.version()['version'].split('.') | ||
self.bzvermajor = int(ver[0]) | ||||
self.bzverminor = int(ver[1]) | ||||
|
r13801 | self.bzproxy.User.login(dict(login=user, password=passwd)) | ||
|
r15870 | def transport(self, uri): | ||
if urlparse.urlparse(uri, "http")[0] == "https": | ||||
return cookiesafetransport() | ||||
else: | ||||
return cookietransport() | ||||
|
r13801 | def get_bug_comments(self, id): | ||
"""Return a string with all comment text for a bug.""" | ||||
|
r16221 | c = self.bzproxy.Bug.comments(dict(ids=[id], include_fields=['text'])) | ||
|
r13801 | return ''.join([t['text'] for t in c['bugs'][str(id)]['comments']]) | ||
|
r16221 | def filter_real_bug_ids(self, bugs): | ||
probe = self.bzproxy.Bug.get(dict(ids=sorted(bugs.keys()), | ||||
include_fields=[], | ||||
permissive=True)) | ||||
for badbug in probe['faults']: | ||||
id = badbug['id'] | ||||
self.ui.status(_('bug %d does not exist\n') % id) | ||||
del bugs[id] | ||||
|
r13801 | |||
|
r16221 | def filter_cset_known_bug_ids(self, node, bugs): | ||
for id in sorted(bugs.keys()): | ||||
|
r13801 | if self.get_bug_comments(id).find(short(node)) != -1: | ||
self.ui.status(_('bug %d already knows about changeset %s\n') % | ||||
(id, short(node))) | ||||
|
r16221 | del bugs[id] | ||
|
r13801 | |||
|
r16221 | def updatebug(self, bugid, newstate, text, committer): | ||
|
r16223 | args = {} | ||
|
r16222 | if 'hours' in newstate: | ||
args['work_time'] = newstate['hours'] | ||||
|
r13801 | |||
|
r16223 | if self.bzvermajor >= 4: | ||
args['ids'] = [bugid] | ||||
args['comment'] = {'body' : text} | ||||
args['status'] = self.fixstatus | ||||
args['resolution'] = self.fixresolution | ||||
self.bzproxy.Bug.update(args) | ||||
else: | ||||
if 'fix' in newstate: | ||||
self.ui.warn(_("Bugzilla/XMLRPC needs Bugzilla 4.0 or later " | ||||
"to mark bugs fixed\n")) | ||||
args['id'] = bugid | ||||
args['comment'] = text | ||||
self.bzproxy.Bug.add_comment(args) | ||||
|
r13801 | |||
|
r13802 | class bzxmlrpcemail(bzxmlrpc): | ||
"""Read data from Bugzilla via XMLRPC, send updates via email. | ||||
Advantages of sending updates via email: | ||||
1. Comments can be added as any user, not just logged in user. | ||||
|
r16222 | 2. Bug statuses or other fields not accessible via XMLRPC can | ||
potentially be updated. | ||||
|
r16223 | There is no XMLRPC function to change bug status before Bugzilla | ||
4.0, so bugs cannot be marked fixed via XMLRPC before Bugzilla 4.0. | ||||
But bugs can be marked fixed via email from 3.4 onwards. | ||||
|
r13802 | """ | ||
|
r16224 | # The email interface changes subtly between 3.4 and 3.6. In 3.4, | ||
# in-email fields are specified as '@<fieldname> = <value>'. In | ||||
# 3.6 this becomes '@<fieldname> <value>'. And fieldname @bug_id | ||||
# in 3.4 becomes @id in 3.6. 3.6 and 4.0 both maintain backwards | ||||
# compatibility, but rather than rely on this use the new format for | ||||
# 4.0 onwards. | ||||
|
r13802 | def __init__(self, ui): | ||
bzxmlrpc.__init__(self, ui) | ||||
self.bzemail = self.ui.config('bugzilla', 'bzemail') | ||||
if not self.bzemail: | ||||
raise util.Abort(_("configuration 'bzemail' missing")) | ||||
mail.validateconfig(self.ui) | ||||
|
r16224 | def makecommandline(self, fieldname, value): | ||
if self.bzvermajor >= 4: | ||||
return "@%s %s" % (fieldname, str(value)) | ||||
else: | ||||
if fieldname == "id": | ||||
fieldname = "bug_id" | ||||
return "@%s = %s" % (fieldname, str(value)) | ||||
|
r13802 | def send_bug_modify_email(self, bugid, commands, comment, committer): | ||
'''send modification message to Bugzilla bug via email. | ||||
The message format is documented in the Bugzilla email_in.pl | ||||
specification. commands is a list of command lines, comment is the | ||||
comment text. | ||||
To stop users from crafting commit comments with | ||||
Bugzilla commands, specify the bug ID via the message body, rather | ||||
than the subject line, and leave a blank line after it. | ||||
''' | ||||
user = self.map_committer(committer) | ||||
matches = self.bzproxy.User.get(dict(match=[user])) | ||||
if not matches['users']: | ||||
user = self.ui.config('bugzilla', 'user', 'bugs') | ||||
matches = self.bzproxy.User.get(dict(match=[user])) | ||||
if not matches['users']: | ||||
raise util.Abort(_("default bugzilla user %s email not found") % | ||||
user) | ||||
user = matches['users'][0]['email'] | ||||
|
r16224 | commands.append(self.makecommandline("id", bugid)) | ||
|
r13802 | |||
|
r16224 | text = "\n".join(commands) + "\n\n" + comment | ||
|
r13802 | |||
_charsets = mail._charsets(self.ui) | ||||
user = mail.addressencode(self.ui, user, _charsets) | ||||
bzemail = mail.addressencode(self.ui, self.bzemail, _charsets) | ||||
msg = mail.mimeencode(self.ui, text, _charsets) | ||||
msg['From'] = user | ||||
msg['To'] = bzemail | ||||
msg['Subject'] = mail.headencode(self.ui, "Bug modification", _charsets) | ||||
sendmail = mail.connect(self.ui) | ||||
sendmail(user, bzemail, msg.as_string()) | ||||
|
r16221 | def updatebug(self, bugid, newstate, text, committer): | ||
|
r16222 | cmds = [] | ||
if 'hours' in newstate: | ||||
cmds.append(self.makecommandline("work_time", newstate['hours'])) | ||||
|
r16223 | if 'fix' in newstate: | ||
cmds.append(self.makecommandline("bug_status", self.fixstatus)) | ||||
cmds.append(self.makecommandline("resolution", self.fixresolution)) | ||||
|
r16222 | self.send_bug_modify_email(bugid, cmds, text, committer) | ||
|
r13802 | |||
|
r2192 | class bugzilla(object): | ||
# supported versions of bugzilla. different versions have | ||||
# different schemas. | ||||
_versions = { | ||||
|
r13800 | '2.16': bzmysql, | ||
'2.18': bzmysql_2_18, | ||||
|
r13801 | '3.0': bzmysql_3_0, | ||
|
r13802 | 'xmlrpc': bzxmlrpc, | ||
'xmlrpc+email': bzxmlrpcemail | ||||
|
r2192 | } | ||
_default_bug_re = (r'bugs?\s*,?\s*(?:#|nos?\.?|num(?:ber)?s?)?\s*' | ||||
|
r16222 | r'(?P<ids>(?:\d+\s*(?:,?\s*(?:and)?)?\s*)+)' | ||
r'\.?\s*(?:h(?:ours?)?\s*(?P<hours>\d*(?:\.\d+)?))?') | ||||
|
r2192 | |||
|
r16223 | _default_fix_re = (r'fix(?:es)?\s*(?:bugs?\s*)?,?\s*' | ||
r'(?:nos?\.?|num(?:ber)?s?)?\s*' | ||||
r'(?P<ids>(?:#?\d+\s*(?:,?\s*(?:and)?)?\s*)+)' | ||||
r'\.?\s*(?:h(?:ours?)?\s*(?P<hours>\d*(?:\.\d+)?))?') | ||||
|
r2192 | |||
_bz = None | ||||
def __init__(self, ui, repo): | ||||
self.ui = ui | ||||
self.repo = repo | ||||
def bz(self): | ||||
'''return object that knows how to talk to bugzilla version in | ||||
use.''' | ||||
if bugzilla._bz is None: | ||||
bzversion = self.ui.config('bugzilla', 'version') | ||||
try: | ||||
bzclass = bugzilla._versions[bzversion] | ||||
except KeyError: | ||||
raise util.Abort(_('bugzilla version %s not supported') % | ||||
bzversion) | ||||
bugzilla._bz = bzclass(self.ui) | ||||
return bugzilla._bz | ||||
def __getattr__(self, key): | ||||
return getattr(self.bz(), key) | ||||
_bug_re = None | ||||
|
r16223 | _fix_re = None | ||
|
r2192 | _split_re = None | ||
|
r16221 | def find_bugs(self, ctx): | ||
'''return bugs dictionary created from commit comment. | ||||
|
r2192 | |||
|
r16221 | Extract bug info from changeset comments. Filter out any that are | ||
|
r13799 | not known to Bugzilla, and any that already have a reference to | ||
the given changeset in their comments. | ||||
''' | ||||
|
r2192 | if bugzilla._bug_re is None: | ||
bugzilla._bug_re = re.compile( | ||||
|
r16223 | self.ui.config('bugzilla', 'regexp', | ||
bugzilla._default_bug_re), re.IGNORECASE) | ||||
bugzilla._fix_re = re.compile( | ||||
self.ui.config('bugzilla', 'fixregexp', | ||||
bugzilla._default_fix_re), re.IGNORECASE) | ||||
|
r2192 | bugzilla._split_re = re.compile(r'\D+') | ||
start = 0 | ||||
|
r16222 | hours = 0.0 | ||
|
r16221 | bugs = {} | ||
|
r16223 | bugmatch = bugzilla._bug_re.search(ctx.description(), start) | ||
fixmatch = bugzilla._fix_re.search(ctx.description(), start) | ||||
|
r2192 | while True: | ||
|
r16222 | bugattribs = {} | ||
|
r16223 | if not bugmatch and not fixmatch: | ||
|
r2192 | break | ||
|
r16223 | if not bugmatch: | ||
m = fixmatch | ||||
elif not fixmatch: | ||||
m = bugmatch | ||||
else: | ||||
if bugmatch.start() < fixmatch.start(): | ||||
m = bugmatch | ||||
else: | ||||
m = fixmatch | ||||
|
r2192 | start = m.end() | ||
|
r16223 | if m is bugmatch: | ||
bugmatch = bugzilla._bug_re.search(ctx.description(), start) | ||||
if 'fix' in bugattribs: | ||||
del bugattribs['fix'] | ||||
else: | ||||
fixmatch = bugzilla._fix_re.search(ctx.description(), start) | ||||
bugattribs['fix'] = None | ||||
|
r16222 | try: | ||
ids = m.group('ids') | ||||
except IndexError: | ||||
ids = m.group(1) | ||||
try: | ||||
hours = float(m.group('hours')) | ||||
bugattribs['hours'] = hours | ||||
except IndexError: | ||||
pass | ||||
except TypeError: | ||||
pass | ||||
except ValueError: | ||||
self.ui.status(_("%s: invalid hours\n") % m.group('hours')) | ||||
for id in bugzilla._split_re.split(ids): | ||||
|
r10282 | if not id: | ||
continue | ||||
|
r16222 | bugs[int(id)] = bugattribs | ||
|
r16221 | if bugs: | ||
self.filter_real_bug_ids(bugs) | ||||
if bugs: | ||||
self.filter_cset_known_bug_ids(ctx.node(), bugs) | ||||
return bugs | ||||
|
r2192 | |||
|
r16221 | def update(self, bugid, newstate, ctx): | ||
|
r2192 | '''update bugzilla bug with reference to changeset.''' | ||
def webroot(root): | ||||
'''strip leading prefix of repo root and turn into | ||||
url-safe path.''' | ||||
count = int(self.ui.config('bugzilla', 'strip', 0)) | ||||
root = util.pconvert(root) | ||||
while count > 0: | ||||
c = root.find('/') | ||||
if c == -1: | ||||
break | ||||
|
r10282 | root = root[c + 1:] | ||
|
r2192 | count -= 1 | ||
return root | ||||
mapfile = self.ui.config('bugzilla', 'style') | ||||
tmpl = self.ui.config('bugzilla', 'template') | ||||
|
r3741 | t = cmdutil.changeset_templater(self.ui, self.repo, | ||
|
r7762 | False, None, mapfile, False) | ||
|
r2192 | if not mapfile and not tmpl: | ||
tmpl = _('changeset {node|short} in repo {root} refers ' | ||||
'to bug {bug}.\ndetails:\n\t{desc|tabindent}') | ||||
if tmpl: | ||||
tmpl = templater.parsestring(tmpl, quoted=False) | ||||
t.use_template(tmpl) | ||||
|
r3741 | self.ui.pushbuffer() | ||
|
r7369 | t.show(ctx, changes=ctx.changeset(), | ||
|
r2192 | bug=str(bugid), | ||
|
r2197 | hgweb=self.ui.config('web', 'baseurl'), | ||
|
r2192 | root=self.repo.root, | ||
webroot=webroot(self.repo.root)) | ||||
|
r3741 | data = self.ui.popbuffer() | ||
|
r16221 | self.updatebug(bugid, newstate, data, util.email(ctx.user())) | ||
|
r2192 | |||
def hook(ui, repo, hooktype, node=None, **kwargs): | ||||
'''add comment to bugzilla for each changeset that refers to a | ||||
bugzilla bug id. only add a comment once per bug, so same change | ||||
seen multiple times does not fill bug with duplicate data.''' | ||||
if node is None: | ||||
raise util.Abort(_('hook type %s does not pass a changeset id') % | ||||
hooktype) | ||||
try: | ||||
bz = bugzilla(ui, repo) | ||||
|
r6747 | ctx = repo[node] | ||
|
r16221 | bugs = bz.find_bugs(ctx) | ||
if bugs: | ||||
for bug in bugs: | ||||
bz.update(bug, bugs[bug], ctx) | ||||
bz.notify(bugs, util.email(ctx.user())) | ||||
|
r13800 | except Exception, e: | ||
raise util.Abort(_('Bugzilla error: %s') % e) | ||||
|
r2192 | |||