Repository: zulip/zulip

Issue:
management: `rename_stream` management command does not work
`rename_stream` uses the `do_rename_stream` function to rename the stream. However, it accesses a non-existent attribute when calling it.

```
do_rename_stream(stream, new_name, self.user_profile)  # self.user_profile does not exist
```

To replicate this, run:
```
python manage.py rename_stream Denmark bar -r zulip
```
and you should see:
```
AttributeError: 'Command' object has no attribute 'user_profile'
```
You might want to look at `zerver/management/commands/rename_stream.py` and `zerver/actions/streams.py`.

The fix should refactor `do_rename_stream` to accept `user_profile: Optional[UserProfile]` with the `None` default, and correctly handle what should happen for the notification message that might be sent when the stream is renamed (which currently mentions the name of the acting user that renames it).


Discussion:
@zulipbot add "help wanted" "area: tooling" "bug"
Hello @zulip/server-tooling members, this issue was labeled with the "area: tooling" label, so you may want to check it out!

<!-- areaLabelAddition -->

Looks like this came in during the work to add the message, in 4dcccf32f842eddf0238f5a252ca23c682f69324 / #11197.
There's also potentially an argument for removing this management command entirely, since it's been broken for 3.5 years and nobody has noticed, and it's trivial to do via the UI.

Resolving patch:
diff --git a/zerver/management/commands/rename_stream.py b/zerver/management/commands/rename_stream.py
deleted file mode 100644
--- a/zerver/management/commands/rename_stream.py
+++ /dev/null
@@ -1,26 +0,0 @@
-from argparse import ArgumentParser
-from typing import Any
-
-from zerver.actions.streams import do_rename_stream
-from zerver.lib.management import ZulipBaseCommand
-from zerver.models import get_stream
-
-
-class Command(ZulipBaseCommand):
-    help = """Change the stream name for a realm."""
-
-    def add_arguments(self, parser: ArgumentParser) -> None:
-        parser.add_argument("old_name", metavar="<old name>", help="name of stream to be renamed")
-        parser.add_argument(
-            "new_name", metavar="<new name>", help="new name to rename the stream to"
-        )
-        self.add_realm_args(parser, required=True)
-
-    def handle(self, *args: Any, **options: str) -> None:
-        realm = self.get_realm(options)
-        assert realm is not None  # Should be ensured by parser
-        old_name = options["old_name"]
-        new_name = options["new_name"]
-
-        stream = get_stream(old_name, realm)
-        do_rename_stream(stream, new_name, self.user_profile)
diff --git a/zilencer/management/commands/migrate_stream_notifications.py b/zilencer/management/commands/migrate_stream_notifications.py
deleted file mode 100644
--- a/zilencer/management/commands/migrate_stream_notifications.py
+++ /dev/null
@@ -1,15 +0,0 @@
-from typing import Any
-
-from django.core.management.base import BaseCommand
-
-from zerver.models import Subscription
-
-
-class Command(BaseCommand):
-    help = """One-off script to migration users' stream notification settings."""
-
-    def handle(self, *args: Any, **options: Any) -> None:
-        for subscription in Subscription.objects.all():
-            subscription.desktop_notifications = subscription.notifications
-            subscription.audible_notifications = subscription.notifications
-            subscription.save(update_fields=["desktop_notifications", "audible_notifications"])


Test patch: