Skip to content

Versions: keep type of version in sync with the project #10606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions readthedocs/api/v2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
log = structlog.get_logger(__name__)


def sync_versions_to_db(project, versions, type): # pylint: disable=redefined-builtin
def sync_versions_to_db(project, versions, type):
"""
Update the database with the current versions from the repository.

Expand Down Expand Up @@ -117,6 +117,8 @@ def sync_versions_to_db(project, versions, type): # pylint: disable=redefined-b
latest_version.machine = True
latest_version.identifier = project.get_default_branch()
latest_version.verbose_name = LATEST_VERBOSE_NAME
# The machine created latest version always points to a branch.
latest_version.type = BRANCH
latest_version.save()
if added:
log.info(
Expand Down Expand Up @@ -156,7 +158,7 @@ def _create_versions(project, type, versions):

def _set_or_create_version(project, slug, version_id, verbose_name, type_):
"""Search or create a version and set its machine attribute to false."""
version = (project.versions.filter(slug=slug).first())
version = project.versions.filter(slug=slug).first()
if version:
version.identifier = version_id
version.machine = False
Expand Down
22 changes: 14 additions & 8 deletions readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,12 @@ def get_original_stable_version(self):
"""
Get the original version that stable points to.

Returns None if the current stable doesn't point to a valid version.
When stable is machine created, it's basically an alias
for the latest stable version (like 2.2),
that version is the "original" one.

Returns None if the current stable doesn't point to a valid version
or if isn't machine created.
"""
current_stable = self.get_stable_version()
if not current_stable or not current_stable.machine:
Expand Down Expand Up @@ -1199,18 +1204,19 @@ def update_stable_version(self):
new_stable = determine_stable_version(versions)
if new_stable:
if current_stable:
identifier_updated = (
version_updated = (
new_stable.identifier != current_stable.identifier
or new_stable.type != current_stable.type
)
if identifier_updated:
if version_updated:
log.info(
'Update stable version: %(project)s:%(version)s',
{
'project': self.slug,
'version': new_stable.identifier,
}
"Stable version updated.",
project_slug=self.slug,
version_identifier=new_stable.identifier,
version_type=new_stable.type,
)
current_stable.identifier = new_stable.identifier
current_stable.type = new_stable.type
current_stable.save()
return new_stable
else:
Expand Down
88 changes: 88 additions & 0 deletions readthedocs/rtd_tests/tests/test_sync_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,94 @@ def test_delete_version(self):
Version.objects.filter(slug='external').exists(),
)

def test_update_stable_version_type(self):
self.pip.update_stable_version()
stable_version = self.pip.get_stable_version()
self.assertEqual(stable_version.type, TAG)

branches_data = [
{
"identifier": "master",
"verbose_name": "master",
},
{
"identifier": "1.0",
"verbose_name": "1.0",
},
{
"identifier": "1.1",
"verbose_name": "1.1",
},
{
"identifier": "2.0",
"verbose_name": "2.0",
},
]

# Deactivate all other versions, so we only have branches for consideration
# for the new stable version.
self.pip.versions.exclude(slug__in=[LATEST, STABLE]).update(active=False)
sync_versions_task(
self.pip.pk,
branches_data=branches_data,
tags_data=[],
)

self.pip.update_stable_version()
stable_version = self.pip.get_stable_version()
self.assertEqual(stable_version.type, BRANCH)
self.assertEqual(stable_version.identifier, "2.0")
self.assertEqual(stable_version.verbose_name, "stable")

original_stable = self.pip.get_original_stable_version()
self.assertEqual(original_stable.type, BRANCH)
self.assertEqual(original_stable.slug, "2.0")
self.assertEqual(original_stable.identifier, "2.0")
self.assertEqual(original_stable.verbose_name, "2.0")

def test_update_latest_version_type(self):
latest_version = self.pip.versions.get(slug=LATEST)
self.assertEqual(latest_version.type, BRANCH)

branches_data = [
{
"identifier": "master",
"verbose_name": "master",
},
]
tags_data = [
{
"identifier": "abc123",
"verbose_name": "latest",
}
]

# Latest is created as machine=False, and as a tag.
sync_versions_task(
self.pip.pk,
branches_data=branches_data,
tags_data=tags_data,
)

latest_version = self.pip.versions.get(slug=LATEST)
self.assertEqual(latest_version.type, TAG)
self.assertEqual(latest_version.identifier, "abc123")
self.assertEqual(latest_version.verbose_name, "latest")
self.assertEqual(latest_version.machine, False)

# Latest is back as machine created, and as a branch.
sync_versions_task(
self.pip.pk,
branches_data=branches_data,
tags_data=[],
)

latest_version = self.pip.versions.get(slug=LATEST)
self.assertEqual(latest_version.type, BRANCH)
self.assertEqual(latest_version.identifier, "master")
self.assertEqual(latest_version.verbose_name, "latest")
self.assertEqual(latest_version.machine, True)

def test_machine_attr_when_user_define_stable_tag_and_delete_it(self):
"""
The user creates a tag named ``stable`` on an existing repo,
Expand Down