Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: patch slack ID reference issue #5314

Merged
merged 10 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
12 changes: 9 additions & 3 deletions engine/apps/alerts/models/alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -1980,12 +1980,18 @@ def is_presented_in_slack(self):

@property
def slack_channel_id(self) -> str | None:
channel_filter = self.channel_filter

if not self.channel.organization.slack_team_identity:
return None
joeyorlando marked this conversation as resolved.
Show resolved Hide resolved
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
# 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
joeyorlando marked this conversation as resolved.
Show resolved Hide resolved
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
81 changes: 80 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,82 @@ 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_no_slack_team_identity(
self,
make_organization,
make_alert_receive_channel,
make_alert_group,
):
"""
Test that slack_channel_id returns None when the organization has no slack_team_identity.
"""
# Create an organization without a Slack team identity
organization = make_organization(slack_team_identity=None)
alert_receive_channel = make_alert_receive_channel(organization)
alert_group = make_alert_group(alert_receive_channel)

# Assert that slack_channel_id is None
assert alert_group.slack_channel_id is None

@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)
Comment on lines -23 to -29
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make_organization_and_user_with_slack_identities does the same thing as these 6 lines

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