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.

importer/slack: Reset per-import global state before each conversion.

PieterCKd808943cf4c1
diff --git a/zerver/data_import/slack.py b/zerver/data_import/slack.py
index 956e9d23b93..ea1ebf22273 100644
--- a/zerver/data_import/slack.py
+++ b/zerver/data_import/slack.py
@@ -929,6 +929,10 @@ def get_messages_iterator(
thread_parent_map: dict[str, str] = {}
+def reset_import_state() -> None:
+ thread_parent_map.clear()
+
+
def get_parent_user_id_from_thread_message(thread_message: ZerverFieldsT, subtype: str) -> str:
"""
This retrieves the user id of the sender of the original thread
@@ -1794,6 +1798,10 @@ def do_convert_directory(
processes: int = 6,
convert_slack_threads: bool = False,
) -> None:
+ # Start from a clean slate: the module-level id maps below persist across
+ # Slack imports in a long-lived worker, so reset them before touching
+ # anything.
+ reset_import_state()
check_slack_token_access(token, SLACK_IMPORT_TOKEN_SCOPES)
os.makedirs(output_dir, exist_ok=True)
diff --git a/zerver/tests/test_slack_importer.py b/zerver/tests/test_slack_importer.py
index 317cbb0f32a..b0b74f5122f 100644
--- a/zerver/tests/test_slack_importer.py
+++ b/zerver/tests/test_slack_importer.py
@@ -67,6 +67,7 @@ from zerver.data_import.slack import (
process_message_files,
slack_emoji_name_to_codepoint,
slack_workspace_to_realm,
+ thread_parent_map,
users_to_zerver_userprofile,
)
from zerver.lib.exceptions import SlackImportInvalidFileError
@@ -2346,11 +2347,17 @@ To Do
status=200,
)
+ # A conversion must not read thread state cached by an earlier
+ # import in the same process; see reset_import_state.
+ thread_parent_map["0000000000.000000"] = "USTALEPARENT"
+
with self.assertLogs(level="INFO"), self.settings(EXTERNAL_HOST="zulip.example.com"):
# We need to mock EXTERNAL_HOST to be a valid domain because Slack's importer
# uses it to generate email addresses for users without an email specified.
do_convert_zipfile(test_slack_zip_file, output_dir, token, processes=1)
+ self.assertNotIn("0000000000.000000", thread_parent_map)
+
realm_id = 0
uploads_folder = os.path.join(output_dir, "uploads", str(realm_id))
self.assertTrue(os.path.exists(output_dir))