Skip to content

Commit

Permalink
Version: better support for manual created latest and stable versions
Browse files Browse the repository at this point in the history
Closes #11773
  • Loading branch information
stsewd committed Dec 4, 2024
1 parent 8774e68 commit 1c31865
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
27 changes: 8 additions & 19 deletions readthedocs/builds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
EXTERNAL_VERSION_STATES,
INTERNAL,
LATEST,
NON_REPOSITORY_VERSIONS,
PREDEFINED_MATCH_ARGS,
PREDEFINED_MATCH_ARGS_VALUES,
STABLE,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 15 additions & 1 deletion readthedocs/rtd_tests/tests/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -31,6 +31,7 @@ def setUp(self):
project=self.pip,
active=True,
type=BRANCH,
machine=True,
)
self.tag_version = get(
Version,
Expand All @@ -40,6 +41,7 @@ def setUp(self):
project=self.pip,
active=True,
type=TAG,
machine=True,
)

self.subproject = get(Project, slug="subproject", language="en")
Expand Down Expand Up @@ -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")

Expand Down
22 changes: 22 additions & 0 deletions readthedocs/rtd_tests/tests/test_version_commit_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 1c31865

Please sign in to comment.