From 1c318652c1ba451c5ff6d69763d1c110c4a14bcc Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Wed, 4 Dec 2024 13:36:56 -0500 Subject: [PATCH] Version: better support for manual created latest and stable versions Closes https://github.com/readthedocs/readthedocs.org/issues/11773 --- readthedocs/builds/models.py | 27 ++++++------------- readthedocs/rtd_tests/tests/test_version.py | 16 ++++++++++- .../tests/test_version_commit_name.py | 22 +++++++++++++++ 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/readthedocs/builds/models.py b/readthedocs/builds/models.py index ffc34333781..5e038d88c2d 100644 --- a/readthedocs/builds/models.py +++ b/readthedocs/builds/models.py @@ -28,7 +28,6 @@ EXTERNAL_VERSION_STATES, INTERNAL, LATEST, - NON_REPOSITORY_VERSIONS, PREDEFINED_MATCH_ARGS, PREDEFINED_MATCH_ARGS_VALUES, STABLE, @@ -263,25 +262,20 @@ def external_version_name(self): @property def ref(self): - if self.slug == STABLE: + if self.slug == STABLE and self.machine: stable = determine_stable_version( self.project.versions(manager=INTERNAL).all() ) if stable: - return stable.slug + return stable.verbose_name @property def vcs_url(self): version_name = self.verbose_name - if not self.is_external: - if self.slug == STABLE: - version_name = self.ref - elif self.slug == LATEST: - version_name = self.project.get_default_branch() - else: - version_name = self.slug - if "bitbucket" in self.project.repo: - version_name = self.identifier + if self.slug == STABLE and self.machine: + version_name = self.ref + elif self.slug == LATEST and self.machine: + version_name = self.project.get_default_branch() return get_vcs_url( project=self.project, @@ -341,10 +335,10 @@ def commit_name(self): """ # LATEST is special as it is usually a branch but does not contain the # name in verbose_name. - if self.slug == LATEST: + if self.slug == LATEST and self.machine: return self.project.get_default_branch() - if self.slug == STABLE: + if self.slug == STABLE and self.machine: if self.type == BRANCH: # Special case, as we do not store the original branch name # that the stable version works on. We can only interpolate the @@ -355,11 +349,6 @@ def commit_name(self): return self.identifier[len("origin/") :] return self.identifier - # By now we must have handled all special versions. - if self.slug in NON_REPOSITORY_VERSIONS: - # pylint: disable=broad-exception-raised - raise Exception("All special versions must be handled by now.") - if self.type in (BRANCH, TAG): # If this version is a branch or a tag, the verbose_name will # contain the actual name. We cannot use identifier as this might diff --git a/readthedocs/rtd_tests/tests/test_version.py b/readthedocs/rtd_tests/tests/test_version.py index 8b3f1dab096..bafc2076b1d 100644 --- a/readthedocs/rtd_tests/tests/test_version.py +++ b/readthedocs/rtd_tests/tests/test_version.py @@ -2,7 +2,7 @@ from django.test.utils import override_settings from django_dynamic_fixture import get -from readthedocs.builds.constants import BRANCH, EXTERNAL, LATEST, TAG +from readthedocs.builds.constants import BRANCH, EXTERNAL, LATEST, STABLE, TAG from readthedocs.builds.models import Version from readthedocs.projects.models import Project @@ -31,6 +31,7 @@ def setUp(self): project=self.pip, active=True, type=BRANCH, + machine=True, ) self.tag_version = get( Version, @@ -40,6 +41,7 @@ def setUp(self): project=self.pip, active=True, type=TAG, + machine=True, ) self.subproject = get(Project, slug="subproject", language="en") @@ -74,10 +76,22 @@ def test_vcs_url_for_latest_version(self): expected_url = f"https://github.com/pypa/pip/tree/{slug}/" self.assertEqual(self.tag_version.vcs_url, expected_url) + def test_vcs_url_for_manual_latest_version(self): + latest = self.pip.versions.get(slug=LATEST) + latest.machine = False + latest.save() + assert "https://github.com/pypa/pip/tree/latest/" == latest.vcs_url + def test_vcs_url_for_stable_version(self): expected_url = f"https://github.com/pypa/pip/tree/{self.branch_version.ref}/" self.assertEqual(self.branch_version.vcs_url, expected_url) + def test_vcs_url_for_manual_stable_version(self): + stable = self.pip.versions.get(slug=STABLE) + stable.machine = False + stable.save() + assert "https://github.com/pypa/pip/tree/stable/" == stable.vcs_url + def test_commit_name_for_stable_version(self): self.assertEqual(self.branch_version.commit_name, "stable") diff --git a/readthedocs/rtd_tests/tests/test_version_commit_name.py b/readthedocs/rtd_tests/tests/test_version_commit_name.py index 3c99dd31b2c..a4dfd4debdc 100644 --- a/readthedocs/rtd_tests/tests/test_version_commit_name.py +++ b/readthedocs/rtd_tests/tests/test_version_commit_name.py @@ -62,12 +62,26 @@ def test_stable_version_tag(self): slug=STABLE, verbose_name=STABLE, type=TAG, + machine=True, ) self.assertEqual( version.commit_name, "3d92b728b7d7b842259ac2020c2fa389f13aff0d", ) + def test_manual_stable_version(self): + project = get(Project) + version = get( + Version, + project=project, + identifier="stable", + slug=STABLE, + verbose_name=STABLE, + type=BRANCH, + machine=False, + ) + self.assertEqual(version.commit_name, "stable") + def test_git_latest_branch(self): git_project = get(Project, repo_type=REPO_TYPE_GIT) version = new( @@ -77,9 +91,17 @@ def test_git_latest_branch(self): slug=LATEST, verbose_name=LATEST, type=BRANCH, + machine=True, ) self.assertEqual(version.commit_name, "master") + def test_manual_latest_version(self): + project = get(Project) + version = project.versions.get(slug=LATEST) + version.machine = False + version.save() + self.assertEqual(version.commit_name, "latest") + def test_external_version(self): identifier = "ec26de721c3235aad62de7213c562f8c821" version = new(