We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN 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).

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `zerver/management/commands/rename_stream.py`
Content:
```
1 from argparse import ArgumentParser
2 from typing import Any
3 
4 from zerver.actions.streams import do_rename_stream
5 from zerver.lib.management import ZulipBaseCommand
6 from zerver.models import get_stream
7 
8 
9 class Command(ZulipBaseCommand):
10     help = """Change the stream name for a realm."""
11 
12     def add_arguments(self, parser: ArgumentParser) -> None:
13         parser.add_argument("old_name", metavar="<old name>", help="name of stream to be renamed")
14         parser.add_argument(
15             "new_name", metavar="<new name>", help="new name to rename the stream to"
16         )
17         self.add_realm_args(parser, required=True)
18 
19     def handle(self, *args: Any, **options: str) -> None:
20         realm = self.get_realm(options)
21         assert realm is not None  # Should be ensured by parser
22         old_name = options["old_name"]
23         new_name = options["new_name"]
24 
25         stream = get_stream(old_name, realm)
26         do_rename_stream(stream, new_name, self.user_profile)
27
```

Path: `zilencer/management/commands/migrate_stream_notifications.py`
Content:
```
1 from typing import Any
2 
3 from django.core.management.base import BaseCommand
4 
5 from zerver.models import Subscription
6 
7 
8 class Command(BaseCommand):
9     help = """One-off script to migration users' stream notification settings."""
10 
11     def handle(self, *args: Any, **options: Any) -> None:
12         for subscription in Subscription.objects.all():
13             subscription.desktop_notifications = subscription.notifications
14             subscription.audible_notifications = subscription.notifications
15             subscription.save(update_fields=["desktop_notifications", "audible_notifications"])
16
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

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"])

{"golden_diff": "diff --git a/zerver/management/commands/rename_stream.py b/zerver/management/commands/rename_stream.py\ndeleted file mode 100644\n--- a/zerver/management/commands/rename_stream.py\n+++ /dev/null\n@@ -1,26 +0,0 @@\n-from argparse import ArgumentParser\n-from typing import Any\n-\n-from zerver.actions.streams import do_rename_stream\n-from zerver.lib.management import ZulipBaseCommand\n-from zerver.models import get_stream\n-\n-\n-class Command(ZulipBaseCommand):\n-    help = \"\"\"Change the stream name for a realm.\"\"\"\n-\n-    def add_arguments(self, parser: ArgumentParser) -> None:\n-        parser.add_argument(\"old_name\", metavar=\"<old name>\", help=\"name of stream to be renamed\")\n-        parser.add_argument(\n-            \"new_name\", metavar=\"<new name>\", help=\"new name to rename the stream to\"\n-        )\n-        self.add_realm_args(parser, required=True)\n-\n-    def handle(self, *args: Any, **options: str) -> None:\n-        realm = self.get_realm(options)\n-        assert realm is not None  # Should be ensured by parser\n-        old_name = options[\"old_name\"]\n-        new_name = options[\"new_name\"]\n-\n-        stream = get_stream(old_name, realm)\n-        do_rename_stream(stream, new_name, self.user_profile)\ndiff --git a/zilencer/management/commands/migrate_stream_notifications.py b/zilencer/management/commands/migrate_stream_notifications.py\ndeleted file mode 100644\n--- a/zilencer/management/commands/migrate_stream_notifications.py\n+++ /dev/null\n@@ -1,15 +0,0 @@\n-from typing import Any\n-\n-from django.core.management.base import BaseCommand\n-\n-from zerver.models import Subscription\n-\n-\n-class Command(BaseCommand):\n-    help = \"\"\"One-off script to migration users' stream notification settings.\"\"\"\n-\n-    def handle(self, *args: Any, **options: Any) -> None:\n-        for subscription in Subscription.objects.all():\n-            subscription.desktop_notifications = subscription.notifications\n-            subscription.audible_notifications = subscription.notifications\n-            subscription.save(update_fields=[\"desktop_notifications\", \"audible_notifications\"])\n", "issue": "management: `rename_stream` management command does not work\n`rename_stream` uses the `do_rename_stream` function to rename the stream. However, it accesses a non-existent attribute when calling it.\r\n\r\n```\r\ndo_rename_stream(stream, new_name, self.user_profile)  # self.user_profile does not exist\r\n```\r\n\r\nTo replicate this, run:\r\n```\r\npython manage.py rename_stream Denmark bar -r zulip\r\n```\r\nand you should see:\r\n```\r\nAttributeError: 'Command' object has no attribute 'user_profile'\r\n```\r\nYou might want to look at `zerver/management/commands/rename_stream.py` and `zerver/actions/streams.py`.\r\n\r\nThe 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).\n", "before_files": [{"content": "from argparse import ArgumentParser\nfrom typing import Any\n\nfrom zerver.actions.streams import do_rename_stream\nfrom zerver.lib.management import ZulipBaseCommand\nfrom zerver.models import get_stream\n\n\nclass Command(ZulipBaseCommand):\n    help = \"\"\"Change the stream name for a realm.\"\"\"\n\n    def add_arguments(self, parser: ArgumentParser) -> None:\n        parser.add_argument(\"old_name\", metavar=\"<old name>\", help=\"name of stream to be renamed\")\n        parser.add_argument(\n            \"new_name\", metavar=\"<new name>\", help=\"new name to rename the stream to\"\n        )\n        self.add_realm_args(parser, required=True)\n\n    def handle(self, *args: Any, **options: str) -> None:\n        realm = self.get_realm(options)\n        assert realm is not None  # Should be ensured by parser\n        old_name = options[\"old_name\"]\n        new_name = options[\"new_name\"]\n\n        stream = get_stream(old_name, realm)\n        do_rename_stream(stream, new_name, self.user_profile)\n", "path": "zerver/management/commands/rename_stream.py"}, {"content": "from typing import Any\n\nfrom django.core.management.base import BaseCommand\n\nfrom zerver.models import Subscription\n\n\nclass Command(BaseCommand):\n    help = \"\"\"One-off script to migration users' stream notification settings.\"\"\"\n\n    def handle(self, *args: Any, **options: Any) -> None:\n        for subscription in Subscription.objects.all():\n            subscription.desktop_notifications = subscription.notifications\n            subscription.audible_notifications = subscription.notifications\n            subscription.save(update_fields=[\"desktop_notifications\", \"audible_notifications\"])\n", "path": "zilencer/management/commands/migrate_stream_notifications.py"}], "after_files": [{"content": null, "path": "zerver/management/commands/rename_stream.py"}, {"content": null, "path": "zilencer/management/commands/migrate_stream_notifications.py"}]}