~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bugtracker.py

Major code cleanup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2010 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
from bzrlib import registry
 
18
from bzrlib.lazy_import import lazy_import
 
19
lazy_import(globals(), """
 
20
from bzrlib import errors, urlutils
 
21
""")
 
22
 
 
23
 
 
24
"""Provides a shorthand for referring to bugs on a variety of bug trackers.
 
25
 
 
26
'commit --fixes' stores references to bugs as a <bug_url> -> <bug_status>
 
27
mapping in the properties for that revision.
 
28
 
 
29
However, it's inconvenient to type out full URLs for bugs on the command line,
 
30
particularly given that many users will be using only a single bug tracker per
 
31
branch.
 
32
 
 
33
Thus, this module provides a registry of types of bug tracker (e.g. Launchpad,
 
34
Trac). Given an abbreviated name (e.g. 'lp', 'twisted') and a branch with
 
35
configuration information, these tracker types can return an instance capable
 
36
of converting bug IDs into URLs.
 
37
"""
 
38
 
 
39
 
 
40
_bugs_help = \
 
41
"""When making a commit, metadata about bugs fixed by that change can be
 
42
recorded by using the ``--fixes`` option. For each bug marked as fixed, an
 
43
entry is included in the 'bugs' revision property stating '<url> <status>'.
 
44
(The only ``status`` value currently supported is ``fixed.``)
 
45
 
 
46
The ``--fixes`` option allows you to specify a bug tracker and a bug identifier
 
47
rather than a full URL. This looks like::
 
48
 
 
49
    bzr commit --fixes <tracker>:<id>
 
50
 
 
51
where "<tracker>" is an identifier for the bug tracker, and "<id>" is the
 
52
identifier for that bug within the bugtracker, usually the bug number.
 
53
 
 
54
Bazaar knows about a few bug trackers that have many users. If
 
55
you use one of these bug trackers then there is no setup required to
 
56
use this feature, you just need to know the tracker identifier to use.
 
57
These are the bugtrackers that are built in:
 
58
 
 
59
  ============================ ============ ============
 
60
  URL                          Abbreviation Example
 
61
  ============================ ============ ============
 
62
  https://bugs.launchpad.net/  lp           lp:12345
 
63
  http://bugs.debian.org/      deb          deb:12345
 
64
  http://bugzilla.gnome.org/   gnome        gnome:12345
 
65
  ============================ ============ ============
 
66
 
 
67
For the bug trackers not listed above configuration is required.
 
68
Support for generating the URLs for any project using Bugzilla or Trac
 
69
is built in, along with a template mechanism for other bugtrackers with
 
70
simple URL schemes. If your bug tracker can't be described by one
 
71
of the schemes described below then you can write a plugin to support
 
72
it.
 
73
 
 
74
If you use Bugzilla or Trac, then you only need to set a configuration
 
75
variable which contains the base URL of the bug tracker. These options
 
76
can go into ``bazaar.conf``, ``branch.conf`` or into a branch-specific
 
77
configuration section in ``locations.conf``.  You can set up these values
 
78
for each of the projects you work on.
 
79
 
 
80
Note: As you provide a short name for each tracker, you can specify one or
 
81
more bugs in one or more trackers at commit time if you wish.
 
82
 
 
83
Launchpad
 
84
---------
 
85
 
 
86
Use ``bzr commit --fixes lp:2`` to record that this commit fixes bug 2.
 
87
 
 
88
bugzilla_<tracker>_url
 
89
----------------------
 
90
 
 
91
If present, the location of the Bugzilla bug tracker referred to by
 
92
<tracker>. This option can then be used together with ``bzr commit
 
93
--fixes`` to mark bugs in that tracker as being fixed by that commit. For
 
94
example::
 
95
 
 
96
    bugzilla_squid_url = http://bugs.squid-cache.org
 
97
 
 
98
would allow ``bzr commit --fixes squid:1234`` to mark Squid's bug 1234 as
 
99
fixed.
 
100
 
 
101
trac_<tracker>_url
 
102
------------------
 
103
 
 
104
If present, the location of the Trac instance referred to by
 
105
<tracker>. This option can then be used together with ``bzr commit
 
106
--fixes`` to mark bugs in that tracker as being fixed by that commit. For
 
107
example::
 
108
 
 
109
    trac_twisted_url = http://www.twistedmatrix.com/trac
 
110
 
 
111
would allow ``bzr commit --fixes twisted:1234`` to mark Twisted's bug 1234 as
 
112
fixed.
 
113
 
 
114
bugtracker_<tracker>_url
 
115
------------------------
 
116
 
 
117
If present, the location of a generic bug tracker instance referred to by
 
118
<tracker>. The location must contain an ``{id}`` placeholder,
 
119
which will be replaced by a specific bug ID. This option can then be used
 
120
together with ``bzr commit --fixes`` to mark bugs in that tracker as being
 
121
fixed by that commit. For example::
 
122
 
 
123
    bugtracker_python_url = http://bugs.python.org/issue{id}
 
124
 
 
125
would allow ``bzr commit --fixes python:1234`` to mark bug 1234 in Python's
 
126
Roundup bug tracker as fixed, or::
 
127
 
 
128
    bugtracker_cpan_url = http://rt.cpan.org/Public/Bug/Display.html?id={id}
 
129
 
 
130
would allow ``bzr commit --fixes cpan:1234`` to mark bug 1234 in CPAN's
 
131
RT bug tracker as fixed, or::
 
132
 
 
133
    bugtracker_hudson_url = http://issues.hudson-ci.org/browse/{id}
 
134
 
 
135
would allow ``bzr commit --fixes hudson:HUDSON-1234`` to mark bug HUDSON-1234
 
136
in Hudson's JIRA bug tracker as fixed.
 
137
"""
 
