Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
mattermost: Fix channel.name collision resolution.
Mateusz Manderab527cb4c8120
diff --git a/zerver/data_import/mattermost.py b/zerver/data_import/mattermost.pyindex cd13a3280f5..14321b84385 100644--- a/zerver/data_import/mattermost.py+++ b/zerver/data_import/mattermost.py@@ -241,9 +241,13 @@ def convert_channel_data(channel_dict["display_name"], Stream.MAX_NAME_LENGTH, "…")- if channel_display_name in channel_name_count:- channel_name_count[channel_display_name] += 1- collision = channel_name_count[channel_display_name]+ # Channel names which only differ in capitalization are a collision,+ # because we treat them case-insensitively at the database level.+ # So we use .lower() of the name for collision-detection purposes.+ channel_display_name_lower = channel_display_name.lower()+ while channel_display_name_lower in channel_name_count:+ channel_name_count[channel_display_name_lower] += 1+ collision = channel_name_count[channel_display_name_lower]count_string = f" ({collision})"channel_display_name = (@@ -252,8 +256,14 @@ def convert_channel_data()+ count_string)- else:- channel_name_count[channel_display_name] = 1+ # The new generated name could still collide with a pre-existing+ # imported channel. E.g. channels ["Foo (2)", "foo", "Foo"] will+ # have a collision here while processing the "Foo" name. This+ # should be very rare, so we opt for an approach that keeps this+ # code simple over prioritizing prettiness of the generated name -+ # so "Foo" will be transformed into "Foo (2) (2)" in this rare case.+ channel_display_name_lower = channel_display_name.lower()+ channel_name_count[channel_display_name_lower] = 1realm["zerver_stream"].append(build_stream(diff --git a/zerver/tests/fixtures/mattermost_fixtures/export.json b/zerver/tests/fixtures/mattermost_fixtures/export.jsonindex 31c4cea88c6..0a03f2bab1e 100644--- a/zerver/tests/fixtures/mattermost_fixtures/export.json+++ b/zerver/tests/fixtures/mattermost_fixtures/export.json@@ -10,6 +10,10 @@{"type":"channel","channel":{"team":"gryffindor","name":"long-channel-name-2","display_name":"Super long channel name, it's more than 60 characters, which is Zulip's channel name length limit.","type":"O","header":"","purpose":""}}{"type":"channel","channel":{"team":"gryffindor","name":"other-long-channel-name","display_name":"Super long channel name, it's more than 60 characters, which if truncated looks similar to unrelated channels","type":"O","header":"","purpose":""}}{"type":"channel","channel":{"team":"gryffindor","name":"gryffindor-quidditch-team-2","display_name":"Gryffindor quidditch team","type":"O","header":"","purpose":"Another place for talking about Gryffindor quidditch team"}}+{"type":"channel","channel":{"team":"gryffindor","name":"slug-club","display_name":"Slug club","type":"O","header":"","purpose":"A place for talking about the Slug club"}}+{"type":"channel","channel":{"team":"gryffindor","name":"slug-club-2","display_name":"SLUG CLUB","type":"O","header":"","purpose":"Another place for talking about the Slug club"}}+{"type":"channel","channel":{"team":"gryffindor","name":"slug-club-3","display_name":"Slug club (3)","type":"O","header":"","purpose":"A third place for talking about the Slug club"}}+{"type":"channel","channel":{"team":"gryffindor","name":"slug-club-4","display_name":"slug club","type":"O","header":"","purpose":"A fourth place for talking about the Slug club"}}{"type":"user","user":{"username":"ron","email":"[email protected]","auth_service":"","nickname":"","first_name":"Ron","last_name":"Weasley","position":"","roles":"system_user","locale":"en","teams":[{"name":"gryffindor","roles":"team_user","channels":[{"name":"gryffindor-quidditch-team","roles":"channel_user","notify_props":{"desktop":"default","mobile":"default","mark_unread":"all"},"favorite":false},{"name":"gryffindor-common-room","roles":"channel_user","notify_props":{"desktop":"default","mobile":"default","mark_unread":"all"},"favorite":false},{"name":"dumbledores-army","roles":"channel_user","notify_props":{"desktop":"default","mobile":"default","mark_unread":"all"},"favorite":false}]}],"notify_props":{"desktop":"mention","desktop_sound":"true","email":"true","mobile":"mention","mobile_push_status":"away","channel":"true","comments":"never","mention_keys":"ron,@ron"}}}{"type":"user","user":{"username":"harry","email":"[email protected]","auth_service":"","nickname":"","first_name":"Harry","last_name":"Potter","position":"","roles":"system_admin system_user","locale":"en","teams":[{"name":"gryffindor","roles":"team_admin team_user","channels":[{"name":"dumbledores-army","roles":"channel_admin channel_user","notify_props":{"desktop":"default","mobile":"default","mark_unread":"all"},"favorite":false},{"name":"gryffindor-common-room","roles":"channel_admin channel_user","notify_props":{"desktop":"default","mobile":"default","mark_unread":"all"},"favorite":false},{"name":"gryffindor-quidditch-team","roles":"channel_admin channel_user","notify_props":{"desktop":"default","mobile":"default","mark_unread":"all"},"favorite":false}]}],"notify_props":{"desktop":"mention","desktop_sound":"true","email":"true","mobile":"mention","mobile_push_status":"away","channel":"true","comments":"never","mention_keys":"harry,@harry"}}}{"type":"user","user":{"username":"malfoy","email":"[email protected]","auth_service":"","nickname":"","first_name":"","last_name":"","position":"","roles":"system_user","locale":"en","teams":[{"name":"slytherin","roles":"team_admin team_user","channels":[{"name":"slytherin-common-room","roles":"channel_admin channel_user","notify_props":{"desktop":"default","mobile":"default","mark_unread":"all"},"favorite":false},{"name":"slytherin-quidditch-team","roles":"channel_admin channel_user","notify_props":{"desktop":"default","mobile":"default","mark_unread":"all"},"favorite":false}]}],"notify_props":{"desktop":"mention","desktop_sound":"true","email":"true","mobile":"mention","mobile_push_status":"away","channel":"true","comments":"never","mention_keys":"malfoy,@malfoy"}}}diff --git a/zerver/tests/test_mattermost_importer.py b/zerver/tests/test_mattermost_importer.pyindex 5dfa3ef6df5..01f139cf624 100644--- a/zerver/tests/test_mattermost_importer.py+++ b/zerver/tests/test_mattermost_importer.py@@ -366,7 +366,7 @@ class MatterMostImporter(MattermostImportTestBase):self.assert_length(mattermost_data["team"], 2)self.assertEqual(mattermost_data["team"][0]["name"], "gryffindor")- self.assert_length(mattermost_data["channel"], 9)+ self.assert_length(mattermost_data["channel"], 13)self.assertEqual(mattermost_data["channel"][0]["name"], "gryffindor-common-room")self.assertEqual(mattermost_data["channel"][0]["team"], "gryffindor")@@ -612,7 +612,7 @@ class MatterMostImporter(MattermostImportTestBase):team_name=team_name,)zerver_stream = mock_realm_dict["zerver_stream"]- self.assert_length(zerver_stream, 7)+ self.assert_length(zerver_stream, 11)self.assertEqual(zerver_stream[0]["name"], "Gryffindor common room")self.assertEqual(zerver_stream[0]["invite_only"], False)@@ -658,6 +658,17 @@ class MatterMostImporter(MattermostImportTestBase):)self.assertGreaterEqual(Stream.MAX_NAME_LENGTH, len(zerver_stream[5]["name"]))+ # Channel names which differ only in capitalization need collision resolution,+ # because Zulip treats them case-insensitively.+ self.assertEqual(zerver_stream[7]["name"], "Slug club")+ self.assertEqual(zerver_stream[8]["name"], "SLUG CLUB (2)")+ # "Slug club (3)" is imported as an original channel name from Mattermost.+ # Collision resolution for the "slug club" channel then generates the same name,+ # which therefore needs another round of collision resolution itself, resulting+ # in a doubly suffixed name.+ self.assertEqual(zerver_stream[9]["name"], "Slug club (3)")+ self.assertEqual(zerver_stream[10]["name"], "slug club (3) (2)")+self.assertTrue(stream_id_mapper.has("gryffindor-common-room"))self.assertTrue(stream_id_mapper.has("gryffindor-quidditch-team"))self.assertTrue(stream_id_mapper.has("dumbledores-army"))@@ -1120,7 +1131,7 @@ class MatterMostImporter(MattermostImportTestBase):)- self.assert_length(realm["zerver_stream"], 7)+ self.assert_length(realm["zerver_stream"], 11)exported_stream_names = self.get_set(realm["zerver_stream"], "name")self.assertSetEqual(exported_stream_names,@@ -1132,6 +1143,10 @@ class MatterMostImporter(MattermostImportTestBase):"Super long channel name, it's more than 60 characters, … (2)","Super long channel name, it's more than 60 characters, … (3)","Gryffindor quidditch team (2)",+ "Slug club",+ "SLUG CLUB (2)",+ "Slug club (3)",+ "slug club (3) (2)",},)self.assertEqual(@@ -1142,11 +1157,11 @@ class MatterMostImporter(MattermostImportTestBase):self.assert_length(realm["zerver_defaultstream"], 0)exported_recipient_ids = self.get_set(realm["zerver_recipient"], "id")- self.assert_length(exported_recipient_ids, 7)+ self.assert_length(exported_recipient_ids, 11)exported_recipient_types = self.get_set(realm["zerver_recipient"], "type")self.assertEqual(exported_recipient_types, {Recipient.STREAM})exported_recipient_type_ids = self.get_set(realm["zerver_recipient"], "type_id")- self.assert_length(exported_recipient_type_ids, 7)+ self.assert_length(exported_recipient_type_ids, 11)exported_subscription_userprofile = self.get_set(realm["zerver_subscription"], "user_profile"