aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2023-08-14 04:50:55 +0200
committerGitHub <noreply@github.com>2023-08-14 02:50:55 +0000
commitecd51f710b7b08eddc952d518f0d097367221388 (patch)
treeefef4d3efadc0480af58b270b6c93a9aecfdc421 /models
parent56b6b2b88ef175cc18d2ccdb86c220e885a24262 (diff)
downloadgitea-ecd51f710b7b08eddc952d518f0d097367221388.tar.gz
gitea-ecd51f710b7b08eddc952d518f0d097367221388.zip
Fix NuGet search endpoints (#25613)
Fixes #25564 Fixes #23191 - Api v2 search endpoint should return only the latest version matching the query - Api v3 search endpoint should return `take` packages not package versions
Diffstat (limited to 'models')
-rw-r--r--models/db/common.go16
-rw-r--r--models/packages/nuget/search.go70
-rw-r--r--models/packages/package_version.go10
3 files changed, 91 insertions, 5 deletions
diff --git a/models/db/common.go b/models/db/common.go
index 2a5043a8e7..ea628bf2a0 100644
--- a/models/db/common.go
+++ b/models/db/common.go
@@ -37,3 +37,19 @@ func BuildCaseInsensitiveIn(key string, values []string) builder.Cond {
return builder.In("UPPER("+key+")", uppers)
}
+
+// BuilderDialect returns the xorm.Builder dialect of the engine
+func BuilderDialect() string {
+ switch {
+ case setting.Database.Type.IsMySQL():
+ return builder.MYSQL
+ case setting.Database.Type.IsSQLite3():
+ return builder.SQLITE
+ case setting.Database.Type.IsPostgreSQL():
+ return builder.POSTGRES
+ case setting.Database.Type.IsMSSQL():
+ return builder.MSSQL
+ default:
+ return ""
+ }
+}
diff --git a/models/packages/nuget/search.go b/models/packages/nuget/search.go
new file mode 100644
index 0000000000..53cdf2d4ad
--- /dev/null
+++ b/models/packages/nuget/search.go
@@ -0,0 +1,70 @@
+// Copyright 2023 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package nuget
+
+import (
+ "context"
+ "strings"
+
+ "code.gitea.io/gitea/models/db"
+ packages_model "code.gitea.io/gitea/models/packages"
+
+ "xorm.io/builder"
+)
+
+// SearchVersions gets all versions of packages matching the search options
+func SearchVersions(ctx context.Context, opts *packages_model.PackageSearchOptions) ([]*packages_model.PackageVersion, int64, error) {
+ cond := toConds(opts)
+
+ e := db.GetEngine(ctx)
+
+ total, err := e.
+ Where(cond).
+ Count(&packages_model.Package{})
+ if err != nil {
+ return nil, 0, err
+ }
+
+ inner := builder.
+ Dialect(db.BuilderDialect()). // builder needs the sql dialect to build the Limit() below
+ Select("*").
+ From("package").
+ Where(cond).
+ OrderBy("package.name ASC")
+ if opts.Paginator != nil {
+ skip, take := opts.GetSkipTake()
+ inner = inner.Limit(take, skip)
+ }
+
+ sess := e.
+ Where(opts.ToConds()).
+ Table("package_version").
+ Join("INNER", inner, "package.id = package_version.package_id")
+
+ pvs := make([]*packages_model.PackageVersion, 0, 10)
+ return pvs, total, sess.Find(&pvs)
+}
+
+// CountPackages counts all packages matching the search options
+func CountPackages(ctx context.Context, opts *packages_model.PackageSearchOptions) (int64, error) {
+ return db.GetEngine(ctx).
+ Where(toConds(opts)).
+ Count(&packages_model.Package{})
+}
+
+func toConds(opts *packages_model.PackageSearchOptions) builder.Cond {
+ var cond builder.Cond = builder.Eq{
+ "package.is_internal": opts.IsInternal.IsTrue(),
+ "package.owner_id": opts.OwnerID,
+ "package.type": packages_model.TypeNuGet,
+ }
+ if opts.Name.Value != "" {
+ if opts.Name.ExactMatch {
+ cond = cond.And(builder.Eq{"package.lower_name": strings.ToLower(opts.Name.Value)})
+ } else {
+ cond = cond.And(builder.Like{"package.lower_name", strings.ToLower(opts.Name.Value)})
+ }
+ }
+ return cond
+}
diff --git a/models/packages/package_version.go b/models/packages/package_version.go
index ab1bcddae5..4392c70f79 100644
--- a/models/packages/package_version.go
+++ b/models/packages/package_version.go
@@ -189,7 +189,7 @@ type PackageSearchOptions struct {
db.Paginator
}
-func (opts *PackageSearchOptions) toConds() builder.Cond {
+func (opts *PackageSearchOptions) ToConds() builder.Cond {
cond := builder.NewCond()
if !opts.IsInternal.IsNone() {
cond = builder.Eq{
@@ -283,7 +283,7 @@ func (opts *PackageSearchOptions) configureOrderBy(e db.Engine) {
// SearchVersions gets all versions of packages matching the search options
func SearchVersions(ctx context.Context, opts *PackageSearchOptions) ([]*PackageVersion, int64, error) {
sess := db.GetEngine(ctx).
- Where(opts.toConds()).
+ Where(opts.ToConds()).
Table("package_version").
Join("INNER", "package", "package.id = package_version.package_id")
@@ -300,7 +300,7 @@ func SearchVersions(ctx context.Context, opts *PackageSearchOptions) ([]*Package
// SearchLatestVersions gets the latest version of every package matching the search options
func SearchLatestVersions(ctx context.Context, opts *PackageSearchOptions) ([]*PackageVersion, int64, error) {
- cond := opts.toConds().
+ cond := opts.ToConds().
And(builder.Expr("pv2.id IS NULL"))
joinCond := builder.Expr("package_version.package_id = pv2.package_id AND (package_version.created_unix < pv2.created_unix OR (package_version.created_unix = pv2.created_unix AND package_version.id < pv2.id))")
@@ -328,7 +328,7 @@ func SearchLatestVersions(ctx context.Context, opts *PackageSearchOptions) ([]*P
// ExistVersion checks if a version matching the search options exist
func ExistVersion(ctx context.Context, opts *PackageSearchOptions) (bool, error) {
return db.GetEngine(ctx).
- Where(opts.toConds()).
+ Where(opts.ToConds()).
Table("package_version").
Join("INNER", "package", "package.id = package_version.package_id").
Exist(new(PackageVersion))
@@ -337,7 +337,7 @@ func ExistVersion(ctx context.Context, opts *PackageSearchOptions) (bool, error)
// CountVersions counts all versions of packages matching the search options
func CountVersions(ctx context.Context, opts *PackageSearchOptions) (int64, error) {
return db.GetEngine(ctx).
- Where(opts.toConds()).
+ Where(opts.ToConds()).
Table("package_version").
Join("INNER", "package", "package.id = package_version.package_id").
Count(new(PackageVersion))