Repository: zulip/zulip

Issue:
Fix data import when `EMAIL_GATEWAY_BOT` setting is in use
If a user has changed the `EMAIL_GATEWAY_BOT` setting (and the bot's email address), our data import/export process into Zulip crashes with an exception (https://chat.zulip.org/#narrow/stream/31-production-help/topic/Realm.20import/near/800549).

I think it's possible that this could be fixed by just adding a bit of code  in `fetch_user_profile_cross_realm` to canonicalize the `EMAIL_GATEWAY_BOT` email address to Zulip's default value (so that all exports use the standard email address).  We should reproduce the issue and test that fix in a development environment.


Discussion:
Hello @zulip/server-misc members, this issue was labeled with the "area: export/import" label, so you may want to check it out!

<!-- areaLabelAddition -->
@zulipbot claim
Hello @akornor, you have been unassigned from this issue because you have not updated this issue or any referenced pull requests for over 14 days.

You can reclaim this issue or claim any other issue by commenting `@zulipbot claim` on that issue.

Thanks for your contributions, and hope to see you again soon!
@zulipbot claim

Resolving patch:
diff --git a/zerver/lib/export.py b/zerver/lib/export.py
--- a/zerver/lib/export.py
+++ b/zerver/lib/export.py
@@ -832,18 +832,28 @@ def fetch_user_profile_cross_realm(response: TableData, config: Config, context:
     realm = context['realm']
     response['zerver_userprofile_crossrealm'] = []
 
+    bot_name_to_default_email = {
+        "NOTIFICATION_BOT": "notification-bot@zulip.com",
+        "EMAIL_GATEWAY_BOT": "emailgateway@zulip.com",
+        "WELCOME_BOT": "welcome-bot@zulip.com",
+    }
+
     if realm.string_id == settings.SYSTEM_BOT_REALM:
         return
 
-    for bot_user in [
-            get_system_bot(settings.NOTIFICATION_BOT),
-            get_system_bot(settings.EMAIL_GATEWAY_BOT),
-            get_system_bot(settings.WELCOME_BOT),
-    ]:
-        recipient_id = Recipient.objects.get(type_id=bot_user.id, type=Recipient.PERSONAL).id
+    for bot in settings.INTERNAL_BOTS:
+        bot_name = bot["var_name"]
+        if bot_name not in bot_name_to_default_email:
+            continue
+
+        bot_email = bot["email_template"] % (settings.INTERNAL_BOT_DOMAIN,)
+        bot_default_email = bot_name_to_default_email[bot_name]
+        bot_user_id = get_system_bot(bot_email).id
+
+        recipient_id = Recipient.objects.get(type_id=bot_user_id, type=Recipient.PERSONAL).id
         response['zerver_userprofile_crossrealm'].append(dict(
-            email=bot_user.email,
-            id=bot_user.id,
+            email=bot_default_email,
+            id=bot_user_id,
             recipient_id=recipient_id,
         ))
 
diff --git a/zerver/lib/import_realm.py b/zerver/lib/import_realm.py
--- a/zerver/lib/import_realm.py
+++ b/zerver/lib/import_realm.py
@@ -825,10 +825,6 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int=1) -> Realm
     # Remap the user IDs for notification_bot and friends to their
     # appropriate IDs on this server
     for item in data['zerver_userprofile_crossrealm']:
-        if item['email'].startswith("emailgateway@"):
-            # The email gateway bot's email is customized to a
-            # different domain on some servers.
-            item['email'] = settings.EMAIL_GATEWAY_BOT
         logging.info("Adding to ID map: %s %s" % (item['id'], get_system_bot(item['email']).id))
         new_user_id = get_system_bot(item['email']).id
         update_id_map(table='user_profile', old_id=item['id'], new_id=new_user_id)


Test patch: