def add_bug_change_notifications(bug_delta, old_bugtask=None, new_subscribers=None): """Generate bug notifications and add them to the bug.""" changes = get_bug_changes(bug_delta) recipients = bug_delta.bug.getBugNotificationRecipients( level=BugNotificationLevel.METADATA) if old_bugtask is not None: old_bugtask_recipients = BugNotificationRecipients() get_also_notified_subscribers(old_bugtask, recipients=old_bugtask_recipients, level=BugNotificationLevel.METADATA) recipients.update(old_bugtask_recipients) for change in changes: bug = bug_delta.bug if isinstance(change, BugDuplicateChange): no_dupe_master_recipients = bug.getBugNotificationRecipients( level=change.change_level) bug_delta.bug.addChange(change, recipients=no_dupe_master_recipients) elif (isinstance(change, BugTaskAssigneeChange) and new_subscribers is not None): for person in new_subscribers: # If this change involves multiple changes, other structural # subscribers will leak into new_subscribers, and they may # not be in the recipients list, due to having a LIFECYCLE # structural subscription. if person not in recipients: continue # We are only interested in dropping the assignee out, since # we send assignment notifications separately. reason, rationale = recipients.getReason(person) if 'Assignee' in rationale: recipients.remove(person) bug_delta.bug.addChange(change, recipients=recipients) else: if change.change_level == BugNotificationLevel.LIFECYCLE: # XXX: The level handling here is duplicated with the # start of this method and *really* needs refactoring. change_recipients = BugNotificationRecipients() change_recipients.update(recipients) change_recipients.update( bug.getBugNotificationRecipients( level=change.change_level)) if old_bugtask is not None: old_bugtask_recipients = BugNotificationRecipients() get_also_notified_subscribers( old_bugtask, recipients=old_bugtask_recipients, level=change.change_level) change_recipients.update(old_bugtask_recipients) else: change_recipients = recipients bug_delta.bug.addChange(change, recipients=change_recipients)
def _make_deferred_notification(self): bug = self.factory.makeBug() empty_recipients = BugNotificationRecipients() message = getUtility(IMessageSet).fromText( 'subject', 'a comment.', bug.owner, datecreated=self.ten_minutes_ago) self.notification_set.addNotification( bug, False, message, empty_recipients, None, deferred=True)
def test_getStructuralSubscribers_recipients(self): # If provided, get_structural_subscribers() calls the appropriate # methods on a BugNotificationRecipients object. subscriber = self.factory.makePerson() login_person(subscriber) product, bug = self.make_product_with_bug() product.addBugSubscription(subscriber, subscriber) recipients = BugNotificationRecipients() subscribers = get_structural_subscribers(bug, recipients, None, None) # The return value is a list only when populating recipients. self.assertIsInstance(subscribers, list) self.assertEqual([subscriber], recipients.getRecipients()) reason, header = recipients.getReason(subscriber) self.assertThat( reason, StartsWith( u"You received this bug notification because " u"you are subscribed to ")) self.assertThat(header, StartsWith(u"Subscriber "))
def test_getStructuralSubscribers_recipients(self): # If provided, get_structural_subscribers() calls the appropriate # methods on a BugNotificationRecipients object. subscriber = self.factory.makePerson() login_person(subscriber) product, bug = self.make_product_with_bug() product.addBugSubscription(subscriber, subscriber) recipients = BugNotificationRecipients() subscribers = get_structural_subscribers(bug, recipients, None, None) # The return value is a list only when populating recipients. self.assertIsInstance(subscribers, list) self.assertEqual([subscriber], recipients.getRecipients()) reason, header = recipients.getReason(subscriber) self.assertThat( reason, StartsWith(u"You received this bug notification because " u"you are subscribed to ")) self.assertThat(header, StartsWith(u"Subscriber "))
def _make_deferred_notification(self): bug = self.factory.makeBug() empty_recipients = BugNotificationRecipients() message = getUtility(IMessageSet).fromText('subject', 'a comment.', bug.owner, datecreated=datetime.now( pytz.UTC)) self.bns.addNotification(bug, False, message, empty_recipients, None, deferred=True)
def getBugNotificationRecipients(self, level=None): recipients = BugNotificationRecipients() no_priv = getUtility(IPersonSet).getByEmail( '*****@*****.**') recipients.addDirectSubscriber(no_priv) return recipients