Repository navigation
feat(np): Adds renderer for MSTeams issue alerts #124860
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
81113fd
feat(np): Adds renderer for MSTeams issue alerts
GabeVillalobos c390812
remove unused parameter
hobzcalvin 9618600
fix(np): Type the expected MSTeams issue card helper
hobzcalvin 5ea23df
test(np): Assert the MSTeams issue renderer is registered
hobzcalvin 774fd4b
harden tests
hobzcalvin d7f35d4
ref(np): Import MSTeams card builders inside renderer methods
hobzcalvin d494ad6
also pass the event
hobzcalvin c51c820
convert Event to GroupEvent, handle stale assignees as unassigned
hobzcalvin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
189 changes: 189 additions & 0 deletions
189
src/sentry/notifications/platform/msteams/renderers/issue.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Sequence | ||
| from datetime import datetime | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from sentry import eventstore | ||
| from sentry.integrations.types import IntegrationProviderSlug | ||
| from sentry.models.group import Group | ||
| from sentry.models.project import Project | ||
| from sentry.models.rule import Rule | ||
| from sentry.notifications.platform.msteams.provider import MSTeamsRenderable | ||
| from sentry.notifications.platform.registry import renderer_registry | ||
| from sentry.notifications.platform.renderer import NotificationRenderer | ||
| from sentry.notifications.platform.service import NotificationRenderError | ||
| from sentry.notifications.platform.templates.issue import IssueNotificationData | ||
| from sentry.notifications.platform.types import ( | ||
| NotificationData, | ||
| NotificationProviderKey, | ||
| NotificationRenderedTemplate, | ||
| NotificationSource, | ||
| ) | ||
| from sentry.services.eventstore.models import Event, GroupEvent | ||
| from sentry.types.actor import Actor | ||
|
|
||
| if TYPE_CHECKING: | ||
| from sentry.integrations.msteams.card_builder.block import ( | ||
| Action, | ||
| Block, | ||
| ColumnSetBlock, | ||
| TextBlock, | ||
| ) | ||
|
|
||
|
|
||
| @renderer_registry.register(NotificationProviderKey.MSTEAMS, sources=[NotificationSource.ISSUE]) | ||
| class IssueMSTeamsRenderer(NotificationRenderer[MSTeamsRenderable]): | ||
| @classmethod | ||
| def render[DataT: NotificationData]( | ||
| cls, *, data: DataT, rendered_template: NotificationRenderedTemplate | ||
| ) -> MSTeamsRenderable: | ||
| if not isinstance(data, IssueNotificationData): | ||
| raise ValueError(f"IssueMSTeamsRenderer does not support {data.__class__.__name__}") | ||
|
|
||
| from sentry.integrations.msteams.card_builder.base import MSTeamsMessageBuilder | ||
|
|
||
| # Retrieving Group and Event data is an anti-pattern, do not do this | ||
| # in permanent renderers. | ||
| try: | ||
| group = Group.objects.get_from_cache(id=data.group_id) | ||
| except Group.DoesNotExist: | ||
| raise NotificationRenderError(f"Group {data.group_id} not found") | ||
|
|
||
| event = None | ||
| if data.event_id: | ||
| try: | ||
| event = eventstore.backend.get_event_by_id( | ||
| project_id=group.project.id, | ||
| event_id=data.event_id, | ||
| group_id=data.group_id, | ||
| ) | ||
| if isinstance(event, Event): | ||
| event = event.for_group(group) | ||
| except Exception: | ||
| raise NotificationRenderError(f"Failed to retrieve event {data.event_id}") | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| rules = [data.rule.to_rule()] if data.rule else [] | ||
| issue_url = cls.build_issue_url(group=group, notification_uuid=data.notification_uuid) | ||
|
|
||
| fields: list[Block | None] = [ | ||
| cls.build_description(group=group, event=event), | ||
| cls.build_footer(group=group, event=event, rules=rules), | ||
| cls.build_assignee_note(group), | ||
| ] | ||
|
|
||
| return MSTeamsMessageBuilder().build( | ||
| title=cls.build_title(group=group, issue_url=issue_url), | ||
| fields=fields, | ||
| actions=cls.build_actions(issue_url=issue_url), | ||
| ) | ||
|
|
||
| @classmethod | ||
| def build_issue_url(cls, *, group: Group, notification_uuid: str) -> str: | ||
| params: dict[str, str] = {"referrer": IntegrationProviderSlug.MSTEAMS.value} | ||
| if notification_uuid: | ||
| params["notification_uuid"] = notification_uuid | ||
| return group.get_absolute_url(params=params) | ||
|
|
||
| @classmethod | ||
| def build_title(cls, *, group: Group, issue_url: str) -> TextBlock: | ||
| from sentry.integrations.messaging.message_builder import build_attachment_title | ||
| from sentry.integrations.msteams.card_builder.block import ( | ||
| TextSize, | ||
| TextWeight, | ||
| create_text_block, | ||
| ) | ||
|
|
||
| title_text = build_attachment_title(group) | ||
| return create_text_block( | ||
| f"[{title_text}]({issue_url})", | ||
| size=TextSize.LARGE, | ||
| weight=TextWeight.BOLDER, | ||
| ) | ||
|
|
||
| @classmethod | ||
| def build_description( | ||
| cls, *, group: Group, event: Event | GroupEvent | None | ||
| ) -> TextBlock | None: | ||
| from sentry.integrations.messaging.message_builder import build_attachment_text | ||
| from sentry.integrations.msteams.card_builder.block import ( | ||
| TextSize, | ||
| TextWeight, | ||
| create_text_block, | ||
| ) | ||
|
|
||
| text = build_attachment_text(group, event) | ||
| if text: | ||
| return create_text_block(text, size=TextSize.MEDIUM, weight=TextWeight.BOLDER) | ||
| return None | ||
|
|
||
| @classmethod | ||
| def build_footer( | ||
| cls, | ||
| *, | ||
| group: Group, | ||
| event: Event | GroupEvent | None, | ||
| rules: Sequence[Rule], | ||
| ) -> ColumnSetBlock: | ||
| from sentry.integrations.messaging.message_builder import build_footer | ||
| from sentry.integrations.msteams.card_builder import MSTEAMS_URL_FORMAT | ||
| from sentry.integrations.msteams.card_builder.block import ( | ||
| ContentAlignment, | ||
| TextSize, | ||
| TextWeight, | ||
| create_column_block, | ||
| create_column_set_block, | ||
| create_footer_column_block, | ||
| create_footer_logo_block, | ||
| create_footer_text_block, | ||
| create_text_block, | ||
| ) | ||
| from sentry.integrations.msteams.card_builder.utils import IssueConstants | ||
|
|
||
| project = Project.objects.get_from_cache(id=group.project_id) | ||
|
hobzcalvin marked this conversation as resolved.
|
||
| footer_text = build_footer( | ||
| group=group, project=project, url_format=MSTEAMS_URL_FORMAT, rules=rules | ||
| ) | ||
|
|
||
| ts: datetime = group.last_seen | ||
| date = max(ts, event.datetime) if event else ts | ||
| date_str = date.replace(microsecond=0).isoformat() | ||
|
|
||
| return create_column_set_block( | ||
| create_column_block(create_footer_logo_block()), | ||
| create_footer_column_block(create_footer_text_block(footer_text)), | ||
| create_column_block( | ||
| create_text_block( | ||
| IssueConstants.DATE_FORMAT.format(date=date_str), | ||
| size=TextSize.SMALL, | ||
| weight=TextWeight.LIGHTER, | ||
| horizontalAlignment=ContentAlignment.CENTER, | ||
| wrap=False, | ||
| ), | ||
| verticalContentAlignment=ContentAlignment.CENTER, | ||
| ), | ||
| ) | ||
|
|
||
| @classmethod | ||
| def build_assignee_note(cls, group: Group) -> TextBlock | None: | ||
| from sentry.integrations.messaging.message_builder import format_actor_option_non_slack | ||
| from sentry.integrations.msteams.card_builder.block import TextSize, create_text_block | ||
| from sentry.integrations.msteams.card_builder.utils import IssueConstants | ||
|
|
||
| try: | ||
| assignee = group.get_assignee() | ||
| except Actor.InvalidActor: | ||
| assignee = None | ||
| if assignee: | ||
| assignee_text = format_actor_option_non_slack(assignee)["text"] | ||
| return create_text_block( | ||
| IssueConstants.ASSIGNEE_NOTE.format(assignee=assignee_text), | ||
| size=TextSize.SMALL, | ||
| ) | ||
| return None | ||
|
|
||
| @classmethod | ||
| def build_actions(cls, *, issue_url: str) -> list[Action]: | ||
| from sentry.integrations.msteams.card_builder.block import ActionType, OpenUrlAction | ||
|
|
||
| return [OpenUrlAction(type=ActionType.OPEN_URL, title="View Issue", url=issue_url)] | ||
Empty file.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think this would be better at the top unless its causing an import error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I never want inline imports, but there's a big complex set of reasons why it works out this way, involving other renderers, the jira-app-id that comes from sentry options...at least this is building on precedent from other renderers.