Skip to content

♻️ ref(aci): update links in issue alert titles #89240

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

Merged
merged 3 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 39 additions & 13 deletions src/sentry/integrations/discord/message_builder/issues.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from sentry import tagstore
from sentry import features, tagstore
from sentry.eventstore.models import GroupEvent
from sentry.integrations.discord.message_builder import LEVEL_TO_COLOR
from sentry.integrations.discord.message_builder.base.base import DiscordMessageBuilder
Expand All @@ -16,12 +16,14 @@
build_footer,
get_color,
get_title_link,
get_title_link_workflow_engine_ui,
)
from sentry.integrations.types import ExternalProviders
from sentry.models.group import Group, GroupStatus
from sentry.models.project import Project
from sentry.models.rule import Rule
from sentry.notifications.notifications.base import ProjectNotification
from sentry.notifications.notifications.rules import get_key_from_rule_data

from ..message_builder.base.component import DiscordComponentCustomIds as CustomIds

Expand Down Expand Up @@ -55,24 +57,48 @@ def build(self, notification_uuid: str | None = None) -> dict[str, object]:
rule_id = None
rule_environment_id = None
if self.rules:
rule_id = self.rules[0].id
rule_environment_id = self.rules[0].environment_id
if features.has("organizations:workflow-engine-ui-links", self.group.organization):
rule_id = int(get_key_from_rule_data(self.rules[0], "workflow_id"))
elif features.has(
"organizations:workflow-engine-trigger-actions", self.group.organization
):
rule_id = int(get_key_from_rule_data(self.rules[0], "legacy_rule_id"))
else:
rule_id = self.rules[0].id
Comment on lines +61 to +68
Copy link
Member

Choose a reason for hiding this comment

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

I'm really starting to think we could use a utility method for this now. Seems like we implement this logic in a few places.


url = None

if features.has("organizations:workflow-engine-ui-links", self.group.organization):
url = get_title_link_workflow_engine_ui(
self.group,
self.event,
self.link_to_event,
self.issue_details,
self.notification,
ExternalProviders.DISCORD,
rule_id,
rule_environment_id,
notification_uuid=notification_uuid,
)
else:
url = get_title_link(
self.group,
self.event,
self.link_to_event,
self.issue_details,
self.notification,
ExternalProviders.DISCORD,
rule_id,
rule_environment_id,
notification_uuid=notification_uuid,
)

