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

Fix wrong version ordering in x-repo search #1517

Merged
merged 8 commits into from
Aug 14, 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
1 change: 1 addition & 0 deletions CHANGES/1516.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ordering by version in the ``/ansible/search/collection-versions/`` endpoint.
26 changes: 22 additions & 4 deletions pulp_ansible/app/galaxy/v3/filters.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from django.contrib.postgres.search import SearchQuery
from django.db.models import fields as db_fields
from django.db.models import fields as db_fields, Case, When, Value
from django.db.models import Q
from django.db.models.expressions import F, Func
from django_filters import (
filters,
FilterSet,
OrderingFilter,
)
import semantic_version
from rest_framework.exceptions import ValidationError
Expand All @@ -17,6 +16,26 @@
from pulp_ansible.app import models


class SemanticVersionOrderingFilter(filters.OrderingFilter):
def filter(self, qs, value):
if value is not None and any(v in ["version", "-version"] for v in value):
order = "-" if "-version" in value else ""

return qs.annotate(
prerelease=Case(
When(collection_version__version_prerelease="", then=Value(None)),
default="collection_version__version_prerelease",
),
).order_by(
f"{order}collection_version__version_major",
f"{order}collection_version__version_minor",
f"{order}collection_version__version_patch",
f"{order}prerelease",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like these happen to be alright in alphabetical order.

)

return super().filter(qs, value)


class CollectionVersionSearchFilter(FilterSet):
"""A custom filterset for cross-repo search."""

Expand Down Expand Up @@ -49,7 +68,7 @@ class CollectionVersionSearchFilter(FilterSet):
keywords = filters.CharFilter(field_name="q", method="filter_by_q")
repository_label = LabelFilter(label_field_name="repository__pulp_labels")

order_by = OrderingFilter(
order_by = SemanticVersionOrderingFilter(
choices=(
("pulp_created", "by CV created"),
("-pulp_created", "by CV created (descending)"),
Expand All @@ -64,7 +83,6 @@ class CollectionVersionSearchFilter(FilterSet):
"collection_version__pulp_created": "pulp_created",
"collection_version__namespace": "namespace",
"collection_version__name": "name",
"collection_version__version": "version",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this change doing?

Copy link
Contributor Author

@jerabekjiri jerabekjiri Aug 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, since we have a custom SemanticVersionOrderingFilter, we don't want this incorrect sorting by collection version.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, now that i can see it on the large screen...

},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1523,3 +1523,51 @@ def test_cross_repo_search_index_with_updated_namespace_metadata(
assert cv2.namespace_metadata.company == "Testing Co. redux"
assert cv2.namespace_metadata.description == "hello 2"
assert cv2.namespace_metadata.name == namespace_name


def test_cross_repo_search_semantic_version_ordering(
ansible_distro_api_client,
ansible_repo_api_client,
build_and_upload_collection,
galaxy_v3_default_search_api_client,
gen_object_with_cleanup,
):
"""Make sure collections are properly sorted using the order_by='version' parameter."""
pulp_repo = gen_object_with_cleanup(ansible_repo_api_client, {"name": str(uuid.uuid4())})

gen_object_with_cleanup(
ansible_distro_api_client,
{
"name": pulp_repo.name,
"base_path": pulp_repo.name,
"repository": pulp_repo.pulp_href,
},
)

versions = [
"2.0.0",
"1.22.2",
"1.22.1",
"1.22.1-rc",
"1.22.1-pre",
"1.22.1-dev",
"1.22.1-beta",
"1.22.1-alpha",
"1.1.0",
"1.0.1",
"1.0.0",
]

for version in versions:
build_and_upload_collection(ansible_repo=pulp_repo, config={"version": version})

resp = galaxy_v3_default_search_api_client.list(
limit=1000, order_by=["-version"], repository_name=[pulp_repo.name]
)

built_collection_versions = [col.collection_version.version for col in resp.data]

# Make sure versions are correctly sorted according to Semantic Versioning.
assert versions == sorted(versions, key=Version, reverse=True)

assert versions == built_collection_versions
jerabekjiri marked this conversation as resolved.
Show resolved Hide resolved
Loading