Skip to content

Commit

Permalink
fix(notifications): Get default settings in `where_should_be_particip…
Browse files Browse the repository at this point in the history
…ating` (#29757)
  • Loading branch information
mgaeta authored Nov 4, 2021
1 parent 61e3a58 commit c380c0b
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 63 deletions.
28 changes: 16 additions & 12 deletions src/sentry/notifications/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,31 @@ def _get_setting_mapping_from_mapping(
type: NotificationSettingTypes,
should_use_slack_automatic: bool = False,
) -> Mapping[ExternalProviders, NotificationSettingOptionValues]:
# XXX(CEO): may not respect granularity of a setting for Slack a setting for email
# but we'll worry about that later since we don't have a FE for it yet
"""
XXX(CEO): may not respect granularity of a setting for Slack a setting for
email but we'll worry about that later since we don't have a FE for it yet.
"""
from sentry.notifications.notify import notification_providers

specific_scope = get_scope_type(type)
# Fill in with the fallback values.
notification_setting_option = {
provider: _get_notification_setting_default(
provider, type, should_use_slack_automatic=should_use_slack_automatic
)
for provider in notification_providers()
}

notification_settings_mapping = notification_settings_by_recipient.get(recipient)
if notification_settings_mapping:
notification_setting_option = (
specific_scope = get_scope_type(type)
notification_setting_option.update(
notification_settings_mapping.get(specific_scope)
or notification_settings_mapping.get(NotificationScopeType.USER)
or notification_settings_mapping.get(NotificationScopeType.TEAM)
or {}
)
if notification_setting_option:
return notification_setting_option

return {
provider: _get_notification_setting_default(
provider, type, should_use_slack_automatic=should_use_slack_automatic
)
for provider in notification_providers()
}
return notification_setting_option


def where_should_recipient_be_notified(
Expand Down
5 changes: 3 additions & 2 deletions src/sentry/testutils/helpers/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ def link_team(team: Team, integration: Integration, channel_name: str, channel_i


def send_notification(*args):
args_list = list(args)[1:]
send_notification_as_slack(*args_list, {})
provider, *args_list = args
if provider == ExternalProviders.SLACK:
send_notification_as_slack(*args_list, {})


def get_attachment():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def test_default_to_slack(self):

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

NotificationSetting.objects.update_settings(
ExternalProviders.SLACK,
NotificationSettingTypes.APPROVAL,
Expand All @@ -53,6 +52,6 @@ def test_turn_off_settings(self):
notification = DummyRequestNotification(self.organization, self.user, member_ids)

assert notification.get_participants() == {
ExternalProviders.EMAIL: {self.user2},
ExternalProviders.SLACK: {self.user1},
ExternalProviders.EMAIL: {self.user1, self.user2},
ExternalProviders.SLACK: {self.user1, self.user2},
}
46 changes: 0 additions & 46 deletions tests/sentry/notifications/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from sentry.models import NotificationSetting
from sentry.notifications.helpers import (
_get_setting_mapping_from_mapping,
collect_groups_by_project,
get_scope_type,
get_settings_by_provider,
Expand Down Expand Up @@ -42,51 +41,6 @@ def setUp(self):
user=self.user,
)

def test_get_setting_mapping_from_mapping_issue_alerts(self):
notification_settings = {
self.user: {
NotificationScopeType.USER: {
ExternalProviders.EMAIL: NotificationSettingOptionValues.ALWAYS
}
}
}
mapping = _get_setting_mapping_from_mapping(
notification_settings,
self.user,
NotificationSettingTypes.ISSUE_ALERTS,
)
assert mapping == {ExternalProviders.EMAIL: NotificationSettingOptionValues.ALWAYS}

def test_get_setting_mapping_from_mapping_deploy(self):
notification_settings = {
self.user: {
NotificationScopeType.USER: {
ExternalProviders.EMAIL: NotificationSettingOptionValues.COMMITTED_ONLY
}
}
}
mapping = _get_setting_mapping_from_mapping(
notification_settings,
self.user,
NotificationSettingTypes.DEPLOY,
)
assert mapping == {ExternalProviders.EMAIL: NotificationSettingOptionValues.COMMITTED_ONLY}

def test_get_setting_mapping_from_mapping_workflow(self):
notification_settings = {
self.user: {
NotificationScopeType.USER: {
ExternalProviders.EMAIL: NotificationSettingOptionValues.SUBSCRIBE_ONLY
}
}
}
mapping = _get_setting_mapping_from_mapping(
notification_settings,
self.user,
NotificationSettingTypes.WORKFLOW,
)
assert mapping == {ExternalProviders.EMAIL: NotificationSettingOptionValues.SUBSCRIBE_ONLY}

def test_get_deploy_values_by_provider_empty_settings(self):
values_by_provider = get_values_by_provider_by_type(
{},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
from unittest import TestCase

from sentry.models import User
from sentry.notifications.helpers import _get_setting_mapping_from_mapping
from sentry.notifications.types import (
NotificationScopeType,
NotificationSettingOptionValues,
NotificationSettingTypes,
)
from sentry.types.integrations import ExternalProviders


class GetSettingMappingFromMappingTest(TestCase):
def setUp(self):
self.user = User(id=1)

def test_get_setting_mapping_from_mapping_issue_alerts(self):
notification_settings = {
self.user: {
NotificationScopeType.USER: {
ExternalProviders.EMAIL: NotificationSettingOptionValues.ALWAYS
}
}
}
mapping = _get_setting_mapping_from_mapping(
notification_settings,
self.user,
NotificationSettingTypes.ISSUE_ALERTS,
)
assert mapping == {
ExternalProviders.EMAIL: NotificationSettingOptionValues.ALWAYS,
ExternalProviders.SLACK: NotificationSettingOptionValues.NEVER,
}

def test_get_setting_mapping_from_mapping_deploy(self):
notification_settings = {
self.user: {
NotificationScopeType.USER: {
ExternalProviders.EMAIL: NotificationSettingOptionValues.COMMITTED_ONLY
}
}
}
mapping = _get_setting_mapping_from_mapping(
notification_settings,
self.user,
NotificationSettingTypes.DEPLOY,
)
assert mapping == {
ExternalProviders.EMAIL: NotificationSettingOptionValues.COMMITTED_ONLY,
ExternalProviders.SLACK: NotificationSettingOptionValues.NEVER,
}

def test_get_setting_mapping_from_mapping_workflow(self):
notification_settings = {
self.user: {
NotificationScopeType.USER: {
ExternalProviders.EMAIL: NotificationSettingOptionValues.SUBSCRIBE_ONLY
}
}
}
mapping = _get_setting_mapping_from_mapping(
notification_settings,
self.user,
NotificationSettingTypes.WORKFLOW,
)
assert mapping == {
ExternalProviders.EMAIL: NotificationSettingOptionValues.SUBSCRIBE_ONLY,
ExternalProviders.SLACK: NotificationSettingOptionValues.NEVER,
}

def test_get_setting_mapping_from_mapping_empty(self):
mapping = _get_setting_mapping_from_mapping(
{}, self.user, NotificationSettingTypes.ISSUE_ALERTS
)
assert mapping == {
ExternalProviders.EMAIL: NotificationSettingOptionValues.ALWAYS,
ExternalProviders.SLACK: NotificationSettingOptionValues.NEVER,
}

def test_get_setting_mapping_from_mapping_slack_never(self):
notification_settings = {
self.user: {
NotificationScopeType.USER: {
ExternalProviders.SLACK: NotificationSettingOptionValues.NEVER
}
}
}

mapping = _get_setting_mapping_from_mapping(
notification_settings,
self.user,
NotificationSettingTypes.ISSUE_ALERTS,
)
assert mapping == {
ExternalProviders.EMAIL: NotificationSettingOptionValues.ALWAYS,
ExternalProviders.SLACK: NotificationSettingOptionValues.NEVER,
}

def test_get_setting_mapping_from_mapping_slack_always(self):
notification_settings = {
self.user: {
NotificationScopeType.USER: {
ExternalProviders.SLACK: NotificationSettingOptionValues.ALWAYS
}
}
}

mapping = _get_setting_mapping_from_mapping(
notification_settings,
self.user,
NotificationSettingTypes.ISSUE_ALERTS,
)
assert mapping == {
ExternalProviders.EMAIL: NotificationSettingOptionValues.ALWAYS,
ExternalProviders.SLACK: NotificationSettingOptionValues.ALWAYS,
}

0 comments on commit c380c0b

Please sign in to comment.