Skip to content
Merged
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
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ module = [
"sentry.integrations.pagerduty.actions.form",
"sentry.integrations.pipeline",
"sentry.integrations.slack.message_builder.notifications.issues",
"sentry.integrations.slack.notifications",
"sentry.integrations.slack.webhooks.command",
"sentry.integrations.slack.webhooks.event",
"sentry.integrations.utils.sync",
Expand Down
3 changes: 2 additions & 1 deletion src/sentry/integrations/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from sentry.models.team import Team
from sentry.notifications.notifications.base import BaseNotification
from sentry.types.actor import Actor
from sentry.users.models.user import User
from sentry.users.services.user import RpcUser


Expand Down Expand Up @@ -102,7 +103,7 @@ def _get_channel_and_integration_by_team(

def get_integrations_by_channel_by_recipient(
organization: Organization,
recipients: Iterable[Actor],
recipients: Iterable[Actor | User],
provider: ExternalProviders,
) -> Mapping[Actor, Mapping[str, RpcIntegration]]:
output: MutableMapping[Actor, Mapping[str, RpcIntegration]] = defaultdict(dict)
Expand Down
19 changes: 16 additions & 3 deletions src/sentry/integrations/slack/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@
IntegrationMetadata,
IntegrationProvider,
)
from sentry.integrations.mixins import NotifyBasicMixin
from sentry.integrations.models.integration import Integration
from sentry.integrations.slack.metrics import (
SLACK_NOTIFY_MIXIN_FAILURE_DATADOG_METRIC,
SLACK_NOTIFY_MIXIN_SUCCESS_DATADOG_METRIC,
)
from sentry.integrations.slack.sdk_client import SlackSdkClient
from sentry.integrations.slack.tasks.link_slack_user_identities import link_slack_user_identities
from sentry.organizations.services.organization.model import RpcOrganization
from sentry.pipeline import NestedPipelineView
from sentry.pipeline.views.base import PipelineView
from sentry.shared_integrations.exceptions import IntegrationError
from sentry.utils import metrics
from sentry.utils.http import absolute_uri

from .notifications import SlackNotifyBasicMixin

_logger = logging.getLogger("sentry.integrations.slack")

Channel = namedtuple("Channel", ["name", "id"])
Expand Down Expand Up @@ -73,7 +77,7 @@
)


class SlackIntegration(SlackNotifyBasicMixin, IntegrationInstallation):
class SlackIntegration(NotifyBasicMixin, IntegrationInstallation):
def get_client(self) -> SlackSdkClient:
return SlackSdkClient(integration_id=self.model.id)

Expand All @@ -85,6 +89,15 @@ def get_config_data(self) -> Mapping[str, str]:
)
return {"installationType": metadata_.get("installation_type", default_installation)}

def send_message(self, channel_id: str, message: str) -> None:
client = self.get_client()

try:
client.chat_postMessage(channel=channel_id, text=message)
metrics.incr(SLACK_NOTIFY_MIXIN_SUCCESS_DATADOG_METRIC, sample_rate=1.0)
except SlackApiError:
metrics.incr(SLACK_NOTIFY_MIXIN_FAILURE_DATADOG_METRIC, sample_rate=1.0)


class SlackIntegrationProvider(IntegrationProvider):
key = "slack"
Expand Down
25 changes: 0 additions & 25 deletions src/sentry/integrations/slack/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@
from typing import Any

import sentry_sdk
from slack_sdk.errors import SlackApiError

from sentry.integrations.mixins import NotifyBasicMixin
from sentry.integrations.notifications import get_integrations_by_channel_by_recipient
from sentry.integrations.slack.metrics import (
SLACK_NOTIFY_MIXIN_FAILURE_DATADOG_METRIC,
SLACK_NOTIFY_MIXIN_SUCCESS_DATADOG_METRIC,
)
from sentry.integrations.slack.service import SlackService
from sentry.integrations.slack.utils.errors import CHANNEL_NOT_FOUND, unpack_slack_api_error
from sentry.integrations.types import ExternalProviders
from sentry.notifications.notifications.base import BaseNotification
from sentry.notifications.notify import register_notification_provider
Expand All @@ -25,24 +18,6 @@
logger = logging.getLogger("sentry.notifications")


class SlackNotifyBasicMixin(NotifyBasicMixin):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this doesn't need to live separately, so moved it under SlackIntegration

def send_message(self, channel_id: str, message: str) -> None:
client = self.get_client()

try:
client.chat_postMessage(channel=channel_id, text=message)
metrics.incr(SLACK_NOTIFY_MIXIN_SUCCESS_DATADOG_METRIC, sample_rate=1.0)
except SlackApiError as e:
metrics.incr(SLACK_NOTIFY_MIXIN_FAILURE_DATADOG_METRIC, sample_rate=1.0)

# TODO: remove this
if unpack_slack_api_error(e) != CHANNEL_NOT_FOUND:
logger.exception(
"slack.slash-response.error",
extra={"error": str(e)},
)


@register_notification_provider(ExternalProviders.SLACK)
def send_notification_as_slack(
notification: BaseNotification,
Expand Down
3 changes: 2 additions & 1 deletion src/sentry/integrations/slack/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
NotificationActionNotificationMessage,
NotificationActionNotificationMessageRepository,
)
from sentry.integrations.services.integration import RpcIntegration
from sentry.integrations.slack.message_builder.base.block import BlockSlackMessageBuilder
from sentry.integrations.slack.message_builder.notifications import get_message_builder
from sentry.integrations.slack.message_builder.types import SlackBlock
Expand Down Expand Up @@ -451,7 +452,7 @@ def notify_recipient(
recipient: Actor,
attachments: SlackBlock,
channel: str,
integration: Integration,
integration: Integration | RpcIntegration,
shared_context: Mapping[str, Any],
) -> None:
from sentry.integrations.slack.tasks.post_message import post_message, post_message_control
Expand Down
Loading