-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(np): Add custom blocks for formatting and styling to BE #102975
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
Changes from 5 commits
138e34d
417e76a
27f7ae3
b50e876
b0102c3
0e95eb1
1bc6553
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from sentry.notifications.platform.provider import NotificationProvider, NotificationProviderError | ||
|
|
@@ -8,6 +10,10 @@ | |
| PreparedIntegrationNotificationTarget, | ||
| ) | ||
| from sentry.notifications.platform.types import ( | ||
| NotificationBodyFormattingBlock, | ||
| NotificationBodyFormattingBlockType, | ||
| NotificationBodyTextBlock, | ||
| NotificationBodyTextBlockType, | ||
| NotificationData, | ||
| NotificationProviderKey, | ||
| NotificationRenderedTemplate, | ||
|
|
@@ -17,7 +23,7 @@ | |
| from sentry.organizations.services.organization.model import RpcOrganizationSummary | ||
|
|
||
| if TYPE_CHECKING: | ||
| from sentry.integrations.msteams.card_builder.block import AdaptiveCard | ||
| from sentry.integrations.msteams.card_builder.block import AdaptiveCard, Block | ||
|
|
||
| type MSTeamsRenderable = AdaptiveCard | ||
|
|
||
|
|
@@ -35,8 +41,6 @@ def render[DataT: NotificationData]( | |
| Action, | ||
| ActionSet, | ||
| ActionType, | ||
| AdaptiveCard, | ||
| Block, | ||
| ImageBlock, | ||
| OpenUrlAction, | ||
| TextSize, | ||
|
|
@@ -47,9 +51,8 @@ def render[DataT: NotificationData]( | |
| title_text = create_text_block( | ||
| text=rendered_template.subject, size=TextSize.LARGE, weight=TextWeight.BOLDER | ||
| ) | ||
| body_text = create_text_block(text=rendered_template.body) | ||
|
|
||
| body_blocks: list[Block] = [title_text, body_text] | ||
| body_text = cls.render_body_blocks(rendered_template.body) | ||
| body_blocks: list[Block] = [title_text, *body_text] | ||
|
|
||
| if len(rendered_template.actions) > 0: | ||
| actions: list[Action] = [] | ||
|
|
@@ -81,6 +84,33 @@ def render[DataT: NotificationData]( | |
| } | ||
| return card | ||
|
|
||
| @classmethod | ||
| def render_body_blocks(cls, body: list[NotificationBodyFormattingBlock]) -> list[Block]: | ||
| from sentry.integrations.msteams.card_builder.block import ( | ||
| create_code_block, | ||
| create_text_block, | ||
| ) | ||
|
|
||
| body_blocks: list[Block] = [] | ||
| for block in body: | ||
| if block.type == NotificationBodyFormattingBlockType.PARAGRAPH: | ||
| body_blocks.append(create_text_block(text=cls.render_text_blocks(block.blocks))) | ||
| elif block.type == NotificationBodyFormattingBlockType.CODE_BLOCK: | ||
| body_blocks.append(create_code_block(text=cls.render_text_blocks(block.blocks))) | ||
| return body_blocks | ||
|
|
||
| @classmethod | ||
| def render_text_blocks(cls, blocks: list[NotificationBodyTextBlock]) -> str: | ||
| texts = [] | ||
| for block in blocks: | ||
| if block.type == NotificationBodyTextBlockType.PLAIN_TEXT: | ||
| texts.append(block.text) | ||
| elif block.type == NotificationBodyTextBlockType.BOLD_TEXT: | ||
| texts.append(f"**{block.text}**") | ||
| elif block.type == NotificationBodyTextBlockType.CODE: | ||
| texts.append(f"`{block.text}`") | ||
| return " ".join(texts) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Markdown Formatting Vulnerable to User InputThe
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've decided this a fine sacrifice, since the alternative is a lot of selective formatting. If it's a big problem I'll come back and do a patch |
||
|
|
||
|
|
||
| @provider_registry.register(NotificationProviderKey.MSTEAMS) | ||
| class MSTeamsNotificationProvider(NotificationProvider[MSTeamsRenderable]): | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.