Skip to content

Commit 7dca844

Browse files
authored
🔧 chore: update msteams notifications typing (#89577)
1 parent f85602f commit 7dca844

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ module = [
131131
"sentry.integrations.github.integration",
132132
"sentry.integrations.gitlab.issues",
133133
"sentry.integrations.jira.integration",
134-
"sentry.integrations.msteams.notifications",
135134
"sentry.integrations.pagerduty.actions.form",
136135
"sentry.integrations.pipeline",
137136
"sentry.integrations.slack.message_builder.notifications.issues",

src/sentry/integrations/msteams/card_builder/notifications.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from sentry.integrations.types import ExternalProviders
1515
from sentry.notifications.notifications.activity.base import GroupActivityNotification
1616
from sentry.notifications.notifications.base import BaseNotification
17+
from sentry.notifications.notifications.rules import AlertRuleNotification
1718
from sentry.notifications.utils.actions import MessageAction
1819
from sentry.types.actor import Actor
1920

@@ -122,7 +123,7 @@ def build_notification_card(self):
122123
class MSTeamsIssueNotificationsMessageBuilder(MSTeamsNotificationsMessageBuilder):
123124
def __init__(
124125
self,
125-
notification: GroupActivityNotification,
126+
notification: GroupActivityNotification | AlertRuleNotification,
126127
context: Mapping[str, Any],
127128
recipient: Actor,
128129
):

src/sentry/integrations/msteams/notifications.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from sentry.integrations.msteams.utils import get_user_conversation_id
1111
from sentry.integrations.notifications import get_context, get_integrations_by_channel_by_recipient
1212
from sentry.integrations.types import ExternalProviders
13-
from sentry.models.team import Team
1413
from sentry.notifications.notifications.activity.assigned import AssignedActivityNotification
14+
from sentry.notifications.notifications.activity.base import GroupActivityNotification
1515
from sentry.notifications.notifications.activity.escalating import EscalatingActivityNotification
1616
from sentry.notifications.notifications.activity.note import NoteActivityNotification
1717
from sentry.notifications.notifications.activity.regression import RegressionActivityNotification
@@ -25,7 +25,6 @@
2525
from sentry.notifications.notifications.rules import AlertRuleNotification
2626
from sentry.notifications.notify import register_notification_provider
2727
from sentry.types.actor import Actor
28-
from sentry.users.models.user import User
2928
from sentry.utils import metrics
3029

3130
from .card_builder.notifications import (
@@ -47,8 +46,13 @@
4746
RegressionActivityNotification,
4847
EscalatingActivityNotification,
4948
]
50-
MESSAGE_BUILDERS = {
49+
50+
BASE_MESSAGE_BUILDERS = {
5151
"SlackNotificationsMessageBuilder": MSTeamsNotificationsMessageBuilder,
52+
}
53+
54+
# For Group-based notifications, it is possible we use a builder that is generic OR uses a group-specific builder
55+
GROUP_MESSAGE_BUILDERS = BASE_MESSAGE_BUILDERS | {
5256
"IssueNotificationMessageBuilder": MSTeamsIssueNotificationsMessageBuilder,
5357
}
5458

@@ -62,10 +66,19 @@ def is_supported_notification_type(notification: BaseNotification) -> bool:
6266
)
6367

6468

65-
def get_notification_card(
66-
notification: BaseNotification, context: Mapping[str, Any], recipient: User | Team | Actor
69+
def get_group_notification_card(
70+
notification: GroupActivityNotification | AlertRuleNotification,
71+
context: Mapping[str, Any],
72+
recipient: Actor,
73+
) -> AdaptiveCard:
74+
cls = GROUP_MESSAGE_BUILDERS[notification.message_builder]
75+
return cls(notification, context, recipient).build_notification_card()
76+
77+
78+
def get_base_notification_card(
79+
notification: BaseNotification, context: Mapping[str, Any], recipient: Actor
6780
) -> AdaptiveCard:
68-
cls = MESSAGE_BUILDERS[notification.message_builder]
81+
cls = BASE_MESSAGE_BUILDERS[notification.message_builder]
6982
return cls(notification, context, recipient).build_notification_card()
7083

7184

@@ -95,7 +108,12 @@ def send_notification_as_msteams(
95108
context = get_context(notification, recipient, shared_context, extra_context)
96109

97110
with sentry_sdk.start_span(op="notification.send_msteams", name="gen_attachments"):
98-
card = get_notification_card(notification, context, recipient)
111+
if isinstance(notification, GroupActivityNotification) or isinstance(
112+
notification, AlertRuleNotification
113+
):
114+
card = get_group_notification_card(notification, context, recipient)
115+
else:
116+
card = get_base_notification_card(notification, context, recipient)
99117

100118
for channel, integration in integrations_by_channel.items():
101119
conversation_id = get_user_conversation_id(integration, channel)

src/sentry/integrations/msteams/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
OpenPeriodContext,
1313
)
1414
from sentry.integrations.models.integration import Integration
15-
from sentry.integrations.services.integration import integration_service
15+
from sentry.integrations.services.integration import RpcIntegration, integration_service
1616
from sentry.models.organization import Organization
1717

1818
from .client import MsTeamsClient, MsTeamsPreInstallClient, get_token_data
@@ -41,7 +41,7 @@ def channel_filter(channel, name):
4141
return name.lower() == "general"
4242

4343

44-
def get_user_conversation_id(integration: Integration, user_id: str) -> str:
44+
def get_user_conversation_id(integration: Integration | RpcIntegration, user_id: str) -> str:
4545
"""
4646
Get the user_conversation_id even if `integration.metadata.tenant_id` is not set.
4747
"""

0 commit comments

Comments
 (0)