Skip to content

Commit

Permalink
fix: patch slack ID reference issue (#5314)
Browse files Browse the repository at this point in the history
## Which issue(s) this PR closes

Fixes grafana/irm#469

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.
  • Loading branch information
joeyorlando authored Dec 2, 2024
1 parent 1829da9 commit 26ff937
Show file tree
Hide file tree
Showing 9 changed files with 796 additions and 224 deletions.
16 changes: 10 additions & 6 deletions engine/apps/alerts/models/alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -1980,12 +1980,16 @@ def is_presented_in_slack(self):

@property
def slack_channel_id(self) -> str | None:
if not self.channel.organization.slack_team_identity:
return None
elif self.slack_message:
return self.slack_message.channel.slack_id
elif self.channel_filter:
return self.channel_filter.slack_channel_id_or_org_default_id
channel_filter = self.channel_filter

if self.slack_message:
# TODO: once _channel_id has been fully migrated to channel, remove _channel_id
# see https://raintank-corp.slack.com/archives/C06K1MQ07GS/p173255546
#
# return self.slack_message.channel.slack_id
return self.slack_message._channel_id
elif channel_filter and channel_filter.slack_channel_or_org_default:
return channel_filter.slack_channel_or_org_default.slack_id
return None

@property
Expand Down
10 changes: 2 additions & 8 deletions engine/apps/alerts/models/channel_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,8 @@ def slack_channel_slack_id(self) -> typing.Optional[str]:
return self.slack_channel.slack_id if self.slack_channel else None

@property
def slack_channel_id_or_org_default_id(self):
organization = self.alert_receive_channel.organization

if organization.slack_team_identity is None:
return None
elif self.slack_channel_slack_id is None:
return organization.default_slack_channel_slack_id
return self.slack_channel_slack_id
def slack_channel_or_org_default(self) -> typing.Optional["SlackChannel"]:
return self.slack_channel or self.alert_receive_channel.organization.default_slack_channel

@property
def str_for_clients(self):
Expand Down
63 changes: 62 additions & 1 deletion engine/apps/alerts/tests/test_alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ def test_delete_by_user(


@pytest.mark.django_db
def test_integration_config_on_alert_group_created(make_organization, make_alert_receive_channel, make_channel_filter):
def test_integration_config_on_alert_group_created(make_organization, make_alert_receive_channel):
organization = make_organization()
alert_receive_channel = make_alert_receive_channel(organization, grouping_id_template="group_to_one_group")

Expand Down Expand Up @@ -806,3 +806,64 @@ def test_alert_group_created_if_resolve_condition_but_auto_resolving_disabled(

# the alert will create a new alert group
assert alert.group != resolved_alert_group


class TestAlertGroupSlackChannelID:
@pytest.mark.django_db
def test_slack_channel_id_with_slack_message(
self,
make_organization_with_slack_team_identity,
make_alert_receive_channel,
make_slack_channel,
make_slack_message,
make_alert_group,
):
"""
Test that slack_channel_id returns the _channel_id from slack_message when slack_message exists.
"""
organization, slack_team_identity = make_organization_with_slack_team_identity()
alert_receive_channel = make_alert_receive_channel(organization)
alert_group = make_alert_group(alert_receive_channel)
slack_channel = make_slack_channel(slack_team_identity)
slack_message = make_slack_message(slack_channel, alert_group=alert_group)

# Assert that slack_channel_id returns the _channel_id from slack_message
assert alert_group.slack_channel_id == slack_message._channel_id

@pytest.mark.django_db
def test_slack_channel_id_with_channel_filter(
self,
make_organization_with_slack_team_identity,
make_alert_receive_channel,
make_channel_filter,
make_slack_channel,
make_alert_group,
):
"""
Test that slack_channel_id returns the slack_id from channel_filter.slack_channel_or_org_default.
"""
organization, slack_team_identity = make_organization_with_slack_team_identity()
alert_receive_channel = make_alert_receive_channel(organization)
slack_channel = make_slack_channel(slack_team_identity)
channel_filter = make_channel_filter(alert_receive_channel, slack_channel=slack_channel)
alert_group = make_alert_group(alert_receive_channel, channel_filter=channel_filter)

# Assert that slack_channel_id returns the slack_id from the channel filter's Slack channel
assert alert_group.slack_channel_id == slack_channel.slack_id

@pytest.mark.django_db
def test_slack_channel_id_no_slack_message_no_channel_filter(
self,
make_organization_with_slack_team_identity,
make_alert_receive_channel,
make_alert_group,
):
"""
Test that slack_channel_id returns None when there is no slack_message and no channel_filter.
"""
organization, _ = make_organization_with_slack_team_identity()
alert_receive_channel = make_alert_receive_channel(organization)
alert_group = make_alert_group(alert_receive_channel, channel_filter=None)

# Assert that slack_channel_id is None
assert alert_group.slack_channel_id is None
64 changes: 64 additions & 0 deletions engine/apps/alerts/tests/test_channel_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,67 @@ def test_channel_filter_using_filter_labels(
assert ChannelFilter.select_filter(alert_receive_channel, {"title": "Test Title", "value": 5}, labels) == (
custom_channel_filter if should_match else default_channel_filter
)


class TestChannelFilterSlackChannelOrOrgDefault:
@pytest.mark.django_db
def test_slack_channel_or_org_default_with_slack_channel(
self,
make_organization_with_slack_team_identity,
make_alert_receive_channel,
make_channel_filter,
make_slack_channel,
):
"""
Test that slack_channel_or_org_default returns self.slack_channel when it is set.
"""
organization, slack_team_identity = make_organization_with_slack_team_identity()
alert_receive_channel = make_alert_receive_channel(organization)
slack_channel = make_slack_channel(slack_team_identity)
channel_filter = make_channel_filter(alert_receive_channel=alert_receive_channel, slack_channel=slack_channel)

# Assert that slack_channel_or_org_default returns slack_channel
assert channel_filter.slack_channel_or_org_default == slack_channel

@pytest.mark.django_db
def test_slack_channel_or_org_default_with_org_default(
self,
make_slack_team_identity,
make_organization,
make_alert_receive_channel,
make_channel_filter,
make_slack_channel,
):
"""
Test that slack_channel_or_org_default returns organization's default_slack_channel when slack_channel is None.
"""
slack_team_identity = make_slack_team_identity()
default_slack_channel = make_slack_channel(slack_team_identity)
organization = make_organization(
slack_team_identity=slack_team_identity,
default_slack_channel=default_slack_channel,
)
alert_receive_channel = make_alert_receive_channel(organization)
channel_filter = make_channel_filter(alert_receive_channel, slack_channel=None)

# Assert that slack_channel_or_org_default returns organization's default_slack_channel
assert channel_filter.slack_channel_or_org_default == default_slack_channel

@pytest.mark.django_db
def test_slack_channel_or_org_default_none(
self,
make_organization_with_slack_team_identity,
make_alert_receive_channel,
make_channel_filter,
):
"""
Test that slack_channel_or_org_default returns None when both slack_channel and organization's default_slack_channel are None.
"""
organization, _ = make_organization_with_slack_team_identity()
assert organization.default_slack_channel is None

alert_receive_channel = make_alert_receive_channel(organization)
channel_filter = make_channel_filter(alert_receive_channel=alert_receive_channel, slack_channel=None)

# Assert that slack_channel_or_org_default returns None
assert channel_filter.slack_channel_or_org_default is None
10 changes: 2 additions & 8 deletions engine/apps/alerts/tests/test_notify_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,18 @@

@pytest.mark.django_db
def test_notify_all(
make_organization,
make_slack_team_identity,
make_organization_and_user_with_slack_identities,
make_slack_channel,
make_user,
make_user_notification_policy,
make_escalation_chain,
make_escalation_policy,
make_channel_filter,
make_alert_receive_channel,
make_alert_group,
):
organization = make_organization()
slack_team_identity = make_slack_team_identity()
organization, user, slack_team_identity, _ = make_organization_and_user_with_slack_identities()
slack_channel = make_slack_channel(slack_team_identity)
organization.slack_team_identity = slack_team_identity
organization.save()

user = make_user(organization=organization)
make_user_notification_policy(
user=user,
step=UserNotificationPolicy.Step.NOTIFY,
Expand Down
Loading

0 comments on commit 26ff937

Please sign in to comment.