From b63e11a8b77607401bb282602fd7c99370ee0b60 Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Tue, 24 Dec 2024 08:33:11 -0500 Subject: [PATCH] Version: better support for manual created latest and stable versions (#11823) Closes https://github.com/readthedocs/readthedocs.org/issues/11773 --- readthedocs/builds/models.py | 32 ++++++++----------- readthedocs/rtd_tests/tests/test_version.py | 18 ++++++++++- .../tests/test_version_commit_name.py | 22 +++++++++++++ 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/readthedocs/builds/models.py b/readthedocs/builds/models.py index ec5988a6ea3..25e576c630a 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, @@ -260,7 +259,12 @@ def external_version_name(self): @property def ref(self): - if self.slug == STABLE: + """ + The version slug where the ``stable`` version points to. + + It returns None when the version is not stable (machine created). + """ + if self.slug == STABLE and self.machine: stable = determine_stable_version( self.project.versions(manager=INTERNAL).all() ) @@ -270,15 +274,12 @@ def ref(self): @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: + stable_version = self.project.get_original_stable_version() + if stable_version: + version_name = stable_version.verbose_name + elif self.slug == LATEST and self.machine: + version_name = self.project.get_default_branch() return get_vcs_url( project=self.project, @@ -338,10 +339,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 @@ -352,11 +353,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..1970066eaa8 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,24 @@ 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): + self.pip.update_stable_version() + self.branch_version.refresh_from_db() 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(