Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

slack_integration: Use `UrlOptions.CHANNELS_MAPPING`.

PieterCK24608972638e
diff --git a/zerver/lib/integrations.py b/zerver/lib/integrations.py
index 6264de85b91..8cb856aa831 100644
--- a/zerver/lib/integrations.py
+++ b/zerver/lib/integrations.py
@@ -882,7 +882,13 @@ INCOMING_WEBHOOK_INTEGRATIONS: list[IncomingWebhookIntegration] = [
IncomingWebhookIntegration(
"sentry", ["monitoring"], [WebhookScreenshotConfig("event_for_exception_python.json")]
),
- IncomingWebhookIntegration("slack", ["communication"]),
+ IncomingWebhookIntegration(
+ "slack",
+ ["communication"],
+ url_options=[
+ WebhookUrlOption.build_preset_config(PresetUrlOption.CHANNEL_MAPPING),
+ ],
+ ),
IncomingWebhookIntegration(
"slack_incoming",
["communication", "meta-integration"],
diff --git a/zerver/lib/webhooks/common.py b/zerver/lib/webhooks/common.py
index 30c9a45b562..f43f7940f69 100644
--- a/zerver/lib/webhooks/common.py
+++ b/zerver/lib/webhooks/common.py
@@ -95,7 +95,7 @@ class WebhookUrlOption:
label="Exclude notifications from private repositories",
validator=check_bool,
)
- case PresetUrlOption.CHANNEL_MAPPING: # nocoverage # Not used yet
+ case PresetUrlOption.CHANNEL_MAPPING:
return cls(
name=config.value,
label="",
diff --git a/zerver/webhooks/slack/doc.md b/zerver/webhooks/slack/doc.md
index ee64b91f4db..75c70d33ddb 100644
--- a/zerver/webhooks/slack/doc.md
+++ b/zerver/webhooks/slack/doc.md
@@ -28,10 +28,8 @@ If you are looking to quickly move your Slack integrations to Zulip, check out
1. {!generate-webhook-url-basic.md!}
To send messages from each Slack channel into a **matching Zulip channel**,
- enable the **Send all notifications to a single topic** option, and add
- `&channels_map_to_topics=0` to the generated URL. For each public channel in
- your Slack workspace, be sure to [create][2] a Zulip channel with the same
- name.
+ open the **Where to send notifications** dropdown and select **Matching
+ Zulip channel**.
To send messages from each Slack channel into a **matching Zulip topic**,
disable the **Send all notifications to a single topic** option when
diff --git a/zerver/webhooks/slack/tests.py b/zerver/webhooks/slack/tests.py
index 450155335f1..240768860ab 100644
--- a/zerver/webhooks/slack/tests.py
+++ b/zerver/webhooks/slack/tests.py
@@ -17,7 +17,7 @@ CHANNEL = "general"
API_CHANNEL_NAME = "Slack general"
EXPECTED_MESSAGE = "**{user}**: {message}"
TOPIC_WITH_CHANNEL = "channel: {channel}"
-
+DEFAULT_TOPIC_NAME = "Message from Slack"
LEGACY_USER = "slack_user"
ParamT = ParamSpec("ParamT")
@@ -108,6 +108,19 @@ class SlackWebhookTests(WebhookTestCase):
content_type="application/json",
)
+ @mock_slack_api_calls
+ def test_url_options_channels_mapping_true(self) -> None:
+ self.CHANNEL_NAME = API_CHANNEL_NAME
+ self.url = self.build_webhook_url(mapping="channels")
+ expected_message = EXPECTED_MESSAGE.format(user=USER, message=MESSAGE_WITH_NORMAL_TEXT)
+ expected_topic_name = DEFAULT_TOPIC_NAME
+ self.check_webhook(
+ "message_with_normal_text",
+ expected_topic_name,
+ expected_message,
+ content_type="application/json",
+ )
+
@mock_slack_api_calls
def test_slack_channels_map_to_topics_false_and_user_specified_topic(self) -> None:
self.CHANNEL_NAME = API_CHANNEL_NAME
@@ -121,6 +134,19 @@ class SlackWebhookTests(WebhookTestCase):
content_type="application/json",
)
+ @mock_slack_api_calls
+ def test_url_options_map_to_channels_and_user_specified_topic(self) -> None:
+ self.CHANNEL_NAME = API_CHANNEL_NAME
+ expected_topic_name = DEFAULT_TOPIC_NAME
+ self.url = self.build_webhook_url(topic=expected_topic_name, mapping="channels")
+ expected_message = EXPECTED_MESSAGE.format(user=USER, message=MESSAGE_WITH_NORMAL_TEXT)
+ self.check_webhook(
+ "message_with_normal_text",
+ expected_topic_name,
+ expected_message,
+ content_type="application/json",
+ )
+
@mock_slack_api_calls
def test_invalid_channels_map_to_topics(self) -> None:
payload = self.get_body("message_with_normal_text")
diff --git a/zerver/webhooks/slack/view.py b/zerver/webhooks/slack/view.py
index a5e3c018a6f..a020617ad4c 100644
--- a/zerver/webhooks/slack/view.py
+++ b/zerver/webhooks/slack/view.py
@@ -1,5 +1,5 @@
import re
-from typing import Any, TypeAlias
+from typing import Annotated, Any, TypeAlias
from django.http import HttpRequest
from django.http.response import HttpResponse
@@ -17,7 +17,7 @@ from zerver.decorator import webhook_view
from zerver.lib.exceptions import JsonableError, UnsupportedWebhookEventTypeError
from zerver.lib.request import RequestVariableMissingError
from zerver.lib.response import json_success
-from zerver.lib.typed_endpoint import typed_endpoint
+from zerver.lib.typed_endpoint import ApiParamConfig, typed_endpoint
from zerver.lib.validator import WildValue, check_none_or, check_string, to_wild_value
from zerver.lib.webhooks.common import check_send_webhook_message, get_setup_webhook_message
from zerver.models import UserProfile
@@ -184,6 +184,7 @@ def api_slack_webhook(
*,
slack_app_token: str = "",
channels_map_to_topics: str | None = None,
+ map_to_channels: Annotated[str | None, ApiParamConfig("mapping")] = None,
) -> HttpResponse:
if request.content_type != "application/json":
# Handle Slack's legacy Outgoing Webhook Service payload.
@@ -276,6 +277,12 @@ def api_slack_webhook(
)
sender = get_slack_sender_name(user_id, slack_app_token)
content = get_message_body(text, sender, files)
+
+ # channels_map_to_topics=0 is ported to use UrlPreset.CHANNELS_MAPPING
+ # (map_to_channels).
+ if map_to_channels == "channels" and channels_map_to_topics is None:
+ channels_map_to_topics = VALID_OPTIONS["SHOULD_NOT_BE_MAPPED"]
+
channel_id = event_dict.get("channel").tame(check_string)
channel = (
get_slack_channel_name(channel_id, slack_app_token) if channels_map_to_topics else None