Skip to content
Draft
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
7 changes: 3 additions & 4 deletions src/sentry/integrations/messaging/message_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from sentry.notifications.notifications.base import BaseNotification
from sentry.notifications.notifications.rules import AlertRuleNotification
from sentry.notifications.utils.links import create_link_to_workflow
from sentry.notifications.utils.rules import get_key_from_rule_data, get_rule_or_workflow_id
from sentry.notifications.utils.rules import get_rule_or_workflow_id
from sentry.services.eventstore.models import Event, GroupEvent
from sentry.users.services.user import RpcUser
from sentry.utils.http import absolute_uri
Expand Down Expand Up @@ -247,10 +247,9 @@ def build_attachment_replay_link(
return None


def build_rule_url(rule: Any, group: Group, project: Project) -> str:
def build_rule_url(rule_id: str, group: Group, project: Project) -> str:
org_slug = group.organization.slug
project_slug = project.slug
rule_id = get_key_from_rule_data(rule, "legacy_rule_id")
rule_url = f"/organizations/{org_slug}/issues/alerts/rules/{project_slug}/{rule_id}/details/"

return absolute_uri(rule_url)
Expand All @@ -269,7 +268,7 @@ def build_footer(
case "workflow_id":
rule_url = absolute_uri(create_link_to_workflow(group.organization.slug, value))
case "legacy_rule_id":
rule_url = build_rule_url(rules[0], group, project)
rule_url = build_rule_url(value, group, project)

# If this notification is triggered via the "Send Test Notification"
# button then the label is not defined, but the url works.
Expand Down
2 changes: 1 addition & 1 deletion src/sentry/integrations/slack/message_builder/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def build_slack_footer(
case "workflow_id":
rule_url = absolute_uri(create_link_to_workflow(group.organization.slug, value))
case "legacy_rule_id":
rule_url = build_rule_url(rules[0], group, project)
rule_url = build_rule_url(value, group, project)
# If this notification is triggered via the "Send Test Notification"
# button then the label is not defined, but the url works.
text = rules[0].label if rules[0].label else "Test Alert"
Expand Down
2 changes: 1 addition & 1 deletion src/sentry/notifications/notifications/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def get_notification_title(
case "workflow_id":
rule_url = absolute_uri(create_link_to_workflow(self.organization.slug, value))
case "legacy_rule_id":
rule_url = build_rule_url(self.rules[0], self.group, self.project)
rule_url = build_rule_url(value, self.group, self.project)

title_str += (
f" {self.format_url(text=self.rules[0].label, url=rule_url, provider=provider)}"
Expand Down
28 changes: 28 additions & 0 deletions tests/sentry/integrations/msteams/test_message_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,34 @@ def test_assigned_issue_message(self) -> None:
assert ActionType.SUBMIT == assign_action["type"]
assert "Unassign" == assign_action["title"]

def test_issue_message_builder_rule_without_legacy_rule_id_in_actions(self) -> None:
"""Regression test: build_group_card must not raise AssertionError when a rule's
actions list does not contain 'legacy_rule_id'. The rule's own id should be used
as a fallback."""
rule_without_legacy_id = self.create_project_rule(
name="rule_no_legacy", include_legacy_rule_id=False
)

# Verify that legacy_rule_id is indeed absent from the rule actions data
assert rule_without_legacy_id.data["actions"][0].get("legacy_rule_id") is None

# This must not raise an AssertionError
issue_card = MSTeamsIssueMessageBuilder(
group=self.group1,
event=self.event1,
rules=[rule_without_legacy_id],
integration=self.integration,
).build_group_card()

body = issue_card["body"]
footer = body[2]
assert _is_column_set_block(footer)
issue_id_and_rule = footer["columns"][1]["items"][0]
assert _is_text_block(issue_id_and_rule)
# The footer should contain the rule label and a URL using str(rule.id)
assert "rule_no_legacy" in issue_id_and_rule["text"]
assert str(rule_without_legacy_id.id) in issue_id_and_rule["text"]


class MSTeamsNotificationMessageBuilderTest(TestCase):
def setUp(self) -> None:
Expand Down
Loading