Skip to content
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

Changed branches and tags data from Dict to DataClass #10936

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 26 additions & 40 deletions readthedocs/api/v2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def sync_versions_to_db(project, versions, type):
:returns: set of versions' slug added
"""
old_version_values = project.versions.filter(type=type).values_list(
'verbose_name',
'identifier',
"verbose_name",
"identifier",
)
old_versions = dict(old_version_values)

Expand All @@ -48,8 +48,8 @@ def sync_versions_to_db(project, versions, type):
has_user_stable = False
has_user_latest = False
for version in versions:
version_id = version['identifier']
version_name = version['verbose_name']
version_id = version.identifier
version_name = version.verbose_name
if version_name == STABLE_VERBOSE_NAME:
has_user_stable = True
created_version, created = _set_or_create_version(
Expand Down Expand Up @@ -90,7 +90,7 @@ def sync_versions_to_db(project, versions, type):
)

log.info(
'Re-syncing versions: version updated.',
"Re-syncing versions: version updated.",
version_verbose_name=version_name,
version_id=version_id,
)
Expand All @@ -101,26 +101,22 @@ def sync_versions_to_db(project, versions, type):
added.update(_create_versions(project, type, versions_to_create))

if not has_user_stable:
stable_version = (
project.versions.filter(slug=STABLE, type=type).first()
)
stable_version = project.versions.filter(slug=STABLE, type=type).first()
if stable_version:
# Put back the RTD's stable version
stable_version.machine = True
stable_version.save()
if not has_user_latest:
latest_version = (
project.versions.filter(slug=LATEST, type=type).first()
)
latest_version = project.versions.filter(slug=LATEST, type=type).first()
if latest_version:
# Put back the RTD's latest version
latest_version.machine = True
latest_version.save()
if added:
log.info(
'Re-syncing versions: versions added.',
"Re-syncing versions: versions added.",
count=len(added),
versions=','.join(itertools.islice(added, 100)),
versions=",".join(itertools.islice(added, 100)),
)
return added

Expand Down Expand Up @@ -174,14 +170,8 @@ def _set_or_create_version(project, slug, version_id, verbose_name, type_):
def _get_deleted_versions_qs(project, tags_data, branches_data):
# We use verbose_name for tags
# because several tags can point to the same identifier.
versions_tags = [
version['verbose_name']
for version in tags_data
]
versions_branches = [
version['identifier']
for version in branches_data
]
versions_tags = [version.verbose_name for version in tags_data]
versions_branches = [version.identifier for version in branches_data]

to_delete_qs = (
project.versions(manager=INTERNAL)
Expand All @@ -206,32 +196,28 @@ def delete_versions_from_db(project, tags_data, branches_data):

:returns: The slug of the deleted versions from the database.
"""
to_delete_qs = (
_get_deleted_versions_qs(
project=project,
tags_data=tags_data,
branches_data=branches_data,
)
.exclude(active=True)
)
to_delete_qs = _get_deleted_versions_qs(
project=project,
tags_data=tags_data,
branches_data=branches_data,
).exclude(active=True)
_, deleted = to_delete_qs.delete()
versions_count = deleted.get('builds.Version', 0)
versions_count = deleted.get("builds.Version", 0)
log.info(
'Re-syncing versions: versions deleted.', project_slug=project.slug, count=versions_count,
"Re-syncing versions: versions deleted.",
project_slug=project.slug,
count=versions_count,
)


def get_deleted_active_versions(project, tags_data, branches_data):
"""Return the slug of active versions that were deleted from the repository."""
to_delete_qs = (
_get_deleted_versions_qs(
project=project,
tags_data=tags_data,
branches_data=branches_data,
)
.filter(active=True)
)
return set(to_delete_qs.values_list('slug', flat=True))
to_delete_qs = _get_deleted_versions_qs(
project=project,
tags_data=tags_data,
branches_data=branches_data,
).filter(active=True)
return set(to_delete_qs.values_list("slug", flat=True))


def run_automation_rules(project, added_versions, deleted_active_versions):
Expand Down
32 changes: 13 additions & 19 deletions readthedocs/projects/tasks/mixins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import Counter
from dataclasses import dataclass

import structlog

Expand All @@ -12,6 +13,12 @@
log = structlog.get_logger(__name__)


@dataclass
class VersionData:
identifier: str
verbose_name: str


class SyncRepositoryMixin:

"""Mixin that handles the VCS sync/update."""
Expand Down Expand Up @@ -50,6 +57,7 @@ def sync_versions(self, vcs_repository):
)
tags = []
branches = []

if vcs_repository.supports_lsremote:
branches, tags = vcs_repository.lsremote(
include_tags=sync_tags,
Expand All @@ -64,21 +72,9 @@ def sync_versions(self, vcs_repository):
if sync_branches:
branches = vcs_repository.branches

tags_data = [
{
"identifier": v.identifier,
"verbose_name": v.verbose_name,
}
for v in tags
]

branches_data = [
{
"identifier": v.identifier,
"verbose_name": v.verbose_name,
}
for v in branches
]
tags_data = [VersionData(v.identifer, v.verbose_name) for v in tags]

branches_data = [VersionData(v.identifer, v.verbose_name) for v in branches]

log.debug("Synchronizing versions.", branches=branches, tags=tags)

Expand All @@ -101,11 +97,9 @@ def validate_duplicate_reserved_versions(self, tags_data, branches_data):
``latest`` or ``stable``. Raise a RepositoryError exception
if there is a duplicated name.

:param data: Dict containing the versions from tags and branches
:param data: DataClass containing the versions from tags and branches
"""
version_names = [
version["verbose_name"] for version in tags_data + branches_data
]
version_names = [version.verbose_name for version in tags_data + branches_data]
counter = Counter(version_names)
for reserved_name in [STABLE_VERBOSE_NAME, LATEST_VERBOSE_NAME]:
if counter[reserved_name] > 1:
Expand Down