From 876ee8c3cd956025aadda14175f80ce4cccfe1bb Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Sun, 23 Oct 2022 03:18:15 +0200 Subject: [PATCH] Allow package version sorting (#21453) --- models/packages/container/search.go | 19 +++++++++++++++++-- models/packages/package_version.go | 23 +++++++++++++++++------ options/locale/locale_en-US.ini | 4 ++++ routers/web/user/package.go | 7 ++++++- templates/admin/packages/list.tmpl | 12 ++++++------ templates/package/shared/versionlist.tmpl | 10 ++++++++-- 6 files changed, 58 insertions(+), 17 deletions(-) diff --git a/models/packages/container/search.go b/models/packages/container/search.go index a3409fe74..e4a5a5384 100644 --- a/models/packages/container/search.go +++ b/models/packages/container/search.go @@ -165,6 +165,7 @@ type ImageTagsSearchOptions struct { PackageID int64 Query string IsTagged bool + Sort packages.VersionSort db.Paginator } @@ -195,12 +196,26 @@ func (opts *ImageTagsSearchOptions) toConds() builder.Cond { return cond } +func (opts *ImageTagsSearchOptions) configureOrderBy(e db.Engine) { + switch opts.Sort { + case packages.SortVersionDesc: + e.Desc("package_version.version") + case packages.SortVersionAsc: + e.Asc("package_version.version") + case packages.SortCreatedAsc: + e.Asc("package_version.created_unix") + default: + e.Desc("package_version.created_unix") + } +} + // SearchImageTags gets a sorted list of the tags of an image func SearchImageTags(ctx context.Context, opts *ImageTagsSearchOptions) ([]*packages.PackageVersion, int64, error) { sess := db.GetEngine(ctx). Join("INNER", "package", "package.id = package_version.package_id"). - Where(opts.toConds()). - Desc("package_version.created_unix") + Where(opts.toConds()) + + opts.configureOrderBy(sess) if opts.Paginator != nil { sess = db.SetSessionPagination(sess, opts) diff --git a/models/packages/package_version.go b/models/packages/package_version.go index 5479bae1c..f9965bcb7 100644 --- a/models/packages/package_version.go +++ b/models/packages/package_version.go @@ -163,6 +163,17 @@ type SearchValue struct { ExactMatch bool } +type VersionSort = string + +const ( + SortNameAsc VersionSort = "name_asc" + SortNameDesc VersionSort = "name_desc" + SortVersionAsc VersionSort = "version_asc" + SortVersionDesc VersionSort = "version_desc" + SortCreatedAsc VersionSort = "created_asc" + SortCreatedDesc VersionSort = "created_desc" +) + // PackageSearchOptions are options for SearchXXX methods // Besides IsInternal are all fields optional and are not used if they have their default value (nil, "", 0) type PackageSearchOptions struct { @@ -176,7 +187,7 @@ type PackageSearchOptions struct { IsInternal util.OptionalBool HasFileWithName string // only results are found which are associated with a file with the specific name HasFiles util.OptionalBool // only results are found which have associated files - Sort string + Sort VersionSort db.Paginator } @@ -254,15 +265,15 @@ func (opts *PackageSearchOptions) toConds() builder.Cond { func (opts *PackageSearchOptions) configureOrderBy(e db.Engine) { switch opts.Sort { - case "alphabetically": + case SortNameAsc: e.Asc("package.name") - case "reversealphabetically": + case SortNameDesc: e.Desc("package.name") - case "highestversion": + case SortVersionDesc: e.Desc("package_version.version") - case "lowestversion": + case SortVersionAsc: e.Asc("package_version.version") - case "oldest": + case SortCreatedAsc: e.Asc("package_version.created_unix") default: e.Desc("package_version.created_unix") diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index a35c6a668..3c2e70187 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -106,6 +106,10 @@ never = Never rss_feed = RSS Feed +[filter] +string.asc = A - Z +string.desc = Z - A + [error] occurred = An error occurred report_message = If you are sure this is a Gitea bug, please search for issues on GitHub or open a new issue if necessary. diff --git a/routers/web/user/package.go b/routers/web/user/package.go index c72592e72..7179e2df9 100644 --- a/routers/web/user/package.go +++ b/routers/web/user/package.go @@ -233,6 +233,7 @@ func ListPackageVersions(ctx *context.Context) { } query := ctx.FormTrim("q") + sort := ctx.FormTrim("sort") ctx.Data["Title"] = ctx.Tr("packages.title") ctx.Data["IsPackagesPage"] = true @@ -243,9 +244,11 @@ func ListPackageVersions(ctx *context.Context) { Owner: ctx.Package.Owner, } ctx.Data["Query"] = query + ctx.Data["Sort"] = sort pagerParams := map[string]string{ - "q": query, + "q": query, + "sort": sort, } var ( @@ -264,6 +267,7 @@ func ListPackageVersions(ctx *context.Context) { PackageID: p.ID, Query: query, IsTagged: tagged == "" || tagged == "tagged", + Sort: sort, }) if err != nil { ctx.ServerError("SearchImageTags", err) @@ -278,6 +282,7 @@ func ListPackageVersions(ctx *context.Context) { Value: query, }, IsInternal: util.OptionalBoolFalse, + Sort: sort, }) if err != nil { ctx.ServerError("SearchVersions", err) diff --git a/templates/admin/packages/list.tmpl b/templates/admin/packages/list.tmpl index b62e78879..3aab2873c 100644 --- a/templates/admin/packages/list.tmpl +++ b/templates/admin/packages/list.tmpl @@ -37,20 +37,20 @@ ID {{.locale.Tr "admin.packages.owner"}} {{.locale.Tr "admin.packages.type"}} - + {{.locale.Tr "admin.packages.name"}} - {{SortArrow "alphabetically" "reversealphabetically" .SortType false}} + {{SortArrow "name_asc" "name_desc" .SortType false}} - + {{.locale.Tr "admin.packages.version"}} - {{SortArrow "highestversion" "lowestversion" .SortType false}} + {{SortArrow "version_desc" "version_asc" .SortType false}} {{.locale.Tr "admin.packages.creator"}} {{.locale.Tr "admin.packages.repository"}} {{.locale.Tr "admin.packages.size"}} - + {{.locale.Tr "admin.packages.published"}} - {{SortArrow "oldest" "newest" .SortType true}} + {{SortArrow "created_asc" "created_desc" .SortType true}} {{.locale.Tr "admin.notices.op"}} diff --git a/templates/package/shared/versionlist.tmpl b/templates/package/shared/versionlist.tmpl index 02bda3fb8..952634cfb 100644 --- a/templates/package/shared/versionlist.tmpl +++ b/templates/package/shared/versionlist.tmpl @@ -3,11 +3,17 @@
+ {{if eq .PackageDescriptor.Package.Type "container"}} {{end}}