Repository: readthedocs/readthedocs.org

Issue:
Setting default branch uses Version.slug instead of identifier
## Details

* Read the Docs project URL: https://readthedocs.org/projects/test-builds/
* Build URL (if applicable): https://readthedocs.org/projects/test-builds/builds/8319150/
* Read the Docs username (if applicable): humitos

## Expected Result

Build the branch set as default branch as when it's built as [non default](https://readthedocs.org/projects/test-builds/builds/8307296/).

> NOTE: the correct command here is `git checkout --force origin/branch/with/slashes`

## Actual Result

The `git checkout --force branch-with-slashes` command fails because the branch does not exist.

----

`Project.default_branch` is saved in an improperly way.

```
>>> Version.objects.get(slug='branch-with-slashes').__dict__
...
 'type': 'branch',
 'identifier': 'origin/branch/with/slashes',
 'verbose_name': 'branch/with/slashes',
 'slug': 'branch-with-slashes',
>>> Version.objects.get(slug='branch-with-slashes').project.default_branch                                                                                                               
'branch-with-slashes'
```



Discussion:

Resolving patch:
diff --git a/readthedocs/projects/forms.py b/readthedocs/projects/forms.py
--- a/readthedocs/projects/forms.py
+++ b/readthedocs/projects/forms.py
@@ -222,7 +222,7 @@ def __init__(self, *args, **kwargs):
 
         default_choice = (None, '-' * 9)
         all_versions = self.instance.versions.values_list(
-            'slug', 'verbose_name'
+            'identifier', 'verbose_name'
         )
         self.fields['default_branch'].widget = forms.Select(
             choices=[default_choice] + list(all_versions)


Test patch:
diff --git a/readthedocs/rtd_tests/tests/test_project_forms.py b/readthedocs/rtd_tests/tests/test_project_forms.py
--- a/readthedocs/rtd_tests/tests/test_project_forms.py
+++ b/readthedocs/rtd_tests/tests/test_project_forms.py
@@ -174,6 +174,7 @@ def setUp(self):
             slug='public-1',
             active=True,
             privacy_level=PUBLIC,
+            identifier='public-1',
         )
         get(
             Version,
@@ -181,6 +182,7 @@ def setUp(self):
             slug='public-2',
             active=True,
             privacy_level=PUBLIC,
+            identifier='public-2',
         )
         get(
             Version,
@@ -188,6 +190,15 @@ def setUp(self):
             slug='public-3',
             active=False,
             privacy_level=PROTECTED,
+            identifier='public-3',
+        )
+        get(
+            Version,
+            project=self.project,
+            slug='public-4',
+            active=False,
+            privacy_level=PUBLIC,
+            identifier='public/4'
         )
         get(
             Version,
@@ -195,6 +206,7 @@ def setUp(self):
             slug='private',
             active=True,
             privacy_level=PRIVATE,
+            identifier='private',
         )
         get(
             Version,
@@ -202,6 +214,7 @@ def setUp(self):
             slug='protected',
             active=True,
             privacy_level=PROTECTED,
+            identifier='protected',
         )
 
     def test_list_only_active_versions_on_default_version(self):
@@ -222,12 +235,12 @@ def test_list_all_versions_on_default_branch(self):
         self.assertTrue(self.project.versions.filter(slug=LATEST).exists())
         self.assertEqual(
             set(
-                slug
-                for slug, _ in form.fields['default_branch'].widget.choices
+                identifier
+                for identifier, _ in form.fields['default_branch'].widget.choices
             ),
             {
-                None, 'latest', 'public-1', 'public-2',
-                'public-3', 'protected', 'private'
+                None, 'master', 'public-1', 'public-2',
+                'public-3', 'public/4', 'protected', 'private'
             },
         )