embeds = [
DiscordMessageEmbed(
title=build_attachment_title(obj),
description=build_attachment_text(self.group, self.event) or None,
url=get_title_link(
self.group,
self.event,
self.link_to_event,
self.issue_details,
self.notification,
ExternalProviders.DISCORD,
rule_id,
rule_environment_id,
notification_uuid=notification_uuid,
),
url=url,
color=LEVEL_TO_COLOR[get_color(event_for_tags, self.notification, self.group)],
# We can't embed urls in Discord embed footers.
footer=DiscordMessageEmbedFooter(
Expand Down
46 changes: 42 additions & 4 deletions src/sentry/integrations/messaging/message_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def fetch_environment_name(rule_env: int) -> str | None:
return env.name


def get_rule_environment_param(
def get_rule_environment_param_from_rule(
rule_id: int, rule_environment_id: int | None, organization: Organization
) -> dict[str, str]:
params = {}
Expand Down Expand Up @@ -145,7 +145,7 @@ def get_title_link(
# add in rule id if we have it
if rule_id:
other_params.update(
get_rule_environment_param(rule_id, rule_environment_id, group.organization)
get_rule_environment_param_from_rule(rule_id, rule_environment_id, group.organization)
)
# hard code for issue alerts
other_params["alert_rule_id"] = str(rule_id)
Expand Down Expand Up @@ -186,10 +186,48 @@ def get_title_link_workflow_engine_ui(
issue_details: bool,
notification: BaseNotification | None,
provider: ExternalProviders,
rule_id: int | None = None,
workflow_id: int | None = None,
environment_id: int | None = None,
notification_uuid: str | None = None,
) -> str:
raise NotImplementedError("Link building for workflow engine UI is not implemented")
other_params = {}
# add in rule id if we have it
if workflow_id:
if (
environment_id is not None
and (environment_name := fetch_environment_name(environment_id)) is not None
):
Comment on lines +196 to +199
Copy link
Member

Choose a reason for hiding this comment

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

Mostly curious, when can environment_id be none?

other_params["environment"] = environment_name
# hard code for issue alerts
other_params["workflow_id"] = str(workflow_id)
other_params["alert_type"] = "issue"

if event and link_to_event:
url = group.get_absolute_url(
params={"referrer": EXTERNAL_PROVIDERS[provider], **other_params},
event_id=event.event_id,
)

elif issue_details and notification:
referrer = notification.get_referrer(provider)
notification_uuid = notification.notification_uuid
url = group.get_absolute_url(
params={"referrer": referrer, "notification_uuid": notification_uuid, **other_params}
)
elif notification_uuid:
url = group.get_absolute_url(
params={
"referrer": EXTERNAL_PROVIDERS[provider],
"notification_uuid": notification_uuid,
**other_params,
}
)
else:
url = group.get_absolute_url(
params={"referrer": EXTERNAL_PROVIDERS[provider], **other_params}
)

return url


def build_attachment_text(group: Group, event: Event | GroupEvent | None = None) -> Any | None:
Expand Down
59 changes: 38 additions & 21 deletions src/sentry/integrations/slack/message_builder/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.core.exceptions import ObjectDoesNotExist
from sentry_relay.processing import parse_release

from sentry import tagstore
from sentry import features, tagstore
from sentry.constants import LOG_LEVELS
from sentry.eventstore.models import Event, GroupEvent
from sentry.identity.services.identity import RpcIdentity, identity_service
Expand All @@ -20,6 +20,7 @@
format_actor_option_slack,
format_actor_options_slack,
get_title_link,
get_title_link_workflow_engine_ui,
)
from sentry.integrations.slack.message_builder.base.block import BlockSlackMessageBuilder
from sentry.integrations.slack.message_builder.image_block_builder import ImageBlockBuilder
Expand Down Expand Up @@ -52,6 +53,7 @@
from sentry.models.rule import Rule
from sentry.models.team import Team
from sentry.notifications.notifications.base import ProjectNotification
from sentry.notifications.notifications.rules import get_key_from_rule_data
from sentry.notifications.utils.actions import BlockKitMessageAction, MessageAction
from sentry.notifications.utils.participants import (
dedupe_suggested_assignees,
Expand Down Expand Up @@ -455,21 +457,8 @@ def get_title_block(
self,
event_or_group: Event | GroupEvent | Group,
has_action: bool,
rule_id: int | None = None,
rule_environment_id: int | None = None,
notification_uuid: str | None = None,
title_link: str | None = None,
) -> SlackBlock:
title_link = get_title_link(
self.group,
self.event,
self.link_to_event,
self.issue_details,
self.notification,
ExternalProviders.SLACK,
rule_id,
rule_environment_id,
notification_uuid=notification_uuid,
)
# If using summary, put the body in the headline as well
headline = None
if self.issue_summary is not None:
Expand Down Expand Up @@ -622,20 +611,48 @@ def build(self, notification_uuid: str | None = None) -> SlackBlock:
rule_id = None
rule_environment_id = None
if self.rules:
rule_id = self.rules[0].id
rule_environment_id = self.rules[0].environment_id
if features.has("organizations:workflow-engine-ui-links", self.group.organization):
rule_id = int(get_key_from_rule_data(self.rules[0], "workflow_id"))
elif features.has(
"organizations:workflow-engine-trigger-actions", self.group.organization
):
rule_id = int(get_key_from_rule_data(self.rules[0], "legacy_rule_id"))
else:
rule_id = self.rules[0].id

# build up actions text
if self.actions and self.identity and not action_text:
# this means somebody is interacting with the message
action_text = get_action_text(self.actions, self.identity)
has_action = True

blocks = [
self.get_title_block(
event_or_group, has_action, rule_id, rule_environment_id, notification_uuid
title_link = None
if features.has("organizations:workflow-engine-ui-links", self.group.organization):
title_link = get_title_link_workflow_engine_ui(
self.group,
self.event,
self.link_to_event,
self.issue_details,
self.notification,
ExternalProviders.SLACK,
rule_id,
rule_environment_id,
notification_uuid=notification_uuid,
)
]
else:
title_link = get_title_link(
self.group,
self.event,
self.link_to_event,
self.issue_details,
self.notification,
ExternalProviders.SLACK,
rule_id,
rule_environment_id,
notification_uuid=notification_uuid,
)

blocks = [self.get_title_block(event_or_group, has_action, title_link)]

if culprit_block := self.get_culprit_block(event_or_group):
blocks.append(culprit_block)
Expand Down
Loading