138
 
 
139
 
 
140
def get_bug_url(abbreviated_bugtracker_name, branch, bug_id):
 
141
    """Return a URL pointing to the canonical web page of the bug identified by
 
142
    'bug_id'.
 
143
    """
 
144
    tracker = tracker_registry.get_tracker(abbreviated_bugtracker_name, branch)
 
145
    return tracker.get_bug_url(bug_id)
 
146
 
 
147
 
 
148
class TrackerRegistry(registry.Registry):
 
149
    """Registry of bug tracker types."""
 
150
 
 
151
    def get_tracker(self, abbreviated_bugtracker_name, branch):
 
152
        """Return the first registered tracker that understands
 
153
        'abbreviated_bugtracker_name'.
 
154
 
 
155
        If no such tracker is found, raise KeyError.
 
156
        """
 
157
        for tracker_name in self.keys():
 
158
            tracker_type = self.get(tracker_name)
 
159
            tracker = tracker_type.get(abbreviated_bugtracker_name, branch)
 
160
            if tracker is not None:
 
161
                return tracker
 
162
        raise errors.UnknownBugTrackerAbbreviation(abbreviated_bugtracker_name,
 
163
                                                   branch)
 
164
 
 
165
    def help_topic(self, topic):
 
166
        return _bugs_help
 
167
 
 
168
 
 
169
tracker_registry = TrackerRegistry()
 
170
"""Registry of bug trackers."""
 
171
 
 
172
 
 
173
class BugTracker(object):
 
174
    """Base class for bug trackers."""
 
175
 
 
176
    def check_bug_id(self, bug_id):
 
177
        """Check that the bug_id is valid.
 
178
 
 
179
        The base implementation assumes that all bug_ids are valid.
 
180
        """
 
181
 
 
182
    def get_bug_url(self, bug_id):
 
183
        """Return the URL for bug_id. Raise an error if bug ID is malformed."""
 
184
        self.check_bug_id(bug_id)
 
185
        return self._get_bug_url(bug_id)
 
186
 
 
187
    def _get_bug_url(self, bug_id):
 
188
        """Given a validated bug_id, return the bug's web page's URL."""
 
189
 
 
190
 
 
191
class IntegerBugTracker(BugTracker):
 
192
    """A bug tracker that only allows integer bug IDs."""
 
193
 
 
194
    def check_bug_id(self, bug_id):
 
195
        try:
 
196
            int(bug_id)
 
197
        except ValueError:
 
198
            raise errors.MalformedBugIdentifier(bug_id, "Must be an integer")
 
199
 
 
200
 
 
201
class UniqueIntegerBugTracker(IntegerBugTracker):
 
202
    """A style of bug tracker that exists in one place only, such as Launchpad.
 
203
 
 
204
    If you have one of these trackers then register an instance passing in an
 
205
    abbreviated name for the bug tracker and a base URL. The bug ids are
 
206
    appended directly to the URL.
 
207
    """
 
208
 
 
209
    def __init__(self, abbreviated_bugtracker_name, base_url):
 
210
        self.abbreviation = abbreviated_bugtracker_name
 
211
        self.base_url = base_url
 
212
 
 
213
    def get(self, abbreviated_bugtracker_name, branch):
 
214
        """Returns the tracker if the abbreviation matches. Returns None
 
215
        otherwise."""
 
216
        if abbreviated_bugtracker_name != self.abbreviation:
 
