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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
|
# Copyright 2009 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
# pylint: disable-msg=E0611,W0212
"""Launchpad bug-related database table classes."""
__metaclass__ = type
__all__ = [
'Bug',
'BugAffectsPerson',
'BugBecameQuestionEvent',
'BugSet',
'BugTag',
'FileBugData',
'get_bug_tags',
'get_bug_tags_open_count',
]
import operator
import re
from cStringIO import StringIO
from datetime import datetime, timedelta
from email.Utils import make_msgid
from pytz import timezone
from zope.contenttype import guess_content_type
from zope.component import getUtility
from zope.event import notify
from zope.interface import implements, providedBy
from sqlobject import BoolCol, IntCol, ForeignKey, StringCol
from sqlobject import SQLMultipleJoin, SQLRelatedJoin
from sqlobject import SQLObjectNotFound
from storm.expr import (
And, Count, Func, In, LeftJoin, Max, Not, Or, Select, SQL, SQLRaw, Union)
from storm.store import EmptyResultSet, Store
from lazr.lifecycle.event import (
ObjectCreatedEvent, ObjectDeletedEvent, ObjectModifiedEvent)
from lazr.lifecycle.snapshot import Snapshot
from canonical.database.constants import UTC_NOW
from canonical.database.datetimecol import UtcDateTimeCol
from canonical.database.sqlbase import cursor, SQLBase, sqlvalues
from canonical.launchpad.database.librarian import LibraryFileAlias
from canonical.launchpad.database.message import (
Message, MessageChunk, MessageSet)
from canonical.launchpad.fields import DuplicateBug
from canonical.launchpad.helpers import shortlist
from lp.hardwaredb.interfaces.hwdb import IHWSubmissionBugSet
from canonical.launchpad.interfaces.launchpad import ILaunchpadCelebrities
from canonical.launchpad.interfaces.librarian import ILibraryFileAliasSet
from canonical.launchpad.interfaces.lpstorm import IStore
from canonical.launchpad.interfaces.message import (
IMessage, IndexedMessage)
from lp.registry.interfaces.structuralsubscription import (
BugNotificationLevel, IStructuralSubscriptionTarget)
from lp.bugs.mail.bugnotificationrecipients import BugNotificationRecipients
from canonical.launchpad.validators import LaunchpadValidationError
from canonical.launchpad.webapp.interfaces import (
IStoreSelector, DEFAULT_FLAVOR, MAIN_STORE, NotFoundError)
from lp.answers.interfaces.questiontarget import IQuestionTarget
from lp.app.errors import UserCannotUnsubscribePerson
from lp.bugs.adapters.bugchange import (
BranchLinkedToBug, BranchUnlinkedFromBug, BugConvertedToQuestion,
BugWatchAdded, BugWatchRemoved, SeriesNominated, UnsubscribedFromBug)
from lp.bugs.interfaces.bug import (
IBug, IBugBecameQuestionEvent, IBugSet, IFileBugData,
InvalidDuplicateValue)
from lp.bugs.interfaces.bugactivity import IBugActivitySet
from lp.bugs.interfaces.bugattachment import (
BugAttachmentType, IBugAttachmentSet)
from lp.bugs.interfaces.bugmessage import IBugMessageSet
from lp.bugs.interfaces.bugnomination import (
NominationError, NominationSeriesObsoleteError)
from lp.bugs.interfaces.bugnotification import IBugNotificationSet
from lp.bugs.interfaces.bugtask import (
BugTaskStatus, IBugTaskSet, UNRESOLVED_BUGTASK_STATUSES)
from lp.bugs.interfaces.bugtracker import BugTrackerType
from lp.bugs.interfaces.bugwatch import IBugWatchSet
from lp.bugs.interfaces.cve import ICveSet
from lp.bugs.model.bugattachment import BugAttachment
from lp.bugs.model.bugbranch import BugBranch
from lp.bugs.model.bugcve import BugCve
from lp.bugs.model.bugmessage import BugMessage
from lp.bugs.model.bugnomination import BugNomination
from lp.bugs.model.bugnotification import BugNotification
from lp.bugs.model.bugsubscription import BugSubscription
from lp.bugs.model.bugtask import (
BugTask, BugTaskSet, NullBugTask, bugtask_sort_key,
get_bug_privacy_filter)
from lp.bugs.model.bugwatch import BugWatch
from lp.registry.interfaces.distribution import IDistribution
from lp.registry.interfaces.distributionsourcepackage import (
IDistributionSourcePackage)
from lp.registry.interfaces.series import SeriesStatus
from lp.registry.interfaces.distroseries import IDistroSeries
from lp.registry.interfaces.person import IPersonSet
from lp.registry.interfaces.person import validate_public_person
from lp.registry.interfaces.product import IProduct
from lp.registry.interfaces.productseries import IProductSeries
from lp.registry.interfaces.sourcepackage import ISourcePackage
from lp.registry.model.mentoringoffer import MentoringOffer
from lp.registry.model.person import Person, ValidPersonCache
from lp.registry.model.pillar import pillar_sort_key
_bug_tag_query_template = """
SELECT %(columns)s FROM %(tables)s WHERE
%(condition)s GROUP BY BugTag.tag ORDER BY BugTag.tag"""
def get_bug_tags(context_clause):
"""Return all the bug tags as a list of strings.
context_clause is a SQL condition clause, limiting the tags to a
specific context. The SQL clause can only use the BugTask table to
choose the context.
"""
from_tables = ['BugTag', 'BugTask']
select_columns = ['BugTag.tag']
conditions = ['BugTag.bug = BugTask.bug', '(%s)' % context_clause]
cur = cursor()
cur.execute(_bug_tag_query_template % dict(
columns=', '.join(select_columns),
tables=', '.join(from_tables),
condition=' AND '.join(conditions)))
return shortlist([row[0] for row in cur.fetchall()])
def get_bug_tags_open_count(context_condition, user):
"""Return all the used bug tags with their open bug count.
:param context_condition: A Storm SQL expression, limiting the
used tags to a specific context. Only the BugTask table may be
used to choose the context.
:param user: The user performing the search.
:return: A list of tuples, (tag name, open bug count).
"""
open_statuses_condition = In(
BugTask.status, sqlvalues(*UNRESOLVED_BUGTASK_STATUSES))
columns = [
BugTag.tag,
Count(),
]
tables = [
BugTag,
LeftJoin(Bug, Bug.id == BugTag.bugID),
LeftJoin(
BugTask,
And(BugTask.bugID == Bug.id, open_statuses_condition)),
]
where_conditions = [
open_statuses_condition,
context_condition,
]
privacy_filter = get_bug_privacy_filter(user)
if privacy_filter:
where_conditions.append(SQLRaw(privacy_filter))
store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
result = store.execute(Select(
columns=columns, where=And(*where_conditions), tables=tables,
group_by=BugTag.tag, order_by=BugTag.tag))
return shortlist([(row[0], row[1]) for row in result.get_all()])
def snapshot_bug_params(bug_params):
"""Return a snapshot of a `CreateBugParams` object."""
return Snapshot(
bug_params, names=[
"owner", "title", "comment", "description", "msg",
"datecreated", "security_related", "private",
"distribution", "sourcepackagename", "binarypackagename",
"product", "status", "subscribers", "tags",
"subscribe_owner", "filed_by"])
class BugTag(SQLBase):
"""A tag belonging to a bug."""
bug = ForeignKey(dbName='bug', foreignKey='Bug', notNull=True)
tag = StringCol(notNull=True)
class BugBecameQuestionEvent:
"""See `IBugBecameQuestionEvent`."""
implements(IBugBecameQuestionEvent)
def __init__(self, bug, question, user):
self.bug = bug
self.question = question
self.user = user
class Bug(SQLBase):
"""A bug."""
implements(IBug)
_defaultOrder = '-id'
# db field names
name = StringCol(unique=True, default=None)
title = StringCol(notNull=True)
description = StringCol(notNull=False,
default=None)
owner = ForeignKey(
dbName='owner', foreignKey='Person',
storm_validator=validate_public_person, notNull=True)
duplicateof = ForeignKey(
dbName='duplicateof', foreignKey='Bug', default=None)
datecreated = UtcDateTimeCol(notNull=True, default=UTC_NOW)
date_last_updated = UtcDateTimeCol(notNull=True, default=UTC_NOW)
private = BoolCol(notNull=True, default=False)
date_made_private = UtcDateTimeCol(notNull=False, default=None)
who_made_private = ForeignKey(
dbName='who_made_private', foreignKey='Person',
storm_validator=validate_public_person, default=None)
security_related = BoolCol(notNull=True, default=False)
# useful Joins
activity = SQLMultipleJoin('BugActivity', joinColumn='bug', orderBy='id')
messages = SQLRelatedJoin('Message', joinColumn='bug',
otherColumn='message',
intermediateTable='BugMessage',
prejoins=['owner'],
orderBy=['datecreated', 'id'])
bug_messages = SQLMultipleJoin(
'BugMessage', joinColumn='bug', orderBy='id')
watches = SQLMultipleJoin(
'BugWatch', joinColumn='bug', orderBy=['bugtracker', 'remotebug'])
cves = SQLRelatedJoin('Cve', intermediateTable='BugCve',
orderBy='sequence', joinColumn='bug', otherColumn='cve')
cve_links = SQLMultipleJoin('BugCve', joinColumn='bug', orderBy='id')
mentoring_offers = SQLMultipleJoin(
'MentoringOffer', joinColumn='bug', orderBy='id')
# XXX: kiko 2006-09-23: Why is subscriptions ordered by ID?
subscriptions = SQLMultipleJoin(
'BugSubscription', joinColumn='bug', orderBy='id',
prejoins=["person"])
duplicates = SQLMultipleJoin(
'Bug', joinColumn='duplicateof', orderBy='id')
specifications = SQLRelatedJoin('Specification', joinColumn='bug',
otherColumn='specification', intermediateTable='SpecificationBug',
orderBy='-datecreated')
questions = SQLRelatedJoin('Question', joinColumn='bug',
otherColumn='question', intermediateTable='QuestionBug',
orderBy='-datecreated')
linked_branches = SQLMultipleJoin(
'BugBranch', joinColumn='bug', orderBy='id')
date_last_message = UtcDateTimeCol(default=None)
number_of_duplicates = IntCol(notNull=True, default=0)
message_count = IntCol(notNull=True, default=0)
users_affected_count = IntCol(notNull=True, default=0)
users_unaffected_count = IntCol(notNull=True, default=0)
heat = IntCol(notNull=True, default=0)
heat_last_updated = UtcDateTimeCol(default=None)
latest_patch_uploaded = UtcDateTimeCol(default=None)
@property
def latest_patch(self):
"""See `IBug`."""
# We want to retrieve the most recently added bug attachment
# that is of type BugAttachmentType.PATCH. In order to find
# this attachment, we should in theory sort by
# BugAttachment.message.datecreated. Since we don't have
# an index for Message.datecreated, such a query would be
# quite slow. We search instead for the BugAttachment with
# the largest ID for a given bug. This is "nearly" equivalent
# to searching the record with the maximum value of
# message.datecreated: The only exception is the rare case when
# two BugAttachment records are simultaneuosly added to the same
# bug, where bug_attachment_1.id < bug_attachment_2.id, while
# the Message record for bug_attachment_2 is created before
# the Message record for bug_attachment_1. The difference of
# the datecreated values of the Message records is in this case
# probably smaller than one second and the selection of the
# "most recent" patch anyway somewhat arbitrary.
return Store.of(self).find(
BugAttachment, BugAttachment.id == Select(
Max(BugAttachment.id),
And(BugAttachment.bug == self.id,
BugAttachment.type == BugAttachmentType.PATCH))).one()
@property
def comment_count(self):
"""See `IBug`."""
return self.message_count - 1
@property
def users_affected(self):
"""See `IBug`."""
return Store.of(self).find(
Person, BugAffectsPerson.person == Person.id,
BugAffectsPerson.affected,
BugAffectsPerson.bug == self)
@property
def users_unaffected(self):
"""See `IBug`."""
return Store.of(self).find(
Person, BugAffectsPerson.person == Person.id,
Not(BugAffectsPerson.affected),
BugAffectsPerson.bug == self)
@property
def user_ids_affected_with_dupes(self):
"""Return all IDs of Persons affected by this bug and its dupes.
The return value is a Storm expression. Running a query with
this expression returns a result that may contain the same ID
multiple times, for example if that person is affected via
more than one duplicate."""
return Union(
Select(Person.id,
And(BugAffectsPerson.person == Person.id,
BugAffectsPerson.affected,
BugAffectsPerson.bug == self)),
Select(Person.id,
And(BugAffectsPerson.person == Person.id,
BugAffectsPerson.bug == Bug.id,
BugAffectsPerson.affected,
Bug.duplicateof == self.id)))
@property
def users_affected_with_dupes(self):
"""See `IBug`."""
return Store.of(self).find(
Person,
In(Person.id, self.user_ids_affected_with_dupes))
@property
def users_affected_count_with_dupes(self):
"""See `IBug`."""
return self.users_affected_with_dupes.count()
@property
def indexed_messages(self):
"""See `IMessageTarget`."""
inside = self.default_bugtask
messages = list(self.messages)
message_set = set(messages)
indexed_messages = []
for index, message in enumerate(messages):
if message.parent not in message_set:
parent = None
else:
parent = message.parent
indexed_message = IndexedMessage(message, inside, index, parent)
indexed_messages.append(indexed_message)
return indexed_messages
@property
def displayname(self):
"""See `IBug`."""
dn = 'Bug #%d' % self.id
if self.name:
dn += ' ('+self.name+')'
return dn
@property
def bugtasks(self):
"""See `IBug`."""
result = BugTask.select('BugTask.bug = %s' % sqlvalues(self.id))
result = result.prejoin(
["assignee", "product", "sourcepackagename",
"owner", "bugwatch"])
# Do not use the default orderBy as the prejoins cause ambiguities
# across the tables.
result = result.orderBy("id")
return sorted(result, key=bugtask_sort_key)
@property
def default_bugtask(self):
"""See `IBug`."""
return Store.of(self).find(
BugTask, bug=self).order_by(BugTask.id).first()
@property
def is_complete(self):
"""See `IBug`."""
for task in self.bugtasks:
if not task.is_complete:
return False
return True
@property
def affected_pillars(self):
"""See `IBug`."""
result = set()
for task in self.bugtasks:
result.add(task.pillar)
return sorted(result, key=pillar_sort_key)
@property
def permits_expiration(self):
"""See `IBug`.
This property checks the general state of the bug to determine if
expiration is permitted *if* a bugtask were to qualify for expiration.
This property does not check the bugtask preconditions to identify
a specific bugtask that can expire.
:See: `IBug.can_expire` or `BugTaskSet.findExpirableBugTasks` to
check or get a list of bugs that can expire.
"""
# Bugs cannot be expired if any bugtask is valid.
expirable_status_list = [
BugTaskStatus.INCOMPLETE, BugTaskStatus.INVALID,
BugTaskStatus.WONTFIX]
has_an_expirable_bugtask = False
for bugtask in self.bugtasks:
if bugtask.status not in expirable_status_list:
# We found an unexpirable bugtask; the bug cannot expire.
return False
if (bugtask.status == BugTaskStatus.INCOMPLETE
and bugtask.pillar.enable_bug_expiration):
# This bugtasks meets the basic conditions to expire.
has_an_expirable_bugtask = True
return has_an_expirable_bugtask
@property
def can_expire(self):
"""See `IBug`.
Only Incomplete bug reports that affect a single pillar with
enabled_bug_expiration set to True can be expired. To qualify for
expiration, the bug and its bugtasks meet the follow conditions:
1. The bug is inactive; the last update of the is older than
Launchpad expiration age.
2. The bug is not a duplicate.
3. The bug has at least one message (a request for more information).
4. The bug does not have any other valid bugtasks.
5. The bugtask belongs to a project with enable_bug_expiration set
to True.
6. The bugtask has the status Incomplete.
7. The bugtask is not assigned to anyone.
8. The bugtask does not have a milestone.
"""
# IBugTaskSet.findExpirableBugTasks() is the authoritative determiner
# if a bug can expire, but it is expensive. We do a general check
# to verify the bug permits expiration before using IBugTaskSet to
# determine if a bugtask can cause expiration.
if not self.permits_expiration:
return False
# Do the search as the Janitor, to ensure that this bug can be
# found, even if it's private. We don't have access to the user
# calling this property. If the user has access to view this
# property, he has permission to see the bug, so we're not
# exposing something we shouldn't. The Janitor has access to
# view all bugs.
bugtasks = getUtility(IBugTaskSet).findExpirableBugTasks(
0, getUtility(ILaunchpadCelebrities).janitor, bug=self)
return bugtasks.count() > 0
@property
def initial_message(self):
"""See `IBug`."""
messages = sorted(self.messages, key=lambda ob: ob.id)
return messages[0]
def followup_subject(self):
"""See `IBug`."""
return 'Re: '+ self.title
@property
def has_patches(self):
"""See `IBug`."""
return self.latest_patch_uploaded is not None
def subscribe(self, person, subscribed_by, suppress_notify=True):
"""See `IBug`."""
# first look for an existing subscription
for sub in self.subscriptions:
if sub.person.id == person.id:
return sub
sub = BugSubscription(
bug=self, person=person, subscribed_by=subscribed_by)
# Ensure that the subscription has been flushed.
Store.of(sub).flush()
# In some cases, a subscription should be created without
# email notifications. suppress_notify determines if
# notifications are sent.
if suppress_notify is False:
notify(ObjectCreatedEvent(sub, user=subscribed_by))
self.updateHeat()
return sub
def unsubscribe(self, person, unsubscribed_by):
"""See `IBug`."""
if person is None:
person = unsubscribed_by
for sub in self.subscriptions:
if sub.person.id == person.id:
if not sub.canBeUnsubscribedByUser(unsubscribed_by):
raise UserCannotUnsubscribePerson(
'%s does not have permission to unsubscribe %s.' % (
unsubscribed_by.displayname,
person.displayname))
self.addChange(UnsubscribedFromBug(
when=UTC_NOW, person=unsubscribed_by,
unsubscribed_user=person))
store = Store.of(sub)
store.remove(sub)
# Make sure that the subscription removal has been
# flushed so that code running with implicit flushes
# disabled see the change.
store.flush()
self.updateHeat()
return
def unsubscribeFromDupes(self, person, unsubscribed_by):
"""See `IBug`."""
if person is None:
person = unsubscribed_by
bugs_unsubscribed = []
for dupe in self.duplicates:
if dupe.isSubscribed(person):
dupe.unsubscribe(person, unsubscribed_by)
bugs_unsubscribed.append(dupe)
return bugs_unsubscribed
def isSubscribed(self, person):
"""See `IBug`."""
if person is None:
return False
bs = BugSubscription.selectBy(bug=self, person=person)
return bool(bs)
def isSubscribedToDupes(self, person):
"""See `IBug`."""
if person is None:
return False
return bool(
BugSubscription.select("""
bug IN (SELECT id FROM Bug WHERE duplicateof = %d) AND
person = %d""" % (self.id, person.id)))
def getDirectSubscriptions(self):
"""See `IBug`."""
# Cache valid persons so that <person>.is_valid_person can
# return from the cache. This operation was previously done at
# the same time as retrieving the bug subscriptions (as a left
# join). However, this ran slowly (far from optimal query
# plan), so we're doing it as two queries now.
valid_persons = Store.of(self).find(
(Person, ValidPersonCache),
Person.id == ValidPersonCache.id,
ValidPersonCache.id == BugSubscription.personID,
BugSubscription.bug == self)
# Suck in all the records so that they're actually cached.
list(valid_persons)
# Do the main query.
return Store.of(self).find(
BugSubscription,
BugSubscription.personID == Person.id,
BugSubscription.bug == self).order_by(
Func('person_sort_key', Person.displayname, Person.name))
def getDirectSubscribers(self, recipients=None):
"""See `IBug`.
The recipients argument is private and not exposed in the
interface. If a BugNotificationRecipients instance is supplied,
the relevant subscribers and rationales will be registered on
it.
"""
subscribers = list(
Person.select("""
Person.id = BugSubscription.person AND
BugSubscription.bug = %d""" % self.id,
orderBy="displayname", clauseTables=["BugSubscription"]))
if recipients is not None:
for subscriber in subscribers:
recipients.addDirectSubscriber(subscriber)
return subscribers
def getIndirectSubscribers(self, recipients=None, level=None):
"""See `IBug`.
See the comment in getDirectSubscribers for a description of the
recipients argument.
"""
# "Also notified" and duplicate subscribers are mutually
# exclusive, so return both lists.
indirect_subscribers = (
self.getAlsoNotifiedSubscribers(recipients, level) +
self.getSubscribersFromDuplicates(recipients, level))
return sorted(
indirect_subscribers, key=operator.attrgetter("displayname"))
def getSubscriptionsFromDuplicates(self, recipients=None):
"""See `IBug`."""
if self.private:
return []
duplicate_subscriptions = set(
BugSubscription.select("""
BugSubscription.bug = Bug.id AND
Bug.duplicateof = %d""" % self.id,
prejoins=["person"], clauseTables=["Bug"]))
# Only add a subscriber once to the list.
duplicate_subscribers = set(
sub.person for sub in duplicate_subscriptions)
subscriptions = []
for duplicate_subscriber in duplicate_subscribers:
for duplicate_subscription in duplicate_subscriptions:
if duplicate_subscription.person == duplicate_subscriber:
subscriptions.append(duplicate_subscription)
break
def get_person_displayname(subscription):
return subscription.person.displayname
return sorted(subscriptions, key=get_person_displayname)
def getSubscribersFromDuplicates(self, recipients=None, level=None):
"""See `IBug`.
See the comment in getDirectSubscribers for a description of the
recipients argument.
"""
if self.private:
return []
dupe_subscribers = set(
Person.select("""
Person.id = BugSubscription.person AND
BugSubscription.bug = Bug.id AND
Bug.duplicateof = %d""" % self.id,
clauseTables=["Bug", "BugSubscription"]))
# Direct and "also notified" subscribers take precedence over
# subscribers from dupes. Note that we don't supply recipients
# here because we are doing this to /remove/ subscribers.
dupe_subscribers -= set(self.getDirectSubscribers())
dupe_subscribers -= set(self.getAlsoNotifiedSubscribers(level=level))
if recipients is not None:
for subscriber in dupe_subscribers:
recipients.addDupeSubscriber(subscriber)
return sorted(
dupe_subscribers, key=operator.attrgetter("displayname"))
def getAlsoNotifiedSubscribers(self, recipients=None, level=None):
"""See `IBug`.
See the comment in getDirectSubscribers for a description of the
recipients argument.
"""
if self.private:
return []
also_notified_subscribers = set()
structural_subscription_targets = set()
for bugtask in self.bugtasks:
if bugtask.assignee:
also_notified_subscribers.add(bugtask.assignee)
if recipients is not None:
recipients.addAssignee(bugtask.assignee)
if IStructuralSubscriptionTarget.providedBy(bugtask.target):
structural_subscription_targets.add(bugtask.target)
if bugtask.target.parent_subscription_target is not None:
structural_subscription_targets.add(
bugtask.target.parent_subscription_target)
if ISourcePackage.providedBy(bugtask.target):
# Distribution series bug tasks with a package have the
# source package set as their target, so we add the
# distroseries explicitly to the set of subscription
# targets.
structural_subscription_targets.add(
bugtask.distroseries)
if bugtask.milestone is not None:
structural_subscription_targets.add(bugtask.milestone)
# If the target's bug supervisor isn't set,
# we add the owner as a subscriber.
pillar = bugtask.pillar
if pillar.bug_supervisor is None:
also_notified_subscribers.add(pillar.owner)
if recipients is not None:
recipients.addRegistrant(pillar.owner, pillar)
person_set = getUtility(IPersonSet)
target_subscribers = person_set.getSubscribersForTargets(
structural_subscription_targets, recipients=recipients,
level=level)
also_notified_subscribers.update(target_subscribers)
# Direct subscriptions always take precedence over indirect
# subscriptions.
direct_subscribers = set(self.getDirectSubscribers())
return sorted(
(also_notified_subscribers - direct_subscribers),
key=operator.attrgetter('displayname'))
def getBugNotificationRecipients(self, duplicateof=None, old_bug=None,
level=None,
include_master_dupe_subscribers=False):
"""See `IBug`."""
recipients = BugNotificationRecipients(duplicateof=duplicateof)
self.getDirectSubscribers(recipients)
if self.private:
assert self.getIndirectSubscribers() == [], (
"Indirect subscribers found on private bug. "
"A private bug should never have implicit subscribers!")
else:
self.getIndirectSubscribers(recipients, level=level)
if include_master_dupe_subscribers and self.duplicateof:
# This bug is a public duplicate of another bug, so include
# the dupe target's subscribers in the recipient list. Note
# that we only do this for duplicate bugs that are public;
# changes in private bugs are not broadcast to their dupe
# targets.
dupe_recipients = (
self.duplicateof.getBugNotificationRecipients(
duplicateof=self.duplicateof, level=level))
recipients.update(dupe_recipients)
# XXX Tom Berger 2008-03-18:
# We want to look up the recipients for `old_bug` too,
# but for this to work, this code has to move out of the
# class and into a free function, since `old_bug` is a
# `Snapshot`, and doesn't have any of the methods of the
# original `Bug`.
return recipients
def addChangeNotification(self, text, person, recipients=None, when=None):
"""See `IBug`."""
if recipients is None:
recipients = self.getBugNotificationRecipients(
level=BugNotificationLevel.METADATA)
if when is None:
when = UTC_NOW
message = MessageSet().fromText(
self.followup_subject(), text, owner=person, datecreated=when)
getUtility(IBugNotificationSet).addNotification(
bug=self, is_comment=False,
message=message, recipients=recipients)
def addCommentNotification(self, message, recipients=None):
"""See `IBug`."""
if recipients is None:
recipients = self.getBugNotificationRecipients(
level=BugNotificationLevel.COMMENTS)
getUtility(IBugNotificationSet).addNotification(
bug=self, is_comment=True,
message=message, recipients=recipients)
def addChange(self, change, recipients=None):
"""See `IBug`."""
when = change.when
if when is None:
when = UTC_NOW
# Only try to add something to the activity log if we have some
# data.
activity_data = change.getBugActivity()
if activity_data is not None:
getUtility(IBugActivitySet).new(
self, when, change.person,
activity_data['whatchanged'],
activity_data.get('oldvalue'),
activity_data.get('newvalue'),
activity_data.get('message'))
notification_data = change.getBugNotification()
if notification_data is not None:
assert notification_data.get('text') is not None, (
"notification_data must include a `text` value.")
self.addChangeNotification(
notification_data['text'], change.person, recipients,
when)
self.updateHeat()
def expireNotifications(self):
"""See `IBug`."""
for notification in BugNotification.selectBy(
bug=self, date_emailed=None):
notification.date_emailed = UTC_NOW
notification.syncUpdate()
def newMessage(self, owner=None, subject=None,
content=None, parent=None, bugwatch=None,
remote_comment_id=None):
"""Create a new Message and link it to this bug."""
if subject is None:
subject = self.followup_subject()
msg = Message(
parent=parent, owner=owner, subject=subject,
rfc822msgid=make_msgid('malone'))
MessageChunk(message=msg, content=content, sequence=1)
bugmsg = self.linkMessage(
msg, bugwatch, remote_comment_id=remote_comment_id)
if not bugmsg:
return
notify(ObjectCreatedEvent(bugmsg, user=owner))
return bugmsg.message
def linkMessage(self, message, bugwatch=None, user=None,
remote_comment_id=None):
"""See `IBug`."""
if message not in self.messages:
if user is None:
user = message.owner
result = BugMessage(bug=self, message=message,
bugwatch=bugwatch, remote_comment_id=remote_comment_id)
getUtility(IBugWatchSet).fromText(
message.text_contents, self, user)
self.findCvesInText(message.text_contents, user)
# XXX 2008-05-27 jamesh:
# Ensure that BugMessages get flushed in same order as
# they are created.
Store.of(result).flush()
return result
def addTask(self, owner, target):
"""See `IBug`."""
product = None
product_series = None
distribution = None
distro_series = None
source_package_name = None
# Turn `target` into something more useful.
if IProduct.providedBy(target):
product = target
if IProductSeries.providedBy(target):
product_series = target
if IDistribution.providedBy(target):
distribution = target
if IDistroSeries.providedBy(target):
distro_series = target
if IDistributionSourcePackage.providedBy(target):
distribution = target.distribution
source_package_name = target.sourcepackagename
if ISourcePackage.providedBy(target):
if target.distroseries is not None:
distro_series = target.distroseries
source_package_name = target.sourcepackagename
elif target.distribution is not None:
distribution = target.distribution
source_package_name = target.sourcepackagename
else:
source_package_name = target.sourcepackagename
new_task = getUtility(IBugTaskSet).createTask(
self, owner=owner, product=product,
productseries=product_series, distribution=distribution,
distroseries=distro_series,
sourcepackagename=source_package_name)
# When a new task is added the bug's heat becomes relevant to the
# target's max_bug_heat.
target.recalculateBugHeatCache()
return new_task
def addWatch(self, bugtracker, remotebug, owner):
"""See `IBug`."""
# We shouldn't add duplicate bug watches.
bug_watch = self.getBugWatch(bugtracker, remotebug)
if bug_watch is None:
bug_watch = BugWatch(
bug=self, bugtracker=bugtracker,
remotebug=remotebug, owner=owner)
Store.of(bug_watch).flush()
self.addChange(BugWatchAdded(UTC_NOW, owner, bug_watch))
notify(ObjectCreatedEvent(bug_watch, user=owner))
return bug_watch
def removeWatch(self, bug_watch, user):
"""See `IBug`."""
self.addChange(BugWatchRemoved(UTC_NOW, user, bug_watch))
bug_watch.destroySelf()
def addAttachment(self, owner, data, comment, filename, is_patch=False,
content_type=None, description=None):
"""See `IBug`."""
if isinstance(data, str):
filecontent = data
else:
filecontent = data.read()
if is_patch:
content_type = 'text/plain'
else:
if content_type is None:
content_type, encoding = guess_content_type(
name=filename, body=filecontent)
filealias = getUtility(ILibraryFileAliasSet).create(
name=filename, size=len(filecontent),
file=StringIO(filecontent), contentType=content_type)
return self.linkAttachment(
owner, filealias, comment, is_patch, description)
def linkAttachment(self, owner, file_alias, comment, is_patch=False,
description=None):
if is_patch:
attach_type = BugAttachmentType.PATCH
else:
attach_type = BugAttachmentType.UNSPECIFIED
if description:
title = description
else:
title = file_alias.filename
if IMessage.providedBy(comment):
message = comment
else:
message = self.newMessage(
owner=owner, subject=description, content=comment)
return getUtility(IBugAttachmentSet).create(
bug=self, filealias=file_alias, attach_type=attach_type,
title=title, message=message, send_notifications=True)
def hasBranch(self, branch):
"""See `IBug`."""
branch = BugBranch.selectOneBy(branch=branch, bug=self)
return branch is not None
def linkBranch(self, branch, registrant):
"""See `IBug`."""
for bug_branch in shortlist(self.linked_branches):
if bug_branch.branch == branch:
return bug_branch
bug_branch = BugBranch(
branch=branch, bug=self, registrant=registrant)
branch.date_last_modified = UTC_NOW
self.addChange(BranchLinkedToBug(UTC_NOW, registrant, branch, self))
notify(ObjectCreatedEvent(bug_branch))
return bug_branch
def unlinkBranch(self, branch, user):
"""See `IBug`."""
bug_branch = BugBranch.selectOneBy(bug=self, branch=branch)
if bug_branch is not None:
self.addChange(BranchUnlinkedFromBug(UTC_NOW, user, branch, self))
notify(ObjectDeletedEvent(bug_branch, user=user))
bug_branch.destroySelf()
def linkCVE(self, cve, user):
"""See `IBug`."""
if cve not in self.cves:
bugcve = BugCve(bug=self, cve=cve)
notify(ObjectCreatedEvent(bugcve, user=user))
return bugcve
# XXX intellectronica 2008-11-06 Bug #294858:
# See lp.bugs.interfaces.bug
def linkCVEAndReturnNothing(self, cve, user):
"""See `IBug`."""
self.linkCVE(cve, user)
return None
def unlinkCVE(self, cve, user):
"""See `IBug`."""
for cve_link in self.cve_links:
if cve_link.cve.id == cve.id:
notify(ObjectDeletedEvent(cve_link, user=user))
BugCve.delete(cve_link.id)
break
def findCvesInText(self, text, user):
"""See `IBug`."""
cves = getUtility(ICveSet).inText(text)
for cve in cves:
self.linkCVE(cve, user)
# Several other classes need to generate lists of bugs, and
# one thing they often have to filter for is completeness. We maintain
# this single canonical query string here so that it does not have to be
# cargo culted into Product, Distribution, ProductSeries etc
completeness_clause = """
BugTask.bug = Bug.id AND """ + BugTask.completeness_clause
def canBeAQuestion(self):
"""See `IBug`."""
return (self._getQuestionTargetableBugTask() is not None
and self.getQuestionCreatedFromBug() is None)
def _getQuestionTargetableBugTask(self):
"""Return the only bugtask that can be a QuestionTarget, or None.
Bugs that are also in external bug trackers cannot be converted
to questions. This is also true for bugs that are being developed.
None is returned when either of these conditions are true.
The bugtask is selected by these rules:
1. It's status is not Invalid.
2. It is not a conjoined slave.
Only one bugtask must meet both conditions to be return. When
zero or many bugtasks match, None is returned.
"""
# XXX sinzui 2007-10-19:
# We may want to removed the bugtask.conjoined_master check
# below. It is used to simplify the task of converting
# conjoined bugtasks to question--since slaves cannot be
# directly updated anyway.
non_invalid_bugtasks = [
bugtask for bugtask in self.bugtasks
if (bugtask.status != BugTaskStatus.INVALID
and bugtask.conjoined_master is None)]
if len(non_invalid_bugtasks) != 1:
return None
[valid_bugtask] = non_invalid_bugtasks
if valid_bugtask.pillar.official_malone:
return valid_bugtask
else:
return None
def convertToQuestion(self, person, comment=None):
"""See `IBug`."""
question = self.getQuestionCreatedFromBug()
assert question is None, (
'This bug was already converted to question #%s.' % question.id)
bugtask = self._getQuestionTargetableBugTask()
assert bugtask is not None, (
'A question cannot be created from this bug without a '
'valid bugtask.')
bugtask_before_modification = Snapshot(
bugtask, providing=providedBy(bugtask))
bugtask.transitionToStatus(BugTaskStatus.INVALID, person)
edited_fields = ['status']
if comment is not None:
self.newMessage(
owner=person, subject=self.followup_subject(),
content=comment)
notify(
ObjectModifiedEvent(
object=bugtask,
object_before_modification=bugtask_before_modification,
edited_fields=edited_fields,
user=person))
question_target = IQuestionTarget(bugtask.target)
question = question_target.createQuestionFromBug(self)
self.addChange(BugConvertedToQuestion(UTC_NOW, person, question))
notify(BugBecameQuestionEvent(self, question, person))
return question
def getQuestionCreatedFromBug(self):
"""See `IBug`."""
for question in self.questions:
if (question.owner == self.owner
and question.datecreated == self.datecreated):
return question
return None
def canMentor(self, user):
"""See `ICanBeMentored`."""
if user is None:
return False
if self.duplicateof is not None or self.is_complete:
return False
if bool(self.isMentor(user)):
return False
if not user.teams_participated_in:
return False
return True
def isMentor(self, user):
"""See `ICanBeMentored`."""
return MentoringOffer.selectOneBy(bug=self, owner=user) is not None
def offerMentoring(self, user, team):
"""See `ICanBeMentored`."""
# if an offer exists, then update the team
mentoringoffer = MentoringOffer.selectOneBy(bug=self, owner=user)
if mentoringoffer is not None:
mentoringoffer.team = team
return mentoringoffer
# if no offer exists, create one from scratch
mentoringoffer = MentoringOffer(owner=user, team=team,
bug=self)
notify(ObjectCreatedEvent(mentoringoffer, user=user))
return mentoringoffer
def retractMentoring(self, user):
"""See `ICanBeMentored`."""
mentoringoffer = MentoringOffer.selectOneBy(bug=self, owner=user)
if mentoringoffer is not None:
notify(ObjectDeletedEvent(mentoringoffer, user=user))
MentoringOffer.delete(mentoringoffer.id)
def getMessageChunks(self):
"""See `IBug`."""
query = """
Message.id = MessageChunk.message AND
BugMessage.message = Message.id AND
BugMessage.bug = %s
""" % sqlvalues(self)
chunks = MessageChunk.select(query,
clauseTables=["BugMessage", "Message"],
# XXX: kiko 2006-09-16 bug=60745:
# There is an issue that presents itself
# here if we prejoin message.owner: because Message is
# already in the clauseTables, the SQL generated joins
# against message twice and that causes the results to
# break.
prejoinClauseTables=["Message"],
# Note the ordering by Message.id here; while datecreated in
# production is never the same, it can be in the test suite.
orderBy=["Message.datecreated", "Message.id",
"MessageChunk.sequence"])
chunks = list(chunks)
# Since we can't prejoin, cache all people at once so we don't
# have to do it while rendering, which is a big deal for bugs
# with a million comments.
owner_ids = set()
for chunk in chunks:
if chunk.message.ownerID:
owner_ids.add(str(chunk.message.ownerID))
list(Person.select("ID in (%s)" % ",".join(owner_ids)))
return chunks
def getNullBugTask(self, product=None, productseries=None,
sourcepackagename=None, distribution=None,
distroseries=None):
"""See `IBug`."""
return NullBugTask(bug=self, product=product,
productseries=productseries,
sourcepackagename=sourcepackagename,
distribution=distribution,
distroseries=distroseries)
def addNomination(self, owner, target):
"""See `IBug`."""
if not self.canBeNominatedFor(target):
raise NominationError(
"This bug cannot be nominated for %s." %
target.bugtargetdisplayname)
distroseries = None
productseries = None
if IDistroSeries.providedBy(target):
distroseries = target
if target.status == SeriesStatus.OBSOLETE:
raise NominationSeriesObsoleteError(
"%s is an obsolete series." % target.bugtargetdisplayname)
else:
assert IProductSeries.providedBy(target)
productseries = target
nomination = BugNomination(
owner=owner, bug=self, distroseries=distroseries,
productseries=productseries)
self.addChange(SeriesNominated(UTC_NOW, owner, target))
return nomination
def canBeNominatedFor(self, target):
"""See `IBug`."""
try:
self.getNominationFor(target)
except NotFoundError:
# No nomination exists. Let's see if the bug is already
# directly targeted to this nomination target.
if IDistroSeries.providedBy(target):
series_getter = operator.attrgetter("distroseries")
pillar_getter = operator.attrgetter("distribution")
elif IProductSeries.providedBy(target):
series_getter = operator.attrgetter("productseries")
pillar_getter = operator.attrgetter("product")
else:
return False
for task in self.bugtasks:
if series_getter(task) == target:
# The bug is already targeted at this
# nomination target.
return False
# No nomination or tasks are targeted at this
# nomination target. But we also don't want to nominate for a
# series of a product or distro for which we don't have a
# plain pillar task.
for task in self.bugtasks:
if pillar_getter(task) == pillar_getter(target):
return True
# No tasks match the candidate's pillar. We must refuse.
return False
else:
# The bug is already nominated for this nomination target.
return False
def getNominationFor(self, target):
"""See `IBug`."""
if IDistroSeries.providedBy(target):
filter_args = dict(distroseriesID=target.id)
elif IProductSeries.providedBy(target):
filter_args = dict(productseriesID=target.id)
else:
return None
nomination = BugNomination.selectOneBy(bugID=self.id, **filter_args)
if nomination is None:
raise NotFoundError(
"Bug #%d is not nominated for %s." % (
self.id, target.displayname))
return nomination
def getNominations(self, target=None, nominations=None):
"""See `IBug`."""
# Define the function used as a sort key.
def by_bugtargetdisplayname(nomination):
"""Return the friendly sort key verson of displayname."""
return nomination.target.bugtargetdisplayname.lower()
if nominations is None:
nominations = BugNomination.selectBy(bugID=self.id)
if IProduct.providedBy(target):
filtered_nominations = []
for nomination in shortlist(nominations):
if (nomination.productseries and
nomination.productseries.product == target):
filtered_nominations.append(nomination)
nominations = filtered_nominations
elif IDistribution.providedBy(target):
filtered_nominations = []
for nomination in shortlist(nominations):
if (nomination.distroseries and
nomination.distroseries.distribution == target):
filtered_nominations.append(nomination)
nominations = filtered_nominations
return sorted(nominations, key=by_bugtargetdisplayname)
def getBugWatch(self, bugtracker, remote_bug):
"""See `IBug`."""
# If the bug tracker is of BugTrackerType.EMAILADDRESS we can
# never tell if a bug is already being watched upstream, since
# the remotebug field for such bug watches contains either '' or
# an RFC822 message ID. In these cases, then, we always return
# None for the sake of sanity.
if bugtracker.bugtrackertype == BugTrackerType.EMAILADDRESS:
return None
# XXX: BjornT 2006-10-11:
# This matching is a bit fragile, since bugwatch.remotebug
# is a user editable text string. We should improve the
# matching so that for example '#42' matches '42' and so on.
return BugWatch.selectFirstBy(
bug=self, bugtracker=bugtracker, remotebug=str(remote_bug),
orderBy='id')
def setStatus(self, target, status, user):
"""See `IBug`."""
bugtask = self.getBugTask(target)
if bugtask is None:
if IProductSeries.providedBy(target):
bugtask = self.getBugTask(target.product)
elif ISourcePackage.providedBy(target):
current_distro_series = target.distribution.currentseries
current_package = current_distro_series.getSourcePackage(
target.sourcepackagename.name)
if self.getBugTask(current_package) is not None:
# The bug is targeted to the current series, don't
# fall back on the general distribution task.
return None
distro_package = target.distribution.getSourcePackage(
target.sourcepackagename.name)
bugtask = self.getBugTask(distro_package)
else:
return None
if bugtask is None:
return None
if bugtask.conjoined_master is not None:
bugtask = bugtask.conjoined_master
if bugtask.status == status:
return None
bugtask_before_modification = Snapshot(
bugtask, providing=providedBy(bugtask))
bugtask.transitionToStatus(status, user)
notify(ObjectModifiedEvent(
bugtask, bugtask_before_modification, ['status'], user=user))
return bugtask
def setPrivate(self, private, who):
"""See `IBug`.
We also record who made the change and when the change took
place.
"""
if self.private != private:
if private:
# Change indirect subscribers into direct subscribers
# *before* setting private because
# getIndirectSubscribers() behaves differently when
# the bug is private.
for person in self.getIndirectSubscribers():
self.subscribe(person, who)
self.private = private
if private:
self.who_made_private = who
self.date_made_private = UTC_NOW
else:
self.who_made_private = None
self.date_made_private = None
# Correct the heat for the bug immediately, so that we don't have
# to wait for the next calculation job for the adjusted heat.
self.updateHeat()
return True # Changed.
else:
return False # Not changed.
def setSecurityRelated(self, security_related):
"""Setter for the `security_related` property."""
if self.security_related != security_related:
self.security_related = security_related
# Correct the heat for the bug immediately, so that we don't have
# to wait for the next calculation job for the adjusted heat.
self.updateHeat()
return True # Changed
else:
return False # Unchanged
def getBugTask(self, target):
"""See `IBug`."""
for bugtask in self.bugtasks:
if bugtask.target == target:
return bugtask
return None
def _getTags(self):
"""Get the tags as a sorted list of strings."""
tags = [
bugtag.tag
for bugtag in BugTag.selectBy(bug=self, orderBy='tag')]
return tags
def _setTags(self, tags):
"""Set the tags from a list of strings."""
# In order to preserve the ordering of the tags, delete all tags
# and insert the new ones.
new_tags = set([tag.lower() for tag in tags])
old_tags = set(self.tags)
added_tags = new_tags.difference(old_tags)
removed_tags = old_tags.difference(new_tags)
for removed_tag in removed_tags:
tag = BugTag.selectOneBy(bug=self, tag=removed_tag)
tag.destroySelf()
for added_tag in added_tags:
BugTag(bug=self, tag=added_tag)
Store.of(self).flush()
tags = property(_getTags, _setTags)
@staticmethod
def getBugTasksByPackageName(bugtasks):
"""See IBugTask."""
bugtasks_by_package = {}
for bugtask in bugtasks:
bugtasks_by_package.setdefault(bugtask.sourcepackagename, [])
bugtasks_by_package[bugtask.sourcepackagename].append(bugtask)
return bugtasks_by_package
def _getAffectedUser(self, user):
"""Return the `IBugAffectsPerson` for a user, or None
:param user: An `IPerson` that may be affected by the bug.
:return: An `IBugAffectsPerson` or None.
"""
if user is None:
return None
else:
return Store.of(self).get(
BugAffectsPerson, (self.id, user.id))
def isUserAffected(self, user):
"""See `IBug`."""
bap = self._getAffectedUser(user)
if bap is not None:
return bap.affected
else:
return None
def _flushAndInvalidate(self):
"""Flush all changes to the store and re-read `self` from the DB."""
store = Store.of(self)
store.flush()
store.invalidate(self)
def markUserAffected(self, user, affected=True):
"""See `IBug`."""
bap = self._getAffectedUser(user)
if bap is None:
BugAffectsPerson(bug=self, person=user, affected=affected)
self._flushAndInvalidate()
else:
if bap.affected != affected:
bap.affected = affected
self._flushAndInvalidate()
# Loop over dupes.
for dupe in self.duplicates:
if dupe._getAffectedUser(user) is not None:
dupe.markUserAffected(user, affected)
self.updateHeat()
@property
def readonly_duplicateof(self):
"""See `IBug`."""
return self.duplicateof
def markAsDuplicate(self, duplicate_of):
"""See `IBug`."""
field = DuplicateBug()
field.context = self
current_duplicateof = self.duplicateof
try:
if duplicate_of is not None:
field._validate(duplicate_of)
self.duplicateof = duplicate_of
except LaunchpadValidationError, validation_error:
raise InvalidDuplicateValue(validation_error)
if duplicate_of is not None:
# Update the heat of the master bug and set this bug's heat
# to 0 (since it's a duplicate, it shouldn't have any heat
# at all).
self.setHeat(0)
duplicate_of.updateHeat()
else:
# Otherwise, recalculate this bug's heat, since it will be 0
# from having been a duplicate. We also update the bug that
# was previously duplicated.
self.updateHeat()
current_duplicateof.updateHeat()
def setCommentVisibility(self, user, comment_number, visible):
"""See `IBug`."""
bug_message_set = getUtility(IBugMessageSet)
bug_message = bug_message_set.getByBugAndMessage(
self, self.messages[comment_number])
bug_message.visible = visible
def userCanView(self, user):
"""See `IBug`."""
admins = getUtility(ILaunchpadCelebrities).admin
if not self.private:
# This is a public bug.
return True
elif user.inTeam(admins):
# Admins can view all bugs.
return True
else:
# This is a private bug. Only explicit subscribers may view it.
for subscription in self.subscriptions:
if user.inTeam(subscription.person):
return True
return False
def linkHWSubmission(self, submission):
"""See `IBug`."""
getUtility(IHWSubmissionBugSet).create(submission, self)
def unlinkHWSubmission(self, submission):
"""See `IBug`."""
getUtility(IHWSubmissionBugSet).remove(submission, self)
def getHWSubmissions(self, user=None):
"""See `IBug`."""
return getUtility(IHWSubmissionBugSet).submissionsForBug(self, user)
def personIsDirectSubscriber(self, person):
"""See `IBug`."""
store = Store.of(self)
subscriptions = store.find(
BugSubscription,
BugSubscription.bug == self,
BugSubscription.person == person)
return not subscriptions.is_empty()
def personIsAlsoNotifiedSubscriber(self, person):
"""See `IBug`."""
# We have to use getAlsoNotifiedSubscribers() here and iterate
# over what it returns because "also notified subscribers" is
# actually a composite of bug contacts, structural subscribers
# and assignees. As such, it's not possible to get them all with
# one query.
also_notified_subscribers = self.getAlsoNotifiedSubscribers()
return person in also_notified_subscribers
def personIsSubscribedToDuplicate(self, person):
"""See `IBug`."""
store = Store.of(self)
subscriptions_from_dupes = store.find(
BugSubscription,
Bug.duplicateof == self,
BugSubscription.bugID == Bug.id,
BugSubscription.person == person)
return not subscriptions_from_dupes.is_empty()
def setHeat(self, heat, timestamp=None):
"""See `IBug`."""
if timestamp is None:
timestamp = UTC_NOW
if heat < 0:
heat = 0
self.heat = heat
self.heat_last_updated = timestamp
for task in self.bugtasks:
task.target.recalculateBugHeatCache()
def updateHeat(self):
"""See `IBug`."""
if self.duplicateof is not None:
# If this bug is a duplicate we don't try to calculate its
# heat.
return
# We need to flush the store first to ensure that changes are
# reflected in the new bug heat total.
store = Store.of(self)
store.flush()
self.heat = SQL("calculate_bug_heat(%s)" % sqlvalues(self))
self.heat_last_updated = UTC_NOW
@property
def attachments(self):
"""See `IBug`."""
# We omit those bug attachments that do not have a
# LibraryFileContent record in order to avoid OOPSes as
# mentioned in bug 542274. These bug attachments will be
# deleted anyway during the next garbo_daily run.
store = Store.of(self)
return store.find(
BugAttachment, BugAttachment.bug == self,
BugAttachment.libraryfile == LibraryFileAlias.id,
LibraryFileAlias.content != None).order_by(BugAttachment.id)
class BugSet:
"""See BugSet."""
implements(IBugSet)
valid_bug_name_re = re.compile(r'''^[a-z][a-z0-9\\+\\.\\-]+$''')
def get(self, bugid):
"""See `IBugSet`."""
try:
return Bug.get(bugid)
except SQLObjectNotFound:
raise NotFoundError(
"Unable to locate bug with ID %s." % str(bugid))
def getByNameOrID(self, bugid):
"""See `IBugSet`."""
if self.valid_bug_name_re.match(bugid):
bug = Bug.selectOneBy(name=bugid)
if bug is None:
raise NotFoundError(
"Unable to locate bug with ID %s." % bugid)
else:
try:
bug = self.get(bugid)
except ValueError:
raise NotFoundError(
"Unable to locate bug with nickname %s." % bugid)
return bug
def searchAsUser(self, user, duplicateof=None, orderBy=None, limit=None):
"""See `IBugSet`."""
where_clauses = []
if duplicateof:
where_clauses.append("Bug.duplicateof = %d" % duplicateof.id)
admins = getUtility(ILaunchpadCelebrities).admin
if user:
if not user.inTeam(admins):
# Enforce privacy-awareness for logged-in, non-admin users,
# so that they can only see the private bugs that they're
# allowed to see.
where_clauses.append("""
(Bug.private = FALSE OR
Bug.id in (
SELECT Bug.id
FROM Bug, BugSubscription, TeamParticipation
WHERE Bug.id = BugSubscription.bug AND
TeamParticipation.person = %(personid)s AND
BugSubscription.person = TeamParticipation.team))
""" % sqlvalues(personid=user.id))
else:
# Anonymous user; filter to include only public bugs in
# the search results.
where_clauses.append("Bug.private = FALSE")
other_params = {}
if orderBy:
other_params['orderBy'] = orderBy
if limit:
other_params['limit'] = limit
return Bug.select(
' AND '.join(where_clauses), **other_params)
def queryByRemoteBug(self, bugtracker, remotebug):
"""See `IBugSet`."""
bug = Bug.selectFirst("""
bugwatch.bugtracker = %s AND
bugwatch.remotebug = %s AND
bugwatch.bug = bug.id
""" % sqlvalues(bugtracker.id, str(remotebug)),
distinct=True,
clauseTables=['BugWatch'],
orderBy=['datecreated'])
return bug
def createBug(self, bug_params):
"""See `IBugSet`."""
# Make a copy of the parameter object, because we might modify some
# of its attribute values below.
params = snapshot_bug_params(bug_params)
if params.product and params.product.private_bugs:
# If the private_bugs flag is set on a product, then
# force the new bug report to be private.
params.private = True
bug, event = self.createBugWithoutTarget(params)
if params.security_related:
assert params.private, (
"A security related bug should always be private by default.")
if params.product:
context = params.product
else:
context = params.distribution
if context.security_contact:
bug.subscribe(context.security_contact, params.owner)
else:
bug.subscribe(context.owner, params.owner)
# XXX: ElliotMurphy 2007-06-14: If we ever allow filing private
# non-security bugs, this test might be simplified to checking
# params.private.
elif params.product and params.product.private_bugs:
# Subscribe the bug supervisor to all bugs,
# because all their bugs are private by default
# otherwise only subscribe the bug reporter by default.
if params.product.bug_supervisor:
bug.subscribe(params.product.bug_supervisor, params.owner)
else:
bug.subscribe(params.product.owner, params.owner)
else:
# nothing to do
pass
# Create the task on a product if one was passed.
if params.product:
BugTaskSet().createTask(
bug=bug, product=params.product, owner=params.owner,
status=params.status)
# Create the task on a source package name if one was passed.
if params.distribution:
BugTaskSet().createTask(
bug=bug, distribution=params.distribution,
sourcepackagename=params.sourcepackagename,
owner=params.owner, status=params.status)
# Tell everyone.
notify(event)
# Calculate the bug's initial heat.
bug.updateHeat()
return bug
def createBugWithoutTarget(self, bug_params):
"""See `IBugSet`."""
# Make a copy of the parameter object, because we might modify some
# of its attribute values below.
params = snapshot_bug_params(bug_params)
if not (params.comment or params.description or params.msg):
raise AssertionError(
'Either comment, msg, or description should be specified.')
if not params.datecreated:
params.datecreated = UTC_NOW
# make sure we did not get TOO MUCH information
assert params.comment is None or params.msg is None, (
"Expected either a comment or a msg, but got both.")
# Store binary package name in the description, because
# storing it as a separate field was a maintenance burden to
# developers.
if params.binarypackagename:
params.comment = "Binary package hint: %s\n\n%s" % (
params.binarypackagename.name, params.comment)
# Create the bug comment if one was given.
if params.comment:
rfc822msgid = make_msgid('malonedeb')
params.msg = Message(
subject=params.title, rfc822msgid=rfc822msgid,
owner=params.owner, datecreated=params.datecreated)
MessageChunk(
message=params.msg, sequence=1, content=params.comment,
blob=None)
# Extract the details needed to create the bug and optional msg.
if not params.description:
params.description = params.msg.text_contents
extra_params = {}
if params.private:
# We add some auditing information. After bug creation
# time these attributes are updated by Bug.setPrivate().
extra_params.update(
date_made_private=params.datecreated,
who_made_private=params.owner)
bug = Bug(
title=params.title, description=params.description,
private=params.private, owner=params.owner,
datecreated=params.datecreated,
security_related=params.security_related,
**extra_params)
if params.subscribe_owner:
bug.subscribe(params.owner, params.owner)
if params.tags:
bug.tags = params.tags
# Subscribe other users.
for subscriber in params.subscribers:
bug.subscribe(subscriber, params.owner)
# Link the bug to the message.
BugMessage(bug=bug, message=params.msg)
# Mark the bug reporter as affected by that bug.
bug.markUserAffected(bug.owner)
# Populate the creation event.
if params.filed_by is None:
event = ObjectCreatedEvent(bug, user=params.owner)
else:
event = ObjectCreatedEvent(bug, user=params.filed_by)
return (bug, event)
def getDistinctBugsForBugTasks(self, bug_tasks, user, limit=10):
"""See `IBugSet`."""
# XXX: Graham Binns 2009-05-28 bug=75764
# We slice bug_tasks here to prevent this method from
# causing timeouts, since if we try to iterate over it
# Transaction.iterSelect() will try to listify the results.
# This can be fixed by selecting from Bugs directly, but
# that's non-trivial.
# We select more than :limit: since if a bug affects more than
# one source package, it will be returned more than one time. 4
# is an arbitrary number that should be large enough.
bugs = []
for bug_task in bug_tasks[:4*limit]:
bug = bug_task.bug
duplicateof = bug.duplicateof
if duplicateof is not None:
bug = duplicateof
if not bug.userCanView(user):
continue
if bug not in bugs:
bugs.append(bug)
if len(bugs) >= limit:
break
return bugs
def getByNumbers(self, bug_numbers):
"""See `IBugSet`."""
if bug_numbers is None or len(bug_numbers) < 1:
return EmptyResultSet()
store = IStore(Bug)
result_set = store.find(Bug, In(Bug.id, bug_numbers))
return result_set.order_by('id')
def dangerousGetAllBugs(self):
"""See `IBugSet`."""
store = IStore(Bug)
result_set = store.find(Bug)
return result_set.order_by('id')
def getBugsWithOutdatedHeat(self, max_heat_age):
"""See `IBugSet`."""
store = IStore(Bug)
last_updated_cutoff = (
datetime.now(timezone('UTC')) -
timedelta(days=max_heat_age))
last_updated_clause = Or(
Bug.heat_last_updated < last_updated_cutoff,
Bug.heat_last_updated == None)
return store.find(
Bug, Bug.duplicateof==None, last_updated_clause).order_by('id')
class BugAffectsPerson(SQLBase):
"""A bug is marked as affecting a user."""
bug = ForeignKey(dbName='bug', foreignKey='Bug', notNull=True)
person = ForeignKey(dbName='person', foreignKey='Person', notNull=True)
affected = BoolCol(notNull=True, default=True)
__storm_primary__ = "bugID", "personID"
class FileBugData:
"""Extra data to be added to the bug."""
implements(IFileBugData)
def __init__(self, initial_summary=None, initial_tags=None,
private=None, subscribers=None, extra_description=None,
comments=None, attachments=None,
hwdb_submission_keys=None):
if initial_tags is None:
initial_tags = []
if subscribers is None:
subscribers = []
if comments is None:
comments = []
if attachments is None:
attachments = []
if hwdb_submission_keys is None:
hwdb_submission_keys = []
self.initial_summary = initial_summary
self.private = private
self.extra_description = extra_description
self.initial_tags = initial_tags
self.subscribers = subscribers
self.comments = comments
self.attachments = attachments
self.hwdb_submission_keys = hwdb_submission_keys
def asDict(self):
"""Return the FileBugData instance as a dict."""
return self.__dict__.copy()
|