3
# Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
# This software may be used and distributed according to the terms
19
# of the GNU General Public License, incorporated herein by reference.
21
"""Bazaar plugin for closing fixed bugs in a trac database."""
23
from bzrlib.commands import Command, register_command
26
class cmd_close_trac_bugs(Command):
27
"""Close trac bugs marked as closed in this branch.
29
bugtracker should be the name of a bugtracker configured in your
30
Bazaar configuration, e.g. 'bitlbee' in the case you have:
32
trac_bitlbee_url = http://bugs.bitlbee.org/bitlbee/
34
trac_env should be the path to your local trac environment.
36
location should refer to a Bazaar branch but defaults to the current
40
takes_args = ["bugtracker", "trac_env", "location?"]
42
def run(self, bugtracker, trac_env, location=None):
43
from bzrlib.branch import Branch
44
from bzrlib.errors import BzrCommandError
45
from trac.env import Environment
46
from trac.ticket import Ticket
50
b = Branch.open(location)
51
from bzrlib.bugtracker import tracker_registry
52
trac_bt = tracker_registry.get("trac")
54
bt = trac_bt.get(bugtracker, b)
56
raise BzrCommandError("No such trac bugtracker '%s' in configuration." %
58
env = Environment(trac_env)
59
db_cnx = env.get_db_cnx()
61
revids = [r for r in b.repository.get_ancestry(b.last_revision()) if r]
62
for i, rev in enumerate(b.repository.get_revisions(revids)):
63
if not 'bugs' in rev.properties:
65
for line in rev.properties['bugs'].splitlines():
66
(url, resolution) = line.rsplit(" ", 1)
67
if resolution != "fixed":
69
if not url.startswith(bt._base_url):
71
# FIXME: Use bzrlib.bugtracker to extract the id
72
bugid = int(url.rsplit("/")[-1])
73
ticket = Ticket(env, bugid)
74
if ticket['resolution'] == 'fixed':
75
# Don't attempt to close again
77
ticket['resolution'] = 'fixed'
78
ticket['status'] = 'close'
79
revno = b.revision_id_to_dotted_revno(rev.revision_id)
80
ticket.save_changes(author=rev.committer,
81
comment="Closed in revision %s." % revno)
84
register_command(cmd_close_trac_bugs)