217
            return None
 
218
        return self
 
219
 
 
220
    def _get_bug_url(self, bug_id):
 
221
        """Return the URL for bug_id."""
 
222
        return self.base_url + bug_id
 
223
 
 
224
 
 
225
tracker_registry.register(
 
226
    'launchpad', UniqueIntegerBugTracker('lp', 'https://launchpad.net/bugs/'))
 
227
 
 
228
 
 
229
tracker_registry.register(
 
230
    'debian', UniqueIntegerBugTracker('deb', 'http://bugs.debian.org/'))
 
231
 
 
232
 
 
233
tracker_registry.register('gnome',
 
234
    UniqueIntegerBugTracker('gnome',
 
235
                            'http://bugzilla.gnome.org/show_bug.cgi?id='))
 
236
 
 
237
 
 
238
class URLParametrizedBugTracker(BugTracker):
 
239
    """A type of bug tracker that can be found on a variety of different sites,
 
240
    and thus needs to have the base URL configured.
 
241
 
 
242
    Looks for a config setting in the form '<type_name>_<abbreviation>_url'.
 
243
    `type_name` is the name of the type of tracker and `abbreviation`
 
244
    is a short name for the particular instance.
 
245
    """
 
246
 
 
247
    def get(self, abbreviation, branch):
 
248
        config = branch.get_config()
 
249
        url = config.get_user_option(
 
250
            "%s_%s_url" % (self.type_name, abbreviation), expand=False)
 
251
        if url is None:
 
252
            return None
 
253
        self._base_url = url
 
254
        return self
 
255
 
 
256
    def __init__(self, type_name, bug_area):
 
257
        self.type_name = type_name
 
258
        self._bug_area = bug_area
 
259
 
 
260
    def _get_bug_url(self, bug_id):
 
261
        """Return a URL for a bug on this Trac instance."""
 
262
        return urlutils.join(self._base_url, self._bug_area) + str(bug_id)
 
263
 
 
264
 
 
265
class URLParametrizedIntegerBugTracker(IntegerBugTracker,
 
266
                                       URLParametrizedBugTracker):
 
267
    """A type of bug tracker that  only allows integer bug IDs.
 
268
 
 
269
    This can be found on a variety of different sites, and thus needs to have
 
270
    the base URL configured.
 
271
 
 
272
    Looks for a config setting in the form '<type_name>_<abbreviation>_url'.
 
273
    `type_name` is the name of the type of tracker (e.g. 'bugzilla' or 'trac')
 
274
    and `abbreviation` is a short name for the particular instance (e.g.
 
275
    'squid' or 'apache').
 
276
    """
 
277
 
 
278
tracker_registry.register(
 
279
    'trac', URLParametrizedIntegerBugTracker('trac', 'ticket/'))
 
280
 
 
281
tracker_registry.register(
 
282
    'bugzilla',
 
283
    URLParametrizedIntegerBugTracker('bugzilla', 'show_bug.cgi?id='))
 
284
 
 
285
 
 
286
class GenericBugTracker(URLParametrizedBugTracker):
 
287
    """Generic bug tracker specified by an URL template."""
 
288
 
 
289
    def __init__(self):
 
290
        super(GenericBugTracker, self).__init__('bugtracker', None)
 
291
 
 
292
    def get(self, abbreviation, branch):
 
293
        self._abbreviation = abbreviation
 
294
        return super(GenericBugTracker, self).get(abbreviation, branch)
 
295
 
 
296
    def _get_bug_url(self, bug_id):
 
297
        """Given a validated bug_id, return the bug's web page's URL."""
 
298
        if '{id}' not in self._base_url:
 
299
            raise errors.InvalidBugTrackerURL(self._abbreviation,
 
300
                                              self._base_url)
 
301
        return self._base_url.replace('{id}', str(bug_id))
 
302
 
 
303
 
 
304
tracker_registry.register('generic', GenericBugTracker())
 
305
 
 
306
 
 
307
FIXED = 'fixed'
 
308
 
 
309
ALLOWED_BUG_STATUSES = set([FIXED])
 
310
 
 
311
 
 
312
def encode_fixes_bug_urls(bug_urls):
 
313
    """Get the revision property value for a commit that fixes bugs.
 
314
 
 
315
    :param bug_urls: An iterable of escaped URLs to bugs. These normally
 
316
        come from `get_bug_url`.
 
317
    :return: A string that will be set as the 'bugs' property of a revision
 
318
        as part of a commit.
 
319
    """
 
320
    return '\n'.join(('%s %s' % (url, FIXED)) for url in bug_urls)