/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2007-2010 Canonical Ltd
2376.4.4 by jml at canonical
Beginnings of generic bug-tracker plugin system.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2376.4.4 by jml at canonical
Beginnings of generic bug-tracker plugin system.
16
6729.4.1 by Jelmer Vernooij
Move bugtracker errors to breezy.bugtracker.
17
from . import (
18
    errors,
19
    registry,
20
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
21
from .lazy_import import lazy_import
2376.4.6 by jml at canonical
Basic docstrings for bugtracker.py
22
lazy_import(globals(), """
6729.4.1 by Jelmer Vernooij
Move bugtracker errors to breezy.bugtracker.
23
from breezy import urlutils
2376.4.6 by jml at canonical
Basic docstrings for bugtracker.py
24
""")
2376.4.4 by jml at canonical
Beginnings of generic bug-tracker plugin system.
25
26
2376.4.7 by jml at canonical
- Add docstrings to tests.
27
"""Provides a shorthand for referring to bugs on a variety of bug trackers.
28
29
'commit --fixes' stores references to bugs as a <bug_url> -> <bug_status>
30
mapping in the properties for that revision.
31
32
However, it's inconvenient to type out full URLs for bugs on the command line,
33
particularly given that many users will be using only a single bug tracker per
34
branch.
35
36
Thus, this module provides a registry of types of bug tracker (e.g. Launchpad,
2376.4.23 by Jonathan Lange
Change 'tag' to 'abbreviated_tracker_name'
37
Trac). Given an abbreviated name (e.g. 'lp', 'twisted') and a branch with
2376.4.7 by jml at canonical
- Add docstrings to tests.
38
configuration information, these tracker types can return an instance capable
39
of converting bug IDs into URLs.
40
"""
41
42
7143.15.1 by Jelmer Vernooij
Fix style issues.
43
_bugs_help = """\
44
When making a commit, metadata about bugs fixed by that change can be
4927.2.6 by Ian Clatworthy
Nicer formatting of bug tracking topic
45
recorded by using the ``--fixes`` option. For each bug marked as fixed, an
3053.8.1 by Ian Clatworthy
Improve bug tracker integration documentation (Ian Clatworthy)
46
entry is included in the 'bugs' revision property stating '<url> <status>'.
3154.1.1 by Ian Clatworthy
Minor tweaks to bug tracker integration documentation
47
(The only ``status`` value currently supported is ``fixed.``)
3535.10.2 by James Westby
Flesh out the bugs help topic and explain the simple things first.
48
4927.2.6 by Ian Clatworthy
Nicer formatting of bug tracking topic
49
The ``--fixes`` option allows you to specify a bug tracker and a bug identifier
50
rather than a full URL. This looks like::
3535.10.2 by James Westby
Flesh out the bugs help topic and explain the simple things first.
51
3535.10.3 by James Westby
Talk about "trackers" rather than "tags" as it may be less confusing.
52
    bzr commit --fixes <tracker>:<id>
3535.10.2 by James Westby
Flesh out the bugs help topic and explain the simple things first.
53
6120.1.2 by Jelmer Vernooij
Doc doc doc.
54
or::
55
56
    bzr commit --fixes <id>
57
3535.10.3 by James Westby
Talk about "trackers" rather than "tags" as it may be less confusing.
58
where "<tracker>" is an identifier for the bug tracker, and "<id>" is the
3535.10.2 by James Westby
Flesh out the bugs help topic and explain the simple things first.
59
identifier for that bug within the bugtracker, usually the bug number.
6120.1.2 by Jelmer Vernooij
Doc doc doc.
60
If "<tracker>" is not specified the ``bugtracker`` set in the branch
61
or global configuration is used.
3535.10.2 by James Westby
Flesh out the bugs help topic and explain the simple things first.
62
3535.10.5 by James Westby
Don't say "well-known" as suggested by Robert.
63
Bazaar knows about a few bug trackers that have many users. If
3535.10.2 by James Westby
Flesh out the bugs help topic and explain the simple things first.
64
you use one of these bug trackers then there is no setup required to
3535.10.3 by James Westby
Talk about "trackers" rather than "tags" as it may be less confusing.
65
use this feature, you just need to know the tracker identifier to use.
66
These are the bugtrackers that are built in:
67
4927.2.6 by Ian Clatworthy
Nicer formatting of bug tracking topic
68
  ============================ ============ ============
69
  URL                          Abbreviation Example
70
  ============================ ============ ============
71
  https://bugs.launchpad.net/  lp           lp:12345
72
  http://bugs.debian.org/      deb          deb:12345
73
  http://bugzilla.gnome.org/   gnome        gnome:12345
74
  ============================ ============ ============
3535.10.2 by James Westby
Flesh out the bugs help topic and explain the simple things first.
75
3535.10.5 by James Westby
Don't say "well-known" as suggested by Robert.
76
For the bug trackers not listed above configuration is required.
77
Support for generating the URLs for any project using Bugzilla or Trac
78
is built in, along with a template mechanism for other bugtrackers with
3535.10.6 by James Westby
Mention that the a plugin can support their tracker as the last resort.
79
simple URL schemes. If your bug tracker can't be described by one
80
of the schemes described below then you can write a plugin to support
81
it.
3535.10.2 by James Westby
Flesh out the bugs help topic and explain the simple things first.
82
3053.8.1 by Ian Clatworthy
Improve bug tracker integration documentation (Ian Clatworthy)
83
If you use Bugzilla or Trac, then you only need to set a configuration
84
variable which contains the base URL of the bug tracker. These options
6740.1.1 by Jelmer Vernooij
Rename bazaar.conf to breezy.conf.
85
can go into ``breezy.conf``, ``branch.conf`` or into a branch-specific
3053.8.1 by Ian Clatworthy
Improve bug tracker integration documentation (Ian Clatworthy)
86
configuration section in ``locations.conf``.  You can set up these values
87
for each of the projects you work on.
88
89
Note: As you provide a short name for each tracker, you can specify one or
90
more bugs in one or more trackers at commit time if you wish.
91
3860.2.1 by Martin Pool
Mention in 'help bugs' the syntax for Launchpad
92
Launchpad
93
---------
94
95
Use ``bzr commit --fixes lp:2`` to record that this commit fixes bug 2.
96
4927.2.6 by Ian Clatworthy
Nicer formatting of bug tracking topic
97
bugzilla_<tracker>_url
98
----------------------
3053.8.1 by Ian Clatworthy
Improve bug tracker integration documentation (Ian Clatworthy)
99
100
If present, the location of the Bugzilla bug tracker referred to by
4927.2.6 by Ian Clatworthy
Nicer formatting of bug tracking topic
101
<tracker>. This option can then be used together with ``bzr commit
3053.8.1 by Ian Clatworthy
Improve bug tracker integration documentation (Ian Clatworthy)
102
--fixes`` to mark bugs in that tracker as being fixed by that commit. For
103
example::
104
5444.1.1 by Martin Pool
Updated URL for Squid bugzilla
105
    bugzilla_squid_url = http://bugs.squid-cache.org
3053.8.1 by Ian Clatworthy
Improve bug tracker integration documentation (Ian Clatworthy)
106
107
would allow ``bzr commit --fixes squid:1234`` to mark Squid's bug 1234 as
108
fixed.
109
4927.2.6 by Ian Clatworthy
Nicer formatting of bug tracking topic
110
trac_<tracker>_url
111
------------------
3053.8.1 by Ian Clatworthy
Improve bug tracker integration documentation (Ian Clatworthy)
112
113
If present, the location of the Trac instance referred to by
4927.2.6 by Ian Clatworthy
Nicer formatting of bug tracking topic
114
<tracker>. This option can then be used together with ``bzr commit
3053.8.1 by Ian Clatworthy
Improve bug tracker integration documentation (Ian Clatworthy)
115
--fixes`` to mark bugs in that tracker as being fixed by that commit. For
116
example::
117
118
    trac_twisted_url = http://www.twistedmatrix.com/trac
119
120
would allow ``bzr commit --fixes twisted:1234`` to mark Twisted's bug 1234 as
121
fixed.
122
4927.2.6 by Ian Clatworthy
Nicer formatting of bug tracking topic
123
bugtracker_<tracker>_url
124
------------------------
3089.3.14 by Ian Clatworthy
follow-up tweaks to bzr.dev integration
125
3053.8.1 by Ian Clatworthy
Improve bug tracker integration documentation (Ian Clatworthy)
126
If present, the location of a generic bug tracker instance referred to by
4927.2.6 by Ian Clatworthy
Nicer formatting of bug tracking topic
127
<tracker>. The location must contain an ``{id}`` placeholder,
3053.8.1 by Ian Clatworthy
Improve bug tracker integration documentation (Ian Clatworthy)
128
which will be replaced by a specific bug ID. This option can then be used
129
together with ``bzr commit --fixes`` to mark bugs in that tracker as being
130
fixed by that commit. For example::
131
132
    bugtracker_python_url = http://bugs.python.org/issue{id}
133
134
would allow ``bzr commit --fixes python:1234`` to mark bug 1234 in Python's
135
Roundup bug tracker as fixed, or::
136
137
    bugtracker_cpan_url = http://rt.cpan.org/Public/Bug/Display.html?id={id}
138
5409.3.1 by Alexandre Garnier
Allow using string bug ID with generic bug trackers.
139
would allow ``bzr commit --fixes cpan:1234`` to mark bug 1234 in CPAN's
140
RT bug tracker as fixed, or::
141
142
    bugtracker_hudson_url = http://issues.hudson-ci.org/browse/{id}
143
144
would allow ``bzr commit --fixes hudson:HUDSON-1234`` to mark bug HUDSON-1234
145
in Hudson's JIRA bug tracker as fixed.
3053.8.1 by Ian Clatworthy
Improve bug tracker integration documentation (Ian Clatworthy)
146
"""
147
148
6729.4.1 by Jelmer Vernooij
Move bugtracker errors to breezy.bugtracker.
149
class MalformedBugIdentifier(errors.BzrError):
150
151
    _fmt = ('Did not understand bug identifier %(bug_id)s: %(reason)s. '
152
            'See "brz help bugs" for more information on this feature.')
153
154
    def __init__(self, bug_id, reason):
155
        self.bug_id = bug_id
156
        self.reason = reason
157
158
159
class InvalidBugTrackerURL(errors.BzrError):
160
161
    _fmt = ("The URL for bug tracker \"%(abbreviation)s\" doesn't "
162
            "contain {id}: %(url)s")
163
164
    def __init__(self, abbreviation, url):
165
        self.abbreviation = abbreviation
166
        self.url = url
167
168
169
class UnknownBugTrackerAbbreviation(errors.BzrError):
170
171
    _fmt = ("Cannot find registered bug tracker called %(abbreviation)s "
172
            "on %(branch)s")
173
174
    def __init__(self, abbreviation, branch):
175
        self.abbreviation = abbreviation
176
        self.branch = branch
177
178
179
class InvalidLineInBugsProperty(errors.BzrError):
180
181
    _fmt = ("Invalid line in bugs property: '%(line)s'")
182
183
    def __init__(self, line):
184
        self.line = line
185
186
7182.1.1 by Jelmer Vernooij
Raise on invalid bug URL.
187
class InvalidBugUrl(errors.BzrError):
188
189
    _fmt = "Invalid bug URL: %(url)s"
190
191
    def __init__(self, url):
192
        self.url = url
193
194
6729.4.1 by Jelmer Vernooij
Move bugtracker errors to breezy.bugtracker.
195
class InvalidBugStatus(errors.BzrError):
196
197
    _fmt = ("Invalid bug status: '%(status)s'")
198
199
    def __init__(self, status):
200
        self.status = status
201
202
2376.4.23 by Jonathan Lange
Change 'tag' to 'abbreviated_tracker_name'
203
def get_bug_url(abbreviated_bugtracker_name, branch, bug_id):
204
    """Return a URL pointing to the canonical web page of the bug identified by
205
    'bug_id'.
206
    """
207
    tracker = tracker_registry.get_tracker(abbreviated_bugtracker_name, branch)
208
    return tracker.get_bug_url(bug_id)
2376.4.4 by jml at canonical
Beginnings of generic bug-tracker plugin system.
209
210
211
class TrackerRegistry(registry.Registry):
212
    """Registry of bug tracker types."""
213
2376.4.23 by Jonathan Lange
Change 'tag' to 'abbreviated_tracker_name'
214
    def get_tracker(self, abbreviated_bugtracker_name, branch):
215
        """Return the first registered tracker that understands
216
        'abbreviated_bugtracker_name'.
2376.4.7 by jml at canonical
- Add docstrings to tests.
217
218
        If no such tracker is found, raise KeyError.
219
        """
2376.4.23 by Jonathan Lange
Change 'tag' to 'abbreviated_tracker_name'
220
        for tracker_name in self.keys():
221
            tracker_type = self.get(tracker_name)
222
            tracker = tracker_type.get(abbreviated_bugtracker_name, branch)
2376.4.4 by jml at canonical
Beginnings of generic bug-tracker plugin system.
223
            if tracker is not None:
224
                return tracker
7143.15.1 by Jelmer Vernooij
Fix style issues.
225
        raise UnknownBugTrackerAbbreviation(
226
            abbreviated_bugtracker_name, branch)
2376.4.7 by jml at canonical
- Add docstrings to tests.
227
2376.4.36 by Jonathan Lange
Provide really basic help topic for our bug tracker support.
228
    def help_topic(self, topic):
3053.8.1 by Ian Clatworthy
Improve bug tracker integration documentation (Ian Clatworthy)
229
        return _bugs_help
2376.4.36 by Jonathan Lange
Provide really basic help topic for our bug tracker support.
230
2376.4.4 by jml at canonical
Beginnings of generic bug-tracker plugin system.
231
232
tracker_registry = TrackerRegistry()
2376.4.6 by jml at canonical
Basic docstrings for bugtracker.py
233
"""Registry of bug trackers."""
2376.4.4 by jml at canonical
Beginnings of generic bug-tracker plugin system.
234
2376.4.15 by Jonathan Lange
Whitespace cleanup
235
2376.4.40 by Jonathan Lange
Redo the hierarchy of bug trackers to reduce duplication.
236
class BugTracker(object):
237
    """Base class for bug trackers."""
238
239
    def check_bug_id(self, bug_id):
240
        """Check that the bug_id is valid.
241
242
        The base implementation assumes that all bug_ids are valid.
243
        """
244
245
    def get_bug_url(self, bug_id):
246
        """Return the URL for bug_id. Raise an error if bug ID is malformed."""
247
        self.check_bug_id(bug_id)
248
        return self._get_bug_url(bug_id)
249
250
    def _get_bug_url(self, bug_id):
251
        """Given a validated bug_id, return the bug's web page's URL."""
252
253
254
class IntegerBugTracker(BugTracker):
255
    """A bug tracker that only allows integer bug IDs."""
256
257
    def check_bug_id(self, bug_id):
258
        try:
259
            int(bug_id)
260
        except ValueError:
6729.4.1 by Jelmer Vernooij
Move bugtracker errors to breezy.bugtracker.
261
            raise MalformedBugIdentifier(bug_id, "Must be an integer")
2376.4.40 by Jonathan Lange
Redo the hierarchy of bug trackers to reduce duplication.
262
263
264
class UniqueIntegerBugTracker(IntegerBugTracker):
2376.4.19 by Jonathan Lange
Rename SimpleBugTracker to UniqueBugTracker
265
    """A style of bug tracker that exists in one place only, such as Launchpad.
2376.4.15 by Jonathan Lange
Whitespace cleanup
266
2376.4.41 by Jonathan Lange
Update UniqueIntegerBugTracker docstring for new API
267
    If you have one of these trackers then register an instance passing in an
3270.5.3 by James Westby
No longer add an extra class to accomoadate gnome.
268
    abbreviated name for the bug tracker and a base URL. The bug ids are
269
    appended directly to the URL.
2376.5.1 by James Westby
Add a superclass for easy bug trackers. Also add bugs.debian.org as deb:
270
    """
271
2376.4.25 by Jonathan Lange
Make singleton bug tracker thing work via instances.
272
    def __init__(self, abbreviated_bugtracker_name, base_url):
273
        self.abbreviation = abbreviated_bugtracker_name
274
        self.base_url = base_url
275
276
    def get(self, abbreviated_bugtracker_name, branch):
6940.3.2 by Jelmer Vernooij
Review comments.
277
        """Returns the tracker if the abbreviation matches, otherwise ``None``.
278
        """
2376.4.25 by Jonathan Lange
Make singleton bug tracker thing work via instances.
279
        if abbreviated_bugtracker_name != self.abbreviation:
2376.5.1 by James Westby
Add a superclass for easy bug trackers. Also add bugs.debian.org as deb:
280
            return None
2376.4.25 by Jonathan Lange
Make singleton bug tracker thing work via instances.
281
        return self
2376.5.1 by James Westby
Add a superclass for easy bug trackers. Also add bugs.debian.org as deb:
282
2376.4.40 by Jonathan Lange
Redo the hierarchy of bug trackers to reduce duplication.
283
    def _get_bug_url(self, bug_id):
2376.5.1 by James Westby
Add a superclass for easy bug trackers. Also add bugs.debian.org as deb:
284
        """Return the URL for bug_id."""
6805.1.4 by Jelmer Vernooij
Fixes from Martin.
285
        return self.base_url + str(bug_id)
2376.5.1 by James Westby
Add a superclass for easy bug trackers. Also add bugs.debian.org as deb:
286
287
6940.3.1 by Jelmer Vernooij
Add a 'github' bugtracker.
288
class ProjectIntegerBugTracker(IntegerBugTracker):
289
    """A bug tracker that exists in one place only with per-project ids.
290
291
    If you have one of these trackers then register an instance passing in an
292
    abbreviated name for the bug tracker and a base URL. The bug ids are
293
    appended directly to the URL.
294
    """
295
296
    def __init__(self, abbreviated_bugtracker_name, base_url):
297
        self.abbreviation = abbreviated_bugtracker_name
298
        self._base_url = base_url
299
300
    def get(self, abbreviated_bugtracker_name, branch):
6940.3.2 by Jelmer Vernooij
Review comments.
301
        """Returns the tracker if the abbreviation matches, otherwise ``None``.
302
        """
6940.3.1 by Jelmer Vernooij
Add a 'github' bugtracker.
303
        if abbreviated_bugtracker_name != self.abbreviation:
304
            return None
305
        return self
306
307
    def check_bug_id(self, bug_id):
308
        try:
309
            (project, bug_id) = bug_id.rsplit('/', 1)
310
        except ValueError:
311
            raise MalformedBugIdentifier(bug_id, "Expected format: project/id")
312
        try:
313
            int(bug_id)
314
        except ValueError:
315
            raise MalformedBugIdentifier(bug_id, "Bug id must be an integer")
316
317
    def _get_bug_url(self, bug_id):
318
        (project, bug_id) = bug_id.rsplit('/', 1)
319
        """Return the URL for bug_id."""
320
        if '{id}' not in self._base_url:
321
            raise InvalidBugTrackerURL(self._abbreviation, self._base_url)
322
        if '{project}' not in self._base_url:
323
            raise InvalidBugTrackerURL(self._abbreviation, self._base_url)
324
        return self._base_url.replace(
7143.15.1 by Jelmer Vernooij
Fix style issues.
325
            '{project}', project).replace('{id}', str(bug_id))
6940.3.1 by Jelmer Vernooij
Add a 'github' bugtracker.
326
327
2376.4.25 by Jonathan Lange
Make singleton bug tracker thing work via instances.
328
tracker_registry.register(
329
    'launchpad', UniqueIntegerBugTracker('lp', 'https://launchpad.net/bugs/'))
330
331
332
tracker_registry.register(
2376.4.29 by Jonathan Lange
Tests for builtin trackers.
333
    'debian', UniqueIntegerBugTracker('deb', 'http://bugs.debian.org/'))
2376.4.4 by jml at canonical
Beginnings of generic bug-tracker plugin system.
334
2376.4.15 by Jonathan Lange
Whitespace cleanup
335
7143.15.1 by Jelmer Vernooij
Fix style issues.
336
tracker_registry.register(
337
    'gnome', UniqueIntegerBugTracker(
338
        'gnome', 'http://bugzilla.gnome.org/show_bug.cgi?id='))
3270.5.3 by James Westby
No longer add an extra class to accomoadate gnome.
339
340
6940.3.1 by Jelmer Vernooij
Add a 'github' bugtracker.
341
tracker_registry.register(
342
    'github', ProjectIntegerBugTracker(
343
        'github', 'https://github.com/{project}/issues/{id}'))
344
345
5409.3.1 by Alexandre Garnier
Allow using string bug ID with generic bug trackers.
346
class URLParametrizedBugTracker(BugTracker):
2376.4.40 by Jonathan Lange
Redo the hierarchy of bug trackers to reduce duplication.
347
    """A type of bug tracker that can be found on a variety of different sites,
348
    and thus needs to have the base URL configured.
349
350
    Looks for a config setting in the form '<type_name>_<abbreviation>_url'.
5409.3.1 by Alexandre Garnier
Allow using string bug ID with generic bug trackers.
351
    `type_name` is the name of the type of tracker and `abbreviation`
352
    is a short name for the particular instance.
2376.4.40 by Jonathan Lange
Redo the hierarchy of bug trackers to reduce duplication.
353
    """
354
2376.4.42 by Jonathan Lange
Parametrize URLParametrizedIntegerBugTracker even further so we don't need to
355
    def get(self, abbreviation, branch):
2376.4.40 by Jonathan Lange
Redo the hierarchy of bug trackers to reduce duplication.
356
        config = branch.get_config()
357
        url = config.get_user_option(
5676.1.4 by Jelmer Vernooij
merge bzr.dev.
358
            "%s_%s_url" % (self.type_name, abbreviation), expand=False)
2376.4.40 by Jonathan Lange
Redo the hierarchy of bug trackers to reduce duplication.
359
        if url is None:
360
            return None
361
        self._base_url = url
2376.4.42 by Jonathan Lange
Parametrize URLParametrizedIntegerBugTracker even further so we don't need to
362
        return self
363
364
    def __init__(self, type_name, bug_area):
365
        self.type_name = type_name
366
        self._bug_area = bug_area
2376.4.40 by Jonathan Lange
Redo the hierarchy of bug trackers to reduce duplication.
367
368
    def _get_bug_url(self, bug_id):
2376.4.6 by jml at canonical
Basic docstrings for bugtracker.py
369
        """Return a URL for a bug on this Trac instance."""
2376.4.42 by Jonathan Lange
Parametrize URLParametrizedIntegerBugTracker even further so we don't need to
370
        return urlutils.join(self._base_url, self._bug_area) + str(bug_id)
371
372
5676.1.4 by Jelmer Vernooij
merge bzr.dev.
373
class URLParametrizedIntegerBugTracker(IntegerBugTracker,
374
                                       URLParametrizedBugTracker):
375
    """A type of bug tracker that  only allows integer bug IDs.
376
377
    This can be found on a variety of different sites, and thus needs to have
378
    the base URL configured.
5409.3.1 by Alexandre Garnier
Allow using string bug ID with generic bug trackers.
379
380
    Looks for a config setting in the form '<type_name>_<abbreviation>_url'.
381
    `type_name` is the name of the type of tracker (e.g. 'bugzilla' or 'trac')
382
    and `abbreviation` is a short name for the particular instance (e.g.
383
    'squid' or 'apache').
384
    """
385
7143.15.1 by Jelmer Vernooij
Fix style issues.
386
2376.4.42 by Jonathan Lange
Parametrize URLParametrizedIntegerBugTracker even further so we don't need to
387
tracker_registry.register(
388
    'trac', URLParametrizedIntegerBugTracker('trac', 'ticket/'))
389
390
tracker_registry.register(
391
    'bugzilla',
392
    URLParametrizedIntegerBugTracker('bugzilla', 'show_bug.cgi?id='))
3035.3.1 by Lukáš Lalinský
Generic bug tracker configuration.
393
394
5409.3.1 by Alexandre Garnier
Allow using string bug ID with generic bug trackers.
395
class GenericBugTracker(URLParametrizedBugTracker):
3035.3.1 by Lukáš Lalinský
Generic bug tracker configuration.
396
    """Generic bug tracker specified by an URL template."""
397
398
    def __init__(self):
3035.3.2 by Lukáš Lalinský
Add tests for InvalidBugTrackerURL.
399
        super(GenericBugTracker, self).__init__('bugtracker', None)
400
401
    def get(self, abbreviation, branch):
402
        self._abbreviation = abbreviation
403
        return super(GenericBugTracker, self).get(abbreviation, branch)
3035.3.1 by Lukáš Lalinský
Generic bug tracker configuration.
404
405
    def _get_bug_url(self, bug_id):
406
        """Given a validated bug_id, return the bug's web page's URL."""
407
        if '{id}' not in self._base_url:
6729.4.1 by Jelmer Vernooij
Move bugtracker errors to breezy.bugtracker.
408
            raise InvalidBugTrackerURL(self._abbreviation, self._base_url)
3035.3.1 by Lukáš Lalinský
Generic bug tracker configuration.
409
        return self._base_url.replace('{id}', str(bug_id))
410
411
412
tracker_registry.register('generic', GenericBugTracker())
4119.4.1 by Jonathan Lange
Extract bug fix encoding logic from commit.
413
414
4119.4.2 by Jonathan Lange
Some refactoring, some unit tests.
415
FIXED = 'fixed'
7131.10.1 by Jelmer Vernooij
Add --bugs option to 'bzr commit'.
416
RELATED = 'related'
4119.4.2 by Jonathan Lange
Some refactoring, some unit tests.
417
7131.10.1 by Jelmer Vernooij
Add --bugs option to 'bzr commit'.
418
ALLOWED_BUG_STATUSES = {FIXED, RELATED}
4119.4.3 by Jonathan Lange
Add Revision.iter_bugs.
419
4119.4.2 by Jonathan Lange
Some refactoring, some unit tests.
420
4119.4.1 by Jonathan Lange
Extract bug fix encoding logic from commit.
421
def encode_fixes_bug_urls(bug_urls):
4119.4.5 by Jonathan Lange
Fix the docstring.
422
    """Get the revision property value for a commit that fixes bugs.
423
7131.10.1 by Jelmer Vernooij
Add --bugs option to 'bzr commit'.
424
    :param bug_urls: An iterable of (escaped URL, tag) tuples. These normally
4119.4.5 by Jonathan Lange
Fix the docstring.
425
        come from `get_bug_url`.
426
    :return: A string that will be set as the 'bugs' property of a revision
427
        as part of a commit.
4119.4.1 by Jonathan Lange
Extract bug fix encoding logic from commit.
428
    """
7182.1.1 by Jelmer Vernooij
Raise on invalid bug URL.
429
    lines = []
430
    for (url, tag) in bug_urls:
431
        if ' ' in url:
432
            raise InvalidBugUrl(url)
433
        lines.append('%s %s' % (url, tag))
434
    return '\n'.join(lines)
7182.1.2 by Jelmer Vernooij
Move decoding of bug properties.
435
436
437
def decode_bug_urls(bug_text):
438
    """Decode a bug property text.
439
440
    :param bug_text: Contents of a bugs property
441
    :return: iterator over (url, status) tuples
442
    """
443
    for line in bug_text.splitlines():
444
        try:
445
            url, status = line.split(None, 2)
446
        except ValueError:
447
            raise InvalidLineInBugsProperty(line)
448
        if status not in ALLOWED_BUG_STATUSES:
449
            raise InvalidBugStatus(status)
450
        yield url, status