Edgewall Software

ChristianBoos: mantis_tickets_link.py

File mantis_tickets_link.py, 1.9 KB (added by techtonik <techtonik@…>, 4 years ago)

Simple plugin to redirect ticket references in commits to another ticket system (here, Mantis). Simple standalone plugin for 0.11, simply copy that file in your $TRAC_ENV/plugins directory.

Line 
1# -- mantis_tickets_link.py
2#
3# Author: Christian Boos <cboos@bct-technology.com>
4# Modified: techtonik <techtonik@rainforce.org>
5# Date:   2008-05-29
6# License: Trac (modified BSD)
7# ----------------------------------------------------------------------------
8
9"""Link tickets referenced in commits to external Mantis bugtracker"""
10
11from trac.core import *
12from trac.wiki import IWikiSyntaxProvider, Formatter
13
14MANTIS_FMT = "http://bugs.yourdomain.com/view.php?id=%d"
15
16class MantisTicketSyntaxProvider(Component):
17    """Link references like "Mantis #xxxxxx" in commits directly to Mantis"""
18
19    implements(IWikiSyntaxProvider)
20
21    def get_wiki_syntax(self):
22        """Return an iterable that provides additional wiki syntax.
23
24        Additional wiki syntax correspond to a pair of (regexp, cb),
25        the `regexp` for the additional syntax and the callback `cb`
26        which will be called if there's a match.
27        That function is of the form cb(formatter, ns, match).
28        """       
29        yield (
30            r"(?i)(?P<mbt_label>mantis *#)(?P<mbt_number>\d+)",
31            lambda x, y, z: self._format_link(x, y, z.group("mbt_number"), z.group("mbt_label")))
32
33    def get_link_resolvers(self):
34        """Return an iterable over (namespace, formatter) tuples.
35
36        Each formatter should be a function of the form
37        fmt(formatter, ns, target, label), and should
38        return some HTML fragment.
39        The `label` is already HTML escaped, whereas the `target` is not.
40        """
41        return [('mantis', self._format_link)]
42
43    def _format_link(self, formatter, ns, target, label, fullmatch=None):
44        try:
45            number = int(target)
46            href = MANTIS_FMT % number
47            text = target == label and "#%07d" % number or "%s%07d" % (label, number)
48            return formatter._make_ext_link(href, text, 'Mantis Bug #%d' % number)
49        except ValueError:
50            return label