Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/sentry/notifications/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ def _get_notification_setting_default(
"""
In order to increase engagement, we automatically opt users into receiving
Slack notifications if they install Slack and link their identity.
Approval notifications always default to Slack being on.
"""
if should_use_slack_automatic:
if should_use_slack_automatic or type == NotificationSettingTypes.APPROVAL:
return NOTIFICATION_SETTINGS_ALL_SOMETIMES[type]
return NOTIFICATION_SETTING_DEFAULTS[provider][type]

Expand Down Expand Up @@ -86,6 +87,7 @@ def where_should_recipient_be_notified(
],
recipient: Team | User,
should_use_slack_automatic: bool = False,
type: NotificationSettingTypes = NotificationSettingTypes.ISSUE_ALERTS,
) -> list[ExternalProviders]:
"""
Given a mapping of default and specific notification settings by user,
Expand All @@ -94,7 +96,7 @@ def where_should_recipient_be_notified(
mapping = _get_setting_mapping_from_mapping(
notification_settings_by_recipient,
recipient,
NotificationSettingTypes.ISSUE_ALERTS,
type,
should_use_slack_automatic=should_use_slack_automatic,
)
return [
Expand Down
14 changes: 8 additions & 6 deletions src/sentry/notifications/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,27 +289,29 @@ def get_for_recipient_by_parent(

def filter_to_accepting_recipients(
self,
project: Project,
parent: Project,
recipients: Iterable[Team | User],
type: NotificationSettingTypes = NotificationSettingTypes.ISSUE_ALERTS,
) -> Mapping[ExternalProviders, Iterable[Team | User]]:
"""
Filters a list of teams or users down to the recipients by provider who
are subscribed to alerts. We check both the project level settings and
global default settings.
"""
notification_settings = self.get_for_recipient_by_parent(
NotificationSettingTypes.ISSUE_ALERTS, parent=project, recipients=recipients
)
from sentry.models import Organization

notification_settings = self.get_for_recipient_by_parent(type, parent, recipients)
notification_settings_by_recipient = transform_to_notification_settings_by_recipient(
notification_settings, recipients
)
mapping = defaultdict(set)
organization = parent if isinstance(parent, Organization) else parent.organization
should_use_slack_automatic = features.has(
"organizations:notification-slack-automatic", project.organization
"organizations:notification-slack-automatic", organization
)
for recipient in recipients:
providers = where_should_recipient_be_notified(
notification_settings_by_recipient, recipient, should_use_slack_automatic
notification_settings_by_recipient, recipient, should_use_slack_automatic, type
)
for provider in providers:
mapping[provider].add(recipient)
Expand Down
14 changes: 10 additions & 4 deletions src/sentry/notifications/notifications/organization_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

from sentry import analytics, features, roles
from sentry.integrations.slack.utils.notifications import get_settings_url
from sentry.models import OrganizationMember, Team
from sentry.models import NotificationSetting, OrganizationMember, Team
from sentry.notifications.notifications.base import BaseNotification, MessageAction
from sentry.notifications.notify import notification_providers
from sentry.notifications.types import NotificationSettingTypes
from sentry.types.integrations import ExternalProviders, get_provider_name

if TYPE_CHECKING:
Expand Down Expand Up @@ -70,11 +71,16 @@ def get_participants(self) -> Mapping[ExternalProviders, Iterable[Team | User]]:
if features.has("organizations:slack-requests", self.organization):
available_providers = notification_providers()

# TODO: need to read off notification settings
recipients = list(self.determine_recipients())
output = {provider: recipients for provider in available_providers}
recipients_by_provider = NotificationSetting.objects.filter_to_accepting_recipients(
self.organization, recipients, NotificationSettingTypes.APPROVAL
)

return output
return {
provider: recipients_of_provider
for provider, recipients_of_provider in recipients_by_provider.items()
if provider in available_providers
}

def send(self) -> None:
from sentry.notifications.notify import notify
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from sentry.models import NotificationSetting, OrganizationMember
from sentry.notifications.notifications.organization_request import OrganizationRequestNotification
from sentry.notifications.types import NotificationSettingOptionValues, NotificationSettingTypes
from sentry.testutils import TestCase
from sentry.testutils.helpers import with_feature
from sentry.types.integrations import ExternalProviders


class DummyRequestNotification(OrganizationRequestNotification):
def __init__(self, organization, requester, member_ids):
super().__init__(organization, requester)
self.member_ids = member_ids

def determine_member_recipients(self):
return OrganizationMember.objects.filter(id__in=self.member_ids)


class GetParticipantsTest(TestCase):
def setUp(self):
self.user1 = self.create_user()
self.user2 = self.create_user()
self.member1 = self.create_member(user=self.user1, organization=self.organization)
self.member2 = self.create_member(user=self.user2, organization=self.organization)

@with_feature("organizations:slack-requests")
def test_default_to_slack(self):
member_ids = [self.member1.id, self.member2.id]
notification = DummyRequestNotification(self.organization, self.user, member_ids)

assert notification.get_participants() == {
ExternalProviders.EMAIL: {self.user1, self.user2},
ExternalProviders.SLACK: {self.user1, self.user2},
}

@with_feature("organizations:slack-requests")
def test_turn_off_settings(self):

NotificationSetting.objects.update_settings(
ExternalProviders.SLACK,
NotificationSettingTypes.APPROVAL,
NotificationSettingOptionValues.ALWAYS,
user=self.user1,
)

NotificationSetting.objects.update_settings(
ExternalProviders.EMAIL,
NotificationSettingTypes.APPROVAL,
NotificationSettingOptionValues.ALWAYS,
user=self.user2,
)

member_ids = [self.member1.id, self.member2.id]
notification = DummyRequestNotification(self.organization, self.user, member_ids)

assert notification.get_participants() == {
ExternalProviders.EMAIL: {self.user2},
ExternalProviders.SLACK: {self.user1},
}