Edgewall Software

ChristianBoos: mantis_tickets.py

File mantis_tickets.py, 1.6 kB (added by cboos, 2 years ago)

Simple plugin that can be used to redirect ticket references to another ticket system (here, Mantis), if Trac's builtin ticket system has been disabled. Simple standalone plugin for 0.10, simply copy that file in your $TRAC_ENV/plugins directory.

Line 
1# -- wiki.py
2#
3# Author: Christian Boos <cboos@bct-technology.com>
4# Date:   2005-09-26
5# License: modified BSD (same as Trac)
6# ----------------------------------------------------------------------------
7
8"""Ticket links to the Mantis BTS"""
9
10from trac.core import *
11from trac.wiki import IWikiSyntaxProvider, Formatter
12
13MANTIS_FMT = "https://your.company.com/bugtracker/view.php?id=%d"
14
15class MantisTicketSyntaxProvider(Component):
16    """Link for tickets directly go to mantis BTS."""
17
18    implements(IWikiSyntaxProvider)
19
20    def get_link_resolvers(self):
21        return [('bug', self._format_link),
22                ('ticket', self._format_link),
23                ('mantis', self._format_link)]
24
25    def get_wiki_syntax(self):
26        yield (
27            # matches #... but not &#... (HTML entity)
28            r"!?(?<!&)#"
29            # optional intertrac shorthand #T... + digits
30            r"(?P<it_mantis>%s)\d+" % Formatter.INTERTRAC_SCHEME,
31            lambda x, y, z: self._format_link(x, 'mantis', y[1:], y, z))
32
33    def _format_link(self, formatter, ns, target, label, fullmatch=None):
34        intertrac = formatter.shorthand_intertrac_helper(ns, target, label,
35                                                         fullmatch)
36        if intertrac:
37            return intertrac
38        try:
39            number = int(target)
40            href = MANTIS_FMT % number
41            text = target == label and "#%07d" % number or label
42            return formatter._make_ext_link(href, text, 'Mantis Bug ' + text)
43        except ValueError:
44            return label