Debian Codegraph @

lib/bugs.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
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.
14 #
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 from breezy import (
20 bugtracker,
21 config,
22 lazy_regex,
23 )
24
25
26 # XXX use just (\d+) at the end of URL as simpler regexp?
27 _bug_id_re = lazy_regex.lazy_compile(r'(?:'
28 r'bugs/' # Launchpad bugs URL
29 r'|ticket/' # Trac bugs URL
30 r'|show_bug\.cgi\?id=' # Bugzilla bugs URL
31 r'|issues/(?:show/)?' # Redmine bugs URL
32 r'|DispForm.aspx\?ID=' # Microsoft SharePoint URL
33 r'|default.asp\?' # Fogbugz URL
34 r'|issue' # Roundup issue tracker URL
35 r'|view.php\?id=' # Mantis bug tracker URL
36 r'|aid=' # FusionForge bug tracker URL
37 r'|task_id=' # Flyspray bug tracker URL (http://flyspray.org) - old style URLs(?)
38 r'|task/' # Flyspray itself bugtracker (https://bugs.flyspray.org)
39 r')(\d+)(?:\b|$)')
40
41
42 _jira_bug_id_re = lazy_regex.lazy_compile(r'(?:.*/browse/)([A-Z][A-Z0-9_]*-\d+)($)')
43
44
45 _unique_bugtrackers = ('lp', 'deb', 'gnome')
46 # bugtracker config settings
47 _bugtracker_re = lazy_regex.lazy_compile('(bugtracker|trac|bugzilla)_(.+)_url')
48
49
50 def get_bug_id(bug_url):
51 match = _bug_id_re.search(bug_url)
52 if match:
53 return match.group(1)
54 match = _jira_bug_id_re.search(bug_url)
55 if match:
56 return match.group(1)
57 return None
58
59
60 class FakeBranchForBugs(object):
61 """Fake branch required for breezy/bugtracker.py"""
62
63 def __init__(self):
64 self._config = config.GlobalConfig()
65
66 def get_config(self):
67 return self._config
68
69
70 def bug_urls_to_ids(bug_urls, branch=None):
71 """Convert list of bug URLs to list of bug ids in form tag:id.
72
73 @param bug_urls: list of bug URLs from revision property 'bugs'.
74 @param branch: current branch object or None.
75 """
76 result = []
77 urls_dict = {}
78 for i in bug_urls:
79 url = i.split(' ')[0]
80 n = get_bug_id(url)
81 if n:
82 urls_dict[url] = n
83 if not urls_dict:
84 return result
85 # collect bug tags
86 bug_tags = {}
87 bug_tags.update(get_unique_bug_tags())
88 bug_tags.update(get_global_bug_tags())
89 if branch is not None:
90 bug_tags.update(get_branch_bug_tags(branch))
91 else:
92 branch = FakeBranchForBugs()
93 # try to convert bug urls to tag:id values
94 for k,n in urls_dict.items():
95 for tag in bug_tags:
96 url = bugtracker.get_bug_url(tag, branch, n)
97 if url == k:
98 result.append(tag+':'+n)
99 break
100 return sorted(result)
101
102
103 def get_user_bug_trackers_tags(config_keys):
104 """Return dict of abbreviated bugtrackers tags from user config.
105
106 @param config_keys: options names from config which could be
107 bugtracker settings.
108
109 @return: dict with tags as keys and bugtracker_type as values,
110 where bugtracker_type is prefix of tag entry in config_dict.
111 Possible prefixes are: bugtracker, trac, bugzilla
112 """
113 result = {}
114 for key in config_keys:
115 m = _bugtracker_re.match(key)
116 if m:
117 result[m.group(2)] = m.group(1)
118 return result
119
120 def get_unique_bug_tags():
121 """Return dict of global unique bugtrackers, e.g. Launchpad."""
122 return dict([(i, 'unique') for i in _unique_bugtrackers])
123
124 def get_global_bug_tags():
125 """Return bug tags collected from global config bazaar.conf"""
126 cfg = config.GlobalConfig()
127 keys = list(cfg._get_parser().get('DEFAULT', {}).keys())
128 return get_user_bug_trackers_tags(keys)
129
130 def get_branch_bug_tags(branch):
131 """Return bug tags collected from branch configs
132 (branch.conf and locations.conf).
133 """
134 result = {}
135 cfg = branch.get_config()
136 loc_cfg = cfg._get_location_config()
137 keys = loc_cfg._get_parser().scalars
138 result.update(get_user_bug_trackers_tags(keys))
139 br_cfg = cfg._get_branch_data_config()
140 keys = br_cfg._get_parser().scalars
141 result.update(get_user_bug_trackers_tags(keys))
142 return result