Skip to content

Commit

Permalink
Fix legacy role view version sorting and validation (#1953)
Browse files Browse the repository at this point in the history
* Always sort the versions at serialization time until we have real models with sortable querysets.
* Need to allow role versions with -any- numerical component.

No-Issue

Signed-off-by: James Tanner <tanner.jc@gmail.com>
  • Loading branch information
jctanner authored Oct 26, 2023
1 parent 96930a9 commit 7fe4e1c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
5 changes: 4 additions & 1 deletion galaxy_ng/app/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from galaxy_ng.app.api.v1.models import LegacyNamespace
from galaxy_ng.app.api.v1.models import LegacyRole
from galaxy_ng.app.api.v1.models import LegacyRoleDownloadCount
from galaxy_ng.app.api.v1.utils import sort_versions


class LegacyNamespacesSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -296,7 +297,8 @@ def get_summary_fields(self, obj):

versions = obj.full_metadata.get('versions', [])
if versions:
# the versions data should have been sorted at sync or import time
# FIXME - we can't assume they're all sorted yet
versions = sort_versions(versions)
versions = versions[::-1]
if len(versions) > 10:
versions = versions[:11]
Expand Down Expand Up @@ -449,6 +451,7 @@ def get_previous_link(self, obj):
def get_results(self, obj):

versions = obj.full_metadata.get('versions', [])
versions = sort_versions(versions)

results = []

Expand Down
23 changes: 3 additions & 20 deletions galaxy_ng/app/api/v1/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import tempfile
import uuid

import semantic_version

from django.db import transaction

from ansible.module_utils.compat.version import LooseVersion
Expand All @@ -24,22 +22,15 @@
from galaxy_ng.app.api.v1.models import LegacyNamespace
from galaxy_ng.app.api.v1.models import LegacyRole
from galaxy_ng.app.api.v1.models import LegacyRoleDownloadCount
from galaxy_ng.app.api.v1.utils import sort_versions
from galaxy_ng.app.api.v1.utils import parse_version_tag

from git import Repo


logger = logging.getLogger(__name__)


def parse_version_tag(value):
value = str(value)
if not value:
raise ValueError('Empty version value')
if value[0].lower() == 'v':
value = value[1:]
return semantic_version.Version(value)


def find_real_role(github_user, github_repo):
"""
Given the github_user and github_repo attributes, find a matching
Expand Down Expand Up @@ -173,14 +164,6 @@ def do_git_checkout(clone_url, checkout_path, github_reference):
return gitrepo, github_reference, last_commit


def sort_versions(versions):
"""
Use ansible-core's LooseVersion util to sort the versions.
"""
sorted_versions = sorted(versions, key=lambda x: LooseVersion(x['tag'].lower()))
return sorted_versions


def normalize_versions(versions):

# convert old integer based IDs to uuid
Expand All @@ -206,7 +189,7 @@ def normalize_versions(versions):
versions.remove(version)
continue
lver = LooseVersion(version['tag'].lower())
if not all(isinstance(x, int) for x in lver.version):
if not any(isinstance(x, int) for x in lver.version):
versions.remove(version)

return versions
Expand Down
19 changes: 19 additions & 0 deletions galaxy_ng/app/api/v1/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import semantic_version
from ansible.module_utils.compat.version import LooseVersion


def parse_version_tag(value):
value = str(value)
if not value:
raise ValueError('Empty version value')
if value[0].lower() == 'v':
value = value[1:]
return semantic_version.Version(value)


def sort_versions(versions):
"""
Use ansible-core's LooseVersion util to sort the version dicts by the tag key.
"""
sorted_versions = sorted(versions, key=lambda x: LooseVersion(x['tag'].lower()))
return sorted_versions

0 comments on commit 7fe4e1c

Please sign in to comment.