1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# Copyright 2009 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""RT ExternalBugTracker Utility."""
__metaclass__ = type
__all__ = ['RequestTracker']
import email
import urllib
import urllib2
from canonical.config import config
from canonical.launchpad.webapp.url import urlparse
from lp.bugs.externalbugtracker import (
BugNotFound,
BugTrackerConnectError,
ExternalBugTracker,
InvalidBugId,
LookupTree,
UnknownRemoteStatusError,
)
from lp.bugs.interfaces.bugtask import (
BugTaskImportance,
BugTaskStatus,
)
from lp.bugs.interfaces.externalbugtracker import UNKNOWN_REMOTE_IMPORTANCE
from lp.services.database.isolation import ensure_no_transaction
from lp.services.propertycache import cachedproperty
class RequestTracker(ExternalBugTracker):
"""`ExternalBugTracker` subclass for handling RT imports."""
ticket_url = 'REST/1.0/ticket/%s/show'
batch_url = 'REST/1.0/search/ticket/'
batch_query_threshold = 1
@property
def credentials(self):
"""Return the authentication credentials needed to log in.
If there are specific credentials for the current RT instance,
these will be returned. Otherwise the RT default guest
credentials (username and password of 'guest') will be returned.
"""
credentials_config = config['checkwatches.credentials']
hostname = urlparse(self.baseurl)[1]
try:
username = credentials_config['%s.username' % hostname]
password = credentials_config['%s.password' % hostname]
return {'user': username, 'pass': password}
except KeyError:
return {'user': 'guest', 'pass': 'guest'}
def _logIn(self, opener):
"""Attempt to log in to the remote RT service.
:param opener: An instance of urllib2.OpenerDirector
to be used to connect to the remote server.
If HTTPError or URLErrors are encountered at any point in this
process, they will be raised to be caught at the callsite.
This method is separate from the _opener property so as to allow
us to test the _opener property without having to connect to a
remote server.
"""
# To log in to an RT instance we must pass a username and
# password to its login form, as a user would from the web.
opener.open('%s/' % self.baseurl, urllib.urlencode(
self.credentials))
@cachedproperty
def _opener(self):
"""Return a urllib2.OpenerDirector for the remote RT instance.
An attempt will be made to log in to the remote instance before
the opener is returned. If logging in is not successful a
BugTrackerConnectError will be raised
"""
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
# Attempt to log in to the remote system. Raise an error if we
# can't.
try:
self._logIn(opener)
except (urllib2.HTTPError, urllib2.URLError), error:
raise BugTrackerConnectError('%s/' % self.baseurl,
"Unable to authenticate with remote RT service: "
"Could not submit login form: " +
error.message)
return opener
@ensure_no_transaction
def urlopen(self, request, data=None):
"""Return a handle to a remote resource.
This method overrides that of `ExternalBugTracker` so that the
custom URL opener for RequestTracker instances can be used.
"""
# We create our own opener so as to handle the RT authentication
# cookies that need to be passed around.
return self._opener.open(request, data)
def getRemoteBug(self, bug_id):
"""See `ExternalBugTracker`."""
ticket_url = self.ticket_url % str(bug_id)
query_url = '%s/%s' % (self.baseurl, ticket_url)
try:
bug_data = self.urlopen(query_url)
except urllib2.HTTPError, error:
raise BugTrackerConnectError(ticket_url, error.message)
# We use the first line of the response to ensure that we've
# made a successful request.
firstline = bug_data.readline().strip().split(' ')
if firstline[1] != '200':
# If anything goes wrong we raise a BugTrackerConnectError.
# We included in the error message the status code and error
# message returned by the server.
raise BugTrackerConnectError(
query_url,
"Unable to retrieve bug %s. The remote server returned the "
"following error: %s." %
(str(bug_id), " ".join(firstline[1:])))
# RT's REST interface returns tickets in RFC822 format, so we
# can use the email module to parse them.
bug = email.message_from_string(bug_data.read().strip())
if bug.get('id') is None:
return None, None
else:
bug_id = bug['id'].replace('ticket/', '')
return int(bug_id), bug
def getRemoteBugBatch(self, bug_ids):
"""See `ExternalBugTracker`."""
# We need to ensure that all the IDs are strings first.
id_list = [str(id) for id in bug_ids]
query = "id = " + "OR id = ".join(id_list)
query_url = '%s/%s' % (self.baseurl, self.batch_url)
request_params = {'query': query, 'format': 'l'}
try:
bug_data = self.urlopen(query_url, urllib.urlencode(
request_params))
except urllib2.HTTPError, error:
raise BugTrackerConnectError(query_url, error.message)
# We use the first line of the response to ensure that we've
# made a successful request.
firstline = bug_data.readline().strip().split(' ')
if firstline[1] != '200':
# If anything goes wrong we raise a BugTrackerConnectError.
# We included in the error message the status code and error
# message returned by the server.
bug_id_string = ", ".join([str(bug_id) for bug_id in bug_ids])
raise BugTrackerConnectError(
query_url,
"Unable to retrieve bugs %s. The remote server returned the "
"following error: %s." %
(bug_id_string, " ".join(firstline[1:])))
# Tickets returned in RT multiline format are separated by lines
# containing only --\n.
tickets = bug_data.read().split("--\n")
bugs = {}
for ticket in tickets:
ticket = ticket.strip()
# RT's REST interface returns tickets in RFC822 format, so we
# can use the email module to parse them.
bug = email.message_from_string(ticket)
# We only bother adding the bug to the bugs dict if we
# actually have some data worth adding.
if bug.get('id') is not None:
bug_id = bug['id'].replace('ticket/', '')
bugs[int(bug_id)] = bug
return bugs
def getRemoteStatus(self, bug_id):
"""Return the remote status of a given bug.
See `ExternalBugTracker`.
"""
try:
bug_id = int(bug_id)
except ValueError:
raise InvalidBugId(
"RequestTracker bug ids must be integers (was passed %r)"
% bug_id)
if bug_id not in self.bugs:
raise BugNotFound(bug_id)
return self.bugs[bug_id]['status']
def getRemoteImportance(self, bug_id):
"""See `IExternalBugTracker`."""
return UNKNOWN_REMOTE_IMPORTANCE
def convertRemoteImportance(self, remote_importance):
"""See `IExternalBugTracker`."""
return BugTaskImportance.UNKNOWN
_status_lookup_titles = 'RT status',
_status_lookup = LookupTree(
('new', BugTaskStatus.NEW),
('open', BugTaskStatus.CONFIRMED),
('stalled', BugTaskStatus.CONFIRMED),
('rejected', BugTaskStatus.INVALID),
('resolved', BugTaskStatus.FIXRELEASED),
)
def convertRemoteStatus(self, remote_status):
"""Convert an RT status into a Launchpad BugTaskStatus."""
try:
return self._status_lookup.find(remote_status.lower())
except KeyError:
raise UnknownRemoteStatusError(remote_status)
def getRemoteProduct(self, remote_bug):
"""Return the remote product for a remote bug.
See `IExternalBugTracker`.
"""
if remote_bug not in self.bugs:
raise BugNotFound(remote_bug)
return self.bugs[remote_bug].get('queue', None)
|