Repository: pulp/pulpcore

Issue:
0077_move_remote_url_credentials.py fails on Remotes that have @ in path, not netloc
**Version**
3.18.10

**Describe the bug**
Migration 0077 fails when you have a remote that has an @ somewhere in the path

```
  Applying core.0077_move_remote_url_credentials...Traceback (most recent call last):
  File "/usr/bin/pulpcore-manager", line 33, in <module>
    sys.exit(load_entry_point('pulpcore==3.18.10', 'console_scripts', 'pulpcore-manager')())
  File "/usr/lib/python3.9/site-packages/pulpcore/app/manage.py", line 11, in manage
    execute_from_command_line(sys.argv)
  File "/usr/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/usr/lib/python3.9/site-packages/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "/usr/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 244, in handle
    post_migrate_state = executor.migrate(
  File "/usr/lib/python3.9/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/lib/python3.9/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/lib/python3.9/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/lib/python3.9/site-packages/django/db/migrations/migration.py", line 126, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/lib/python3.9/site-packages/django/db/migrations/operations/special.py", line 190, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/usr/lib/python3.9/site-packages/pulpcore/app/migrations/0077_move_remote_url_credentials.py", line 19, in move_remote_url_credentials
    _, url_split = url.netloc.rsplit("@", maxsplit=1)
ValueError: not enough values to unpack (expected 2, got 1)
```

**To Reproduce**
Steps to reproduce the behavior:
* Have a remote `https://download.copr.fedorainfracloud.org/results/@caddy/caddy/epel-8-x86_64/`
* Try to migrate 0077

**Expected behavior**
migration aplies

**Additional context**
https://community.theforeman.org/t/foreman-3-3-katello-4-5-upgrade-failed-pulpcore-manager-migrate-noinput/31088



Discussion:

Resolving patch:
diff --git a/pulpcore/app/migrations/0077_move_remote_url_credentials.py b/pulpcore/app/migrations/0077_move_remote_url_credentials.py
--- a/pulpcore/app/migrations/0077_move_remote_url_credentials.py
+++ b/pulpcore/app/migrations/0077_move_remote_url_credentials.py
@@ -11,6 +11,11 @@ def move_remote_url_credentials(apps, schema_editor):
     for remote in Remote.objects.filter(url__contains="@").iterator():
         url = urlparse(remote.url)
 
+        if '@' not in url.netloc:
+            # URLs can have an @ in other places than the netloc,
+            # but those do not indicate credentials
+            continue
+
         if not remote.username:
             remote.username = url.username
         if not remote.password:


Test patch:
diff --git a/pulpcore/tests/unit/migrations/test_0077_move_remote_url_credentials.py b/pulpcore/tests/unit/migrations/test_0077_move_remote_url_credentials.py
--- a/pulpcore/tests/unit/migrations/test_0077_move_remote_url_credentials.py
+++ b/pulpcore/tests/unit/migrations/test_0077_move_remote_url_credentials.py
@@ -13,9 +13,13 @@ def setUp(self):
         self.remote = Remote.objects.create(
             name="test-url", url="http://elladan:lembas@rivendell.org", pulp_type="file"
         )
+        self.remote_without_credentials = Remote.objects.create(
+            name="test-url-no-credentials", url="https://download.copr.fedorainfracloud.org/results/@caddy/caddy/epel-8-x86_64/", pulp_type="file"
+        )
 
     def tearDown(self):
         self.remote.delete()
+        self.remote_without_credentials.delete()
 
     def test_move_remote_url_credentials(self):
         apps_mock = Mock()
@@ -29,3 +33,14 @@ def test_move_remote_url_credentials(self):
         self.assertEqual(self.remote.url, "http://rivendell.org")
         self.assertEqual(self.remote.username, "elladan")
         self.assertEqual(self.remote.password, "lembas")
+
+    def test_accept_remote_without_credentials_but_with_at(self):
+        apps_mock = Mock()
+        apps_mock.get_model = Mock(return_value=Remote)
+
+        # use import_module due to underscores
+        migration = import_module("pulpcore.app.migrations.0077_move_remote_url_credentials")
+        migration.move_remote_url_credentials(apps_mock, None)
+
+        self.remote_without_credentials = Remote.objects.get(name="test-url-no-credentials")
+        self.assertEqual(self.remote_without_credentials.url, "https://download.copr.fedorainfracloud.org/results/@caddy/caddy/epel-8-x86_64/")