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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
Updating Product.remote_product
===============================
The remote_product attribute of a Product is used to present links for
filing and searching bugs in the Product's bug tracker, in case it's not
using Launchpad to track its bugs. We don't expect users to set the
remote_product themselves, so we have a script that tries to set this
automatically.
>>> from lp.services.webapp.interfaces import (
... IStoreSelector, DEFAULT_FLAVOR, MAIN_STORE)
>>> store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
>>> store.execute("UPDATE Product SET remote_product = 'not-None'")
<storm...>
>>> from lp.bugs.interfaces.bugtracker import BugTrackerType
>>> from lp.services.log.logger import FakeLogger, BufferLogger
>>> from lp.bugs.scripts.updateremoteproduct import (
... RemoteProductUpdater)
>>> from lp.testing.faketransaction import FakeTransaction
>>> updater = RemoteProductUpdater(FakeTransaction(), BufferLogger())
Testing
-------
To help testing, there is a method, _getExternalBugTracker(), that
creates the ExternalBugTracker for the given BugTracker.
>>> rt = factory.makeBugTracker(
... bugtrackertype=BugTrackerType.RT,
... base_url=u'http://rt.example.com/')
>>> rt_external = updater._getExternalBugTracker(rt)
>>> rt_external.__class__.__name__
'RequestTracker'
>>> rt_external.baseurl
u'http://rt.example.com'
For testing, _getExternalBugTracker() can be overridden to return an
ExternalBugTracker that doesn't require network access.
>>> class FakeExternalBugTracker:
...
... def initializeRemoteBugDB(self, bug_ids):
... print "Initializing DB for bugs: %s." % bug_ids
...
... def getRemoteProduct(self, remote_bug):
... return 'product-for-bug-%s' % remote_bug
>>> class NoNetworkRemoteProductUpdater(RemoteProductUpdater):
...
... external_bugtracker_to_return = FakeExternalBugTracker
...
... def _getExternalBugTracker(self, bug_tracker):
... return self.external_bugtracker_to_return()
update()
--------
The update method simply loops over all the bug tracker types that can
track more than one product, and calls updateByBugTrackerType(). Any bug
tracker type that isn't specified as being for a single product is being
looped over. The EMAILADDRESS one is special, though. It could be used
for more than one product, but we have no way of interacting with it, so
it's skipped as well.
>>> class TrackerTypeCollectingUpdater(RemoteProductUpdater):
... def __init__(self):
... self.logger = BufferLogger()
... self.looped_over_bug_tracker_types = set()
... def updateByBugTrackerType(self, bugtracker_type):
... self.looped_over_bug_tracker_types.add(bugtracker_type)
>>> from lp.bugs.interfaces.bugtracker import (
... SINGLE_PRODUCT_BUGTRACKERTYPES)
>>> multi_product_trackers = set(
... bugtracker_type for bugtracker_type in BugTrackerType.items
... if bugtracker_type not in SINGLE_PRODUCT_BUGTRACKERTYPES)
>>> multi_product_trackers.remove(BugTrackerType.EMAILADDRESS)
>>> updater = TrackerTypeCollectingUpdater()
>>> updater.update()
>>> multi_product_trackers.symmetric_difference(
... updater.looped_over_bug_tracker_types)
set([])
updateByBugTrackerType()
------------------------
The updateByBugTrackerType() method looks at the bug watches that are
linked to the product, to decide what remote_product should be set to.
It accepts a single parameter, the type of the bug tracker that should
be updated.
No bug watches
..............
If there are no bug watches, nothing will be done.
>>> bugzilla_product = factory.makeProduct(
... name=u'bugzilla-product', official_malone=False)
>>> bugzilla = factory.makeBugTracker(
... bugtrackertype=BugTrackerType.BUGZILLA)
>>> bugzilla_product.bugtracker = bugzilla
>>> rt_product = factory.makeProduct(
... name=u'rt-product', official_malone=False)
>>> rt = factory.makeBugTracker(
... bugtrackertype=BugTrackerType.RT)
>>> rt_product.bugtracker = rt
>>> list(bugzilla_product.getLinkedBugWatches())
[]
>>> updater.updateByBugTrackerType(BugTrackerType.RT)
>>> print bugzilla_product.remote_product
None
>>> print rt_product.remote_product
None
Linked bug watches
..................
If there are bug watches for a product having a None remote_product, an
arbitrary bug watch will be retrieved, and queried for its remote
product. Products having a bug tracker of a different type than the
given one are ignored.
>>> import transaction
>>> from lp.services.config import config
>>> from lp.testing.layers import LaunchpadZopelessLayer
>>> def switch_db_to_launchpad():
... transaction.commit()
... LaunchpadZopelessLayer.switchDbUser('launchpad')
>>> def switch_db_to_updateremoteproduct():
... transaction.commit()
... LaunchpadZopelessLayer.switchDbUser(
... config.updateremoteproduct.dbuser)
>>> updater = NoNetworkRemoteProductUpdater(
... FakeTransaction(), BufferLogger())
>>> switch_db_to_launchpad()
>>> bugzilla_bugtask = factory.makeBugTask(target=bugzilla_product)
>>> bugzilla_bugwatch = factory.makeBugWatch(
... '42', bugtracker=bugzilla, bug=bugzilla_bugtask.bug)
>>> bugzilla_bugtask.bugwatch = bugzilla_bugwatch
>>> rt_bugtask = factory.makeBugTask(target=rt_product)
>>> rt_bugwatch = factory.makeBugWatch(
... '84', bugtracker=rt, bug=rt_bugtask.bug)
>>> rt_bugtask.bugwatch = rt_bugwatch
>>> switch_db_to_updateremoteproduct()
>>> updater.updateByBugTrackerType(BugTrackerType.RT)
Initializing DB for bugs: [u'84'].
>>> print rt_product.remote_product
product-for-bug-84
>>> print bugzilla_product.remote_product
None
remote_product already set
..........................
If a product already has remote_product set, it will not be updated.
>>> switch_db_to_launchpad()
>>> rt_product = factory.makeProduct(official_malone=False)
>>> rt = factory.makeBugTracker(
... bugtrackertype=BugTrackerType.RT)
>>> rt_product.bugtracker = rt
>>> rt_bugtask = factory.makeBugTask(target=rt_product)
>>> rt_bugwatch = factory.makeBugWatch(
... '84', bugtracker=rt, bug=rt_bugtask.bug)
>>> rt_bugtask.bugwatch = rt_bugwatch
>>> switch_db_to_updateremoteproduct()
>>> rt_product.remote_product = u'already-set'
>>> updater = NoNetworkRemoteProductUpdater(
... FakeTransaction(), BufferLogger())
>>> updater.updateByBugTrackerType(BugTrackerType.RT)
>>> print rt_product.remote_product
already-set
Transaction handling
....................
To avoid long-running write transactions, the transaction is committed
after each product's remote_product has been updated.
>>> switch_db_to_launchpad()
>>> for index in range(3):
... rt_product = factory.makeProduct(official_malone=False)
... rt = factory.makeBugTracker(
... bugtrackertype=BugTrackerType.RT)
... rt_product.bugtracker = rt
... rt_bugtask = factory.makeBugTask(target=rt_product)
... rt_bugwatch = factory.makeBugWatch(
... '84', bugtracker=rt, bug=rt_bugtask.bug)
... rt_bugtask.bugwatch = rt_bugwatch
>>> switch_db_to_updateremoteproduct()
>>> updater = NoNetworkRemoteProductUpdater(
... FakeTransaction(log_calls=True), BufferLogger())
>>> updater.print_method_calls = False
>>> updater.updateByBugTrackerType(BugTrackerType.RT)
Initializing DB for bugs: [u'84'].
COMMIT
Initializing DB for bugs: [u'84'].
COMMIT
Initializing DB for bugs: [u'84'].
COMMIT
Error handling
..............
If the ExternalBugTracker raises any BugWatchUpdateErrors,
updateByBugTrackerType() will simply log the error and then continue.
This is a simplistic approach but it means that problems with one bug
tracker don't break the run for all bug trackers.
>>> switch_db_to_launchpad()
>>> new_rt_product = factory.makeProduct(
... name='fooix', official_malone=False)
>>> new_rt_product.bugtracker = rt
>>> new_rt_bugtask = factory.makeBugTask(target=new_rt_product)
>>> new_rt_bugwatch = factory.makeBugWatch(
... '42', bugtracker=rt, bug=new_rt_bugtask.bug)
>>> new_rt_bugtask.bugwatch = new_rt_bugwatch
>>> switch_db_to_updateremoteproduct()
>>> from lp.bugs.externalbugtracker.base import (
... BugNotFound, BugWatchUpdateError)
>>> class BrokenOnInitExternalBugTracker(
... FakeExternalBugTracker):
... def initializeRemoteBugDB(self, bug_ids):
... raise BugWatchUpdateError("This here is an error")
>>> updater.logger = FakeLogger()
>>> updater.external_bugtracker_to_return = (
... BrokenOnInitExternalBugTracker)
>>> updater.updateByBugTrackerType(BugTrackerType.RT)
INFO 1 projects using RT needing updating.
DEBUG Trying to update fooix
ERROR Unable to set remote_product for 'fooix': This here is an error
>>> class BrokenOnGetRemoteProductExternalBugTracker(
... FakeExternalBugTracker):
... def getRemoteProduct(self, remote_bug):
... raise BugNotFound("Didn't find bug %s." % remote_bug)
>>> updater.external_bugtracker_to_return = (
... BrokenOnGetRemoteProductExternalBugTracker)
>>> updater.updateByBugTrackerType(BugTrackerType.RT)
INFO 1 projects using RT needing updating.
DEBUG Trying to update fooix
Initializing DB for bugs: [u'42'].
ERROR Unable to set remote_product for 'fooix': Didn't find bug 42.
AssertionErrors are also handled.
>>> class RaisesAssertionErrorExternalBugTracker(FakeExternalBugTracker):
... def initializeRemoteBugDB(self, bug_ids):
... assert True == False, "True isn't False!"
>>> updater.external_bugtracker_to_return = (
... RaisesAssertionErrorExternalBugTracker)
>>> updater.updateByBugTrackerType(BugTrackerType.RT)
INFO 1 projects using RT needing updating.
DEBUG Trying to update fooix
ERROR Unable to set remote_product for 'fooix': True isn't False!
|