From fc50736f4e0f9c948aa1462a16669ed8c6b6da82 Mon Sep 17 00:00:00 2001 From: PieterCK Date: Mon, 12 Aug 2024 12:25:40 +0700 Subject: [PATCH] slack_data_import: Fix incorrect hyperlink conversion. Currently, Slack messages containing hyperlinks (e.g.,) are converted like normal links. This commit reformats Slack hyperlinks into Zulip-friendly markdown (e.g., [Foo!](http://foo.com)). Part of #32165. --- zerver/data_import/slack_message_conversion.py | 13 ++++++++++--- zerver/tests/fixtures/slack_message_conversion.json | 7 ++++++- zerver/tests/test_slack_message_conversion.py | 5 +++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/zerver/data_import/slack_message_conversion.py b/zerver/data_import/slack_message_conversion.py index d023e443b4..9887590523 100644 --- a/zerver/data_import/slack_message_conversion.py +++ b/zerver/data_import/slack_message_conversion.py @@ -157,13 +157,20 @@ def convert_markdown_syntax(text: str, regex: str, zulip_keyword: str) -> str: def convert_link_format(text: str) -> tuple[str, bool]: """ 1. Converts '' to 'https://foo.com' - 2. Converts '' to 'https://foo.com|foo' + 2. Converts '' to '[foo](https://foo.com)' """ has_link = False for match in re.finditer(LINK_REGEX, text, re.VERBOSE): - converted_text = match.group(0).replace(">", "").replace("<", "") + slack_url = match.group(0) + url_parts = slack_url[1:-1].split("|", maxsplit=1) + # Check if there's a pipe with text after it + if len(url_parts) == 2: + converted_url = f"[{url_parts[1]}]({url_parts[0]})" + else: + converted_url = url_parts[0] + has_link = True - text = text.replace(match.group(0), converted_text) + text = text.replace(slack_url, converted_url) return text, has_link diff --git a/zerver/tests/fixtures/slack_message_conversion.json b/zerver/tests/fixtures/slack_message_conversion.json index d5a038a5c9..7725743998 100644 --- a/zerver/tests/fixtures/slack_message_conversion.json +++ b/zerver/tests/fixtures/slack_message_conversion.json @@ -8,7 +8,12 @@ { "name": "slack_link_with_pipe", "input": ">Google logo today:\n>\n>Kinda boring", - "conversion_output": ">Google logo today:\n>https://foo.com|foo\n>Kinda boring" + "conversion_output": ">Google logo today:\n>[foo](https://foo.com)\n>Kinda boring" + }, + { + "name": "slack_link_with_pipes", + "input": ">Google logo today:\n>\n>Kinda boring", + "conversion_output": ">Google logo today:\n>[foo|oof](https://foo.com)\n>Kinda boring" }, { "name": "slack_link2", diff --git a/zerver/tests/test_slack_message_conversion.py b/zerver/tests/test_slack_message_conversion.py index f24dc857b8..40334327f3 100644 --- a/zerver/tests/test_slack_message_conversion.py +++ b/zerver/tests/test_slack_message_conversion.py @@ -113,6 +113,11 @@ class SlackMessageConversion(ZulipTestCase): self.assertEqual(text, "http://journals.plos.org/plosone/article") self.assertEqual(has_link, True) + message = "" + text, mentioned_users, has_link = convert_to_zulip_markdown(message, [], {}, slack_user_map) + self.assertEqual(text, "[Help logging in to CZO](http://chat.zulip.org/help/logging-in)") + self.assertEqual(has_link, True) + message = "" text, mentioned_users, has_link = convert_to_zulip_markdown(message, [], {}, slack_user_map) self.assertEqual(text, "mailto:foo@foo.com")