Skip to content

Commit dcc6867

Browse files
committed
fix SearchVersions
1 parent d130dd6 commit dcc6867

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

models/packages/package_version.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/util"
1515

1616
"xorm.io/builder"
17+
"xorm.io/xorm"
1718
)
1819

1920
// ErrDuplicatePackageVersion indicates a duplicated package version error
@@ -187,7 +188,7 @@ type PackageSearchOptions struct {
187188
HasFileWithName string // only results are found which are associated with a file with the specific name
188189
HasFiles optional.Option[bool] // only results are found which have associated files
189190
Sort VersionSort
190-
db.Paginator
191+
Paginator db.Paginator
191192
}
192193

193194
func (opts *PackageSearchOptions) ToConds() builder.Cond {
@@ -282,25 +283,28 @@ func (opts *PackageSearchOptions) configureOrderBy(e db.Engine) {
282283
e.Desc("package_version.id") // Sort by id for stable order with duplicates in the other field
283284
}
284285

285-
// SearchVersions gets all versions of packages matching the search options
286-
func SearchVersions(ctx context.Context, opts *PackageSearchOptions) ([]*PackageVersion, int64, error) {
287-
sess := db.GetEngine(ctx).
288-
Select("package_version.*").
289-
Table("package_version").
290-
Join("INNER", "package", "package.id = package_version.package_id").
291-
Where(opts.ToConds())
292-
286+
func searchVersionsBySession(sess *xorm.Session, opts *PackageSearchOptions) ([]*PackageVersion, int64, error) {
293287
opts.configureOrderBy(sess)
294288
pvs := make([]*PackageVersion, 0, 10)
295289
if opts.Paginator != nil {
296-
sess = db.SetSessionPagination(sess, opts)
290+
sess = db.SetSessionPagination(sess, opts.Paginator)
297291
count, err := sess.FindAndCount(&pvs)
298292
return pvs, count, err
299293
}
300294
err := sess.Find(&pvs)
301295
return pvs, int64(len(pvs)), err
302296
}
303297

298+
// SearchVersions gets all versions of packages matching the search options
299+
func SearchVersions(ctx context.Context, opts *PackageSearchOptions) ([]*PackageVersion, int64, error) {
300+
sess := db.GetEngine(ctx).
301+
Select("package_version.*").
302+
Table("package_version").
303+
Join("INNER", "package", "package.id = package_version.package_id").
304+
Where(opts.ToConds())
305+
return searchVersionsBySession(sess, opts)
306+
}
307+
304308
// SearchLatestVersions gets the latest version of every package matching the search options
305309
func SearchLatestVersions(ctx context.Context, opts *PackageSearchOptions) ([]*PackageVersion, int64, error) {
306310
in := builder.
@@ -316,15 +320,7 @@ func SearchLatestVersions(ctx context.Context, opts *PackageSearchOptions) ([]*P
316320
Join("INNER", "package", "package.id = package_version.package_id").
317321
Where(builder.In("package_version.id", in))
318322

319-
opts.configureOrderBy(sess)
320-
321-
if opts.Paginator != nil {
322-
sess = db.SetSessionPagination(sess, opts)
323-
}
324-
325-
pvs := make([]*PackageVersion, 0, 10)
326-
count, err := sess.FindAndCount(&pvs)
327-
return pvs, count, err
323+
return searchVersionsBySession(sess, opts)
328324
}
329325

330326
// ExistVersion checks if a version matching the search options exist

0 commit comments

Comments
 (0)