Skip to content

Commit 9d203a9

Browse files
committed
Versions: keep type of version in sync with the project
So, there are a couple of problems here: - Versions have been created with the type set as unknown, and where never updated to the correct type. I was able to track down this to projects created before 2018, so probably an old bug allowed this to happen. - Stable machine versions are basically an alias for the latest stable version of the project, and they can be a branch or a tag, this means that if this version is a branch, so will be the "stable" version, same for tags. Currently, we aren't updating the version type, if the stable version was created as a branch, it will remain as a branch, even if the latest stable version is a tag, and vice versa. - Latest machine versions are basically an alias for the default branch of the project, they are always branches. Since we allow to have non-machine latest versions, they can be a branch or a tag. If this version was changed back to be a non-machine version, currently it will remain as branch or tag, this was incorrect. Our new clone/checkout pattern relies on the type of the version always being correct, so it was failing for some projects. - Fixes #10600 - Fixes #10601 After deploy, we need to clean up the invalid versions (versions with "unknown" type). We can do that by: - Updating all latest machine versions to be branches. ```python Version.objects.filter(type="unknown", machine=True).update(type="branch") ``` - Re-evaluate the stable version of all projects that have a machine stable version, so they can have the correct type. ``` for project in Project.objects.filter(versions__machine=True).distinct(): project.update_stable_version() ``` - Check all remaining active versions with "unknown" type, and update them to be branches or tags (324). We can use a simple regex to see the version identifier looks like a commit. ```python vesions = Version.objects.filter(type="unknown", active=True) commit_regex = re.compile(r"^([a-f0-9]{40})|([a-f0-9]{7})$") for version in versions: if commit_regex.match(version.identifier): version.type = "tag" else: version.type = "branch" version.save() ```
1 parent 713bdbd commit 9d203a9

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

readthedocs/api/v2/utils.py

+2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ def sync_versions_to_db(project, versions, type): # pylint: disable=redefined-b
117117
latest_version.machine = True
118118
latest_version.identifier = project.get_default_branch()
119119
latest_version.verbose_name = LATEST_VERBOSE_NAME
120+
# The machine created latest version always points to a branch.
121+
latest_version.type = BRANCH
120122
latest_version.save()
121123
if added:
122124
log.info(

readthedocs/projects/models.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1199,18 +1199,19 @@ def update_stable_version(self):
11991199
new_stable = determine_stable_version(versions)
12001200
if new_stable:
12011201
if current_stable:
1202-
identifier_updated = (
1202+
version_updated = (
12031203
new_stable.identifier != current_stable.identifier
1204+
or new_stable.type != current_stable.type
12041205
)
1205-
if identifier_updated:
1206+
if version_updated:
12061207
log.info(
1207-
'Update stable version: %(project)s:%(version)s',
1208-
{
1209-
'project': self.slug,
1210-
'version': new_stable.identifier,
1211-
}
1208+
"Stable version updated.",
1209+
project_slug=self.slug,
1210+
version_identifier=new_stable.identifier,
1211+
version_type=new_stable.type,
12121212
)
12131213
current_stable.identifier = new_stable.identifier
1214+
current_stable.type = new_stable.type
12141215
current_stable.save()
12151216
return new_stable
12161217
else:

0 commit comments

Comments
 (0)