From dbdb5ba33eeecf788650e6c1c675abc602d836b5 Mon Sep 17 00:00:00 2001 From: Samuel FORESTIER Date: Sun, 19 Mar 2023 14:19:37 +0000 Subject: [PATCH] Sort Python package descriptors by version to mimic PyPI format (#23550) --- Hi, very naive and **untested** first time ever Go code, feel free to reject/edit this as needed. (PyPI actually performs "naive" string comparison too) --------- Co-authored-by: Lauris BH --- routers/api/packages/pypi/pypi.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/routers/api/packages/pypi/pypi.go b/routers/api/packages/pypi/pypi.go index 609c63dc6..2f71801a8 100644 --- a/routers/api/packages/pypi/pypi.go +++ b/routers/api/packages/pypi/pypi.go @@ -8,6 +8,7 @@ import ( "io" "net/http" "regexp" + "sort" "strings" packages_model "code.gitea.io/gitea/models/packages" @@ -62,6 +63,11 @@ func PackageMetadata(ctx *context.Context) { return } + // sort package descriptors by version to mimic PyPI format + sort.Slice(pds, func(i, j int) bool { + return strings.Compare(pds[i].Version.Version, pds[j].Version.Version) < 0 + }) + ctx.Data["RegistryURL"] = setting.AppURL + "api/packages/" + ctx.Package.Owner.Name + "/pypi" ctx.Data["PackageDescriptor"] = pds[0] ctx.Data["PackageDescriptors"] = pds