Skip to content

Commit b63e11a

Browse files
authored
Version: better support for manual created latest and stable versions (#11823)
Closes #11773
1 parent efb15b4 commit b63e11a

File tree

3 files changed

+53
-19
lines changed

3 files changed

+53
-19
lines changed

readthedocs/builds/models.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
EXTERNAL_VERSION_STATES,
2929
INTERNAL,
3030
LATEST,
31-
NON_REPOSITORY_VERSIONS,
3231
PREDEFINED_MATCH_ARGS,
3332
PREDEFINED_MATCH_ARGS_VALUES,
3433
STABLE,
@@ -260,7 +259,12 @@ def external_version_name(self):
260259

261260
@property
262261
def ref(self):
263-
if self.slug == STABLE:
262+
"""
263+
The version slug where the ``stable`` version points to.
264+
265+
It returns None when the version is not stable (machine created).
266+
"""
267+
if self.slug == STABLE and self.machine:
264268
stable = determine_stable_version(
265269
self.project.versions(manager=INTERNAL).all()
266270
)
@@ -270,15 +274,12 @@ def ref(self):
270274
@property
271275
def vcs_url(self):
272276
version_name = self.verbose_name
273-
if not self.is_external:
274-
if self.slug == STABLE:
275-
version_name = self.ref
276-
elif self.slug == LATEST:
277-
version_name = self.project.get_default_branch()
278-
else:
279-
version_name = self.slug
280-
if "bitbucket" in self.project.repo:
281-
version_name = self.identifier
277+
if self.slug == STABLE and self.machine:
278+
stable_version = self.project.get_original_stable_version()
279+
if stable_version:
280+
version_name = stable_version.verbose_name
281+
elif self.slug == LATEST and self.machine:
282+
version_name = self.project.get_default_branch()
282283

283284
return get_vcs_url(
284285
project=self.project,
@@ -338,10 +339,10 @@ def commit_name(self):
338339
"""
339340
# LATEST is special as it is usually a branch but does not contain the
340341
# name in verbose_name.
341-
if self.slug == LATEST:
342+
if self.slug == LATEST and self.machine:
342343
return self.project.get_default_branch()
343344

344-
if self.slug == STABLE:
345+
if self.slug == STABLE and self.machine:
345346
if self.type == BRANCH:
346347
# Special case, as we do not store the original branch name
347348
# that the stable version works on. We can only interpolate the
@@ -352,11 +353,6 @@ def commit_name(self):
352353
return self.identifier[len("origin/") :]
353354
return self.identifier
354355

355-
# By now we must have handled all special versions.
356-
if self.slug in NON_REPOSITORY_VERSIONS:
357-
# pylint: disable=broad-exception-raised
358-
raise Exception("All special versions must be handled by now.")
359-
360356
if self.type in (BRANCH, TAG):
361357
# If this version is a branch or a tag, the verbose_name will
362358
# contain the actual name. We cannot use identifier as this might

readthedocs/rtd_tests/tests/test_version.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.test.utils import override_settings
33
from django_dynamic_fixture import get
44

5-
from readthedocs.builds.constants import BRANCH, EXTERNAL, LATEST, TAG
5+
from readthedocs.builds.constants import BRANCH, EXTERNAL, LATEST, STABLE, TAG
66
from readthedocs.builds.models import Version
77
from readthedocs.projects.models import Project
88

@@ -31,6 +31,7 @@ def setUp(self):
3131
project=self.pip,
3232
active=True,
3333
type=BRANCH,
34+
machine=True,
3435
)
3536
self.tag_version = get(
3637
Version,
@@ -40,6 +41,7 @@ def setUp(self):
4041
project=self.pip,
4142
active=True,
4243
type=TAG,
44+
machine=True,
4345
)
4446

4547
self.subproject = get(Project, slug="subproject", language="en")
@@ -74,10 +76,24 @@ def test_vcs_url_for_latest_version(self):
7476
expected_url = f"https://github.com/pypa/pip/tree/{slug}/"
7577
self.assertEqual(self.tag_version.vcs_url, expected_url)
7678

79+
def test_vcs_url_for_manual_latest_version(self):
80+
latest = self.pip.versions.get(slug=LATEST)
81+
latest.machine = False
82+
latest.save()
83+
assert "https://github.com/pypa/pip/tree/latest/" == latest.vcs_url
84+
7785
def test_vcs_url_for_stable_version(self):
86+
self.pip.update_stable_version()
87+
self.branch_version.refresh_from_db()
7888
expected_url = f"https://github.com/pypa/pip/tree/{self.branch_version.ref}/"
7989
self.assertEqual(self.branch_version.vcs_url, expected_url)
8090

91+
def test_vcs_url_for_manual_stable_version(self):
92+
stable = self.pip.versions.get(slug=STABLE)
93+
stable.machine = False
94+
stable.save()
95+
assert "https://github.com/pypa/pip/tree/stable/" == stable.vcs_url
96+
8197
def test_commit_name_for_stable_version(self):
8298
self.assertEqual(self.branch_version.commit_name, "stable")
8399

readthedocs/rtd_tests/tests/test_version_commit_name.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,26 @@ def test_stable_version_tag(self):
6262
slug=STABLE,
6363
verbose_name=STABLE,
6464
type=TAG,
65+
machine=True,
6566
)
6667
self.assertEqual(
6768
version.commit_name,
6869
"3d92b728b7d7b842259ac2020c2fa389f13aff0d",
6970
)
7071

72+
def test_manual_stable_version(self):
73+
project = get(Project)
74+
version = get(
75+
Version,
76+
project=project,
77+
identifier="stable",
78+
slug=STABLE,
79+
verbose_name=STABLE,
80+
type=BRANCH,
81+
machine=False,
82+
)
83+
self.assertEqual(version.commit_name, "stable")
84+
7185
def test_git_latest_branch(self):
7286
git_project = get(Project, repo_type=REPO_TYPE_GIT)
7387
version = new(
@@ -77,9 +91,17 @@ def test_git_latest_branch(self):
7791
slug=LATEST,
7892
verbose_name=LATEST,
7993
type=BRANCH,
94+
machine=True,
8095
)
8196
self.assertEqual(version.commit_name, "master")
8297

98+
def test_manual_latest_version(self):
99+
project = get(Project)
100+
version = project.versions.get(slug=LATEST)
101+
version.machine = False
102+
version.save()
103+
self.assertEqual(version.commit_name, "latest")
104+
83105
def test_external_version(self):
84106
identifier = "ec26de721c3235aad62de7213c562f8c821"
85107
version = new(

0 commit comments

Comments
 (0)