From 547fa1b6ce1e7c8965aa72ae5cbc3ad2296eba6b Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Wed, 14 Dec 2022 10:16:01 +0100 Subject: [PATCH] Fix condition for is_internal (#22095) depends on #22094 Fixes https://codeberg.org/forgejo/forgejo/issues/77 The old logic did not consider `is_internal`. Co-authored-by: Lunny Xiao Co-authored-by: techknowlogick --- models/packages/package_version.go | 7 ++++++- routers/api/packages/npm/npm.go | 5 +++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/models/packages/package_version.go b/models/packages/package_version.go index f9965bcb7489e..c68bef0bfa31b 100644 --- a/models/packages/package_version.go +++ b/models/packages/package_version.go @@ -303,9 +303,14 @@ func SearchLatestVersions(ctx context.Context, opts *PackageSearchOptions) ([]*P 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))") + if !opts.IsInternal.IsNone() { + joinCond = joinCond.And(builder.Eq{"pv2.is_internal": opts.IsInternal.IsTrue()}) + } + sess := db.GetEngine(ctx). Table("package_version"). - Join("LEFT", "package_version pv2", "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))"). + Join("LEFT", "package_version pv2", joinCond). Join("INNER", "package", "package.id = package_version.package_id"). Where(cond) diff --git a/routers/api/packages/npm/npm.go b/routers/api/packages/npm/npm.go index a9676b9191031..502d353ffc15b 100644 --- a/routers/api/packages/npm/npm.go +++ b/routers/api/packages/npm/npm.go @@ -402,8 +402,9 @@ func setPackageTag(tag string, pv *packages_model.PackageVersion, deleteOnly boo func PackageSearch(ctx *context.Context) { pvs, total, err := packages_model.SearchLatestVersions(ctx, &packages_model.PackageSearchOptions{ - OwnerID: ctx.Package.Owner.ID, - Type: packages_model.TypeNpm, + OwnerID: ctx.Package.Owner.ID, + Type: packages_model.TypeNpm, + IsInternal: util.OptionalBoolFalse, Name: packages_model.SearchValue{ ExactMatch: false, Value: ctx.FormTrim("text"),