2022-03-30 14:12:47 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-30 14:12:47 +05:30
|
|
|
|
|
|
|
package packages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2024-03-02 21:12:31 +05:30
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2022-03-30 14:12:47 +05:30
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2022-04-06 07:02:09 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2022-03-30 14:12:47 +05:30
|
|
|
|
|
|
|
"xorm.io/builder"
|
|
|
|
)
|
|
|
|
|
2022-04-06 07:02:09 +05:30
|
|
|
// ErrDuplicatePackageVersion indicates a duplicated package version error
|
2022-12-31 17:19:37 +05:30
|
|
|
var ErrDuplicatePackageVersion = util.NewAlreadyExistErrorf("package version already exists")
|
2022-03-30 14:12:47 +05:30
|
|
|
|
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(PackageVersion))
|
|
|
|
}
|
|
|
|
|
|
|
|
// PackageVersion represents a package version
|
|
|
|
type PackageVersion struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
PackageID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
|
|
|
|
CreatorID int64 `xorm:"NOT NULL DEFAULT 0"`
|
|
|
|
Version string `xorm:"NOT NULL"`
|
|
|
|
LowerVersion string `xorm:"UNIQUE(s) INDEX NOT NULL"`
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"created INDEX NOT NULL"`
|
|
|
|
IsInternal bool `xorm:"INDEX NOT NULL DEFAULT false"`
|
2022-11-03 12:58:46 +05:30
|
|
|
MetadataJSON string `xorm:"metadata_json LONGTEXT"`
|
2022-03-30 14:12:47 +05:30
|
|
|
DownloadCount int64 `xorm:"NOT NULL DEFAULT 0"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrInsertVersion inserts a version. If the same version exist already ErrDuplicatePackageVersion is returned
|
|
|
|
func GetOrInsertVersion(ctx context.Context, pv *PackageVersion) (*PackageVersion, error) {
|
|
|
|
e := db.GetEngine(ctx)
|
|
|
|
|
2024-01-19 17:07:10 +05:30
|
|
|
existing := &PackageVersion{}
|
2022-03-30 14:12:47 +05:30
|
|
|
|
2024-01-19 17:07:10 +05:30
|
|
|
has, err := e.Where(builder.Eq{
|
|
|
|
"package_id": pv.PackageID,
|
|
|
|
"lower_version": pv.LowerVersion,
|
|
|
|
}).Get(existing)
|
2022-03-30 14:12:47 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if has {
|
2024-01-19 17:07:10 +05:30
|
|
|
return existing, ErrDuplicatePackageVersion
|
2022-03-30 14:12:47 +05:30
|
|
|
}
|
|
|
|
if _, err = e.Insert(pv); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateVersion updates a version
|
|
|
|
func UpdateVersion(ctx context.Context, pv *PackageVersion) error {
|
|
|
|
_, err := db.GetEngine(ctx).ID(pv.ID).Update(pv)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// IncrementDownloadCounter increments the download counter of a version
|
|
|
|
func IncrementDownloadCounter(ctx context.Context, versionID int64) error {
|
|
|
|
_, err := db.GetEngine(ctx).Exec("UPDATE `package_version` SET `download_count` = `download_count` + 1 WHERE `id` = ?", versionID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetVersionByID gets a version by id
|
|
|
|
func GetVersionByID(ctx context.Context, versionID int64) (*PackageVersion, error) {
|
|
|
|
pv := &PackageVersion{}
|
|
|
|
|
|
|
|
has, err := db.GetEngine(ctx).ID(versionID).Get(pv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
return nil, ErrPackageNotExist
|
|
|
|
}
|
|
|
|
return pv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetVersionByNameAndVersion gets a version by name and version number
|
|
|
|
func GetVersionByNameAndVersion(ctx context.Context, ownerID int64, packageType Type, name, version string) (*PackageVersion, error) {
|
|
|
|
return getVersionByNameAndVersion(ctx, ownerID, packageType, name, version, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInternalVersionByNameAndVersion gets a version by name and version number
|
|
|
|
func GetInternalVersionByNameAndVersion(ctx context.Context, ownerID int64, packageType Type, name, version string) (*PackageVersion, error) {
|
|
|
|
return getVersionByNameAndVersion(ctx, ownerID, packageType, name, version, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getVersionByNameAndVersion(ctx context.Context, ownerID int64, packageType Type, name, version string, isInternal bool) (*PackageVersion, error) {
|
2022-04-06 07:02:09 +05:30
|
|
|
pvs, _, err := SearchVersions(ctx, &PackageSearchOptions{
|
|
|
|
OwnerID: ownerID,
|
|
|
|
Type: packageType,
|
|
|
|
Name: SearchValue{
|
|
|
|
ExactMatch: true,
|
|
|
|
Value: name,
|
|
|
|
},
|
|
|
|
Version: SearchValue{
|
|
|
|
ExactMatch: true,
|
|
|
|
Value: version,
|
|
|
|
},
|
2024-03-02 21:12:31 +05:30
|
|
|
IsInternal: optional.Some(isInternal),
|
2022-04-06 07:02:09 +05:30
|
|
|
Paginator: db.NewAbsoluteListOptions(0, 1),
|
|
|
|
})
|
2022-03-30 14:12:47 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-06 07:02:09 +05:30
|
|
|
if len(pvs) == 0 {
|
2022-03-30 14:12:47 +05:30
|
|
|
return nil, ErrPackageNotExist
|
|
|
|
}
|
2022-04-06 07:02:09 +05:30
|
|
|
return pvs[0], nil
|
2022-03-30 14:12:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// GetVersionsByPackageType gets all versions of a specific type
|
|
|
|
func GetVersionsByPackageType(ctx context.Context, ownerID int64, packageType Type) ([]*PackageVersion, error) {
|
2022-04-06 07:02:09 +05:30
|
|
|
pvs, _, err := SearchVersions(ctx, &PackageSearchOptions{
|
2022-07-27 07:29:10 +05:30
|
|
|
OwnerID: ownerID,
|
|
|
|
Type: packageType,
|
2024-03-02 21:12:31 +05:30
|
|
|
IsInternal: optional.Some(false),
|
2022-04-06 07:02:09 +05:30
|
|
|
})
|
|
|
|
return pvs, err
|
2022-03-30 14:12:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// GetVersionsByPackageName gets all versions of a specific package
|
|
|
|
func GetVersionsByPackageName(ctx context.Context, ownerID int64, packageType Type, name string) ([]*PackageVersion, error) {
|
2022-04-06 07:02:09 +05:30
|
|
|
pvs, _, err := SearchVersions(ctx, &PackageSearchOptions{
|
|
|
|
OwnerID: ownerID,
|
|
|
|
Type: packageType,
|
|
|
|
Name: SearchValue{
|
|
|
|
ExactMatch: true,
|
|
|
|
Value: name,
|
|
|
|
},
|
2024-03-02 21:12:31 +05:30
|
|
|
IsInternal: optional.Some(false),
|
2022-04-06 07:02:09 +05:30
|
|
|
})
|
|
|
|
return pvs, err
|
2022-03-30 14:12:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVersionByID deletes a version by id
|
|
|
|
func DeleteVersionByID(ctx context.Context, versionID int64) error {
|
|
|
|
_, err := db.GetEngine(ctx).ID(versionID).Delete(&PackageVersion{})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasVersionFileReferences checks if there are associated files
|
|
|
|
func HasVersionFileReferences(ctx context.Context, versionID int64) (bool, error) {
|
|
|
|
return db.GetEngine(ctx).Get(&PackageFile{
|
|
|
|
VersionID: versionID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-06 07:02:09 +05:30
|
|
|
// SearchValue describes a value to search
|
|
|
|
// If ExactMatch is true, the field must match the value otherwise a LIKE search is performed.
|
|
|
|
type SearchValue struct {
|
|
|
|
Value string
|
|
|
|
ExactMatch bool
|
|
|
|
}
|
|
|
|
|
2022-10-23 06:48:15 +05:30
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2022-03-30 14:12:47 +05:30
|
|
|
// PackageSearchOptions are options for SearchXXX methods
|
2023-05-02 22:01:35 +05:30
|
|
|
// All fields optional and are not used if they have their default value (nil, "", 0)
|
2022-03-30 14:12:47 +05:30
|
|
|
type PackageSearchOptions struct {
|
2022-04-06 07:02:09 +05:30
|
|
|
OwnerID int64
|
|
|
|
RepoID int64
|
|
|
|
Type Type
|
|
|
|
PackageID int64
|
|
|
|
Name SearchValue // only results with the specific name are found
|
|
|
|
Version SearchValue // only results with the specific version are found
|
|
|
|
Properties map[string]string // only results are found which contain all listed version properties with the specific value
|
2024-03-02 21:12:31 +05:30
|
|
|
IsInternal optional.Option[bool]
|
|
|
|
HasFileWithName string // only results are found which are associated with a file with the specific name
|
|
|
|
HasFiles optional.Option[bool] // only results are found which have associated files
|
2022-10-23 06:48:15 +05:30
|
|
|
Sort VersionSort
|
2022-03-30 14:12:47 +05:30
|
|
|
db.Paginator
|
|
|
|
}
|
|
|
|
|
2023-08-14 08:20:55 +05:30
|
|
|
func (opts *PackageSearchOptions) ToConds() builder.Cond {
|
2022-07-14 12:52:09 +05:30
|
|
|
cond := builder.NewCond()
|
2024-03-02 21:12:31 +05:30
|
|
|
if opts.IsInternal.Has() {
|
2023-05-02 22:01:35 +05:30
|
|
|
cond = builder.Eq{
|
2024-03-02 21:12:31 +05:30
|
|
|
"package_version.is_internal": opts.IsInternal.Value(),
|
2023-05-02 22:01:35 +05:30
|
|
|
}
|
2022-07-14 12:52:09 +05:30
|
|
|
}
|
2022-03-30 14:12:47 +05:30
|
|
|
|
|
|
|
if opts.OwnerID != 0 {
|
|
|
|
cond = cond.And(builder.Eq{"package.owner_id": opts.OwnerID})
|
|
|
|
}
|
|
|
|
if opts.RepoID != 0 {
|
|
|
|
cond = cond.And(builder.Eq{"package.repo_id": opts.RepoID})
|
|
|
|
}
|
|
|
|
if opts.Type != "" && opts.Type != "all" {
|
|
|
|
cond = cond.And(builder.Eq{"package.type": opts.Type})
|
|
|
|
}
|
|
|
|
if opts.PackageID != 0 {
|
|
|
|
cond = cond.And(builder.Eq{"package.id": opts.PackageID})
|
|
|
|
}
|
2022-04-06 07:02:09 +05:30
|
|
|
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)})
|
|
|
|
}
|
2022-03-30 14:12:47 +05:30
|
|
|
}
|
2022-04-06 07:02:09 +05:30
|
|
|
if opts.Version.Value != "" {
|
|
|
|
if opts.Version.ExactMatch {
|
|
|
|
cond = cond.And(builder.Eq{"package_version.lower_version": strings.ToLower(opts.Version.Value)})
|
|
|
|
} else {
|
|
|
|
cond = cond.And(builder.Like{"package_version.lower_version", strings.ToLower(opts.Version.Value)})
|
|
|
|
}
|
2022-03-30 14:12:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if len(opts.Properties) != 0 {
|
|
|
|
var propsCond builder.Cond = builder.Eq{
|
|
|
|
"package_property.ref_type": PropertyTypeVersion,
|
|
|
|
}
|
|
|
|
propsCond = propsCond.And(builder.Expr("package_property.ref_id = package_version.id"))
|
|
|
|
|
|
|
|
propsCondBlock := builder.NewCond()
|
|
|
|
for name, value := range opts.Properties {
|
|
|
|
propsCondBlock = propsCondBlock.Or(builder.Eq{
|
|
|
|
"package_property.name": name,
|
|
|
|
"package_property.value": value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
propsCond = propsCond.And(propsCondBlock)
|
|
|
|
|
|
|
|
cond = cond.And(builder.Eq{
|
|
|
|
strconv.Itoa(len(opts.Properties)): builder.Select("COUNT(*)").Where(propsCond).From("package_property"),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-06 07:02:09 +05:30
|
|
|
if opts.HasFileWithName != "" {
|
|
|
|
fileCond := builder.Expr("package_file.version_id = package_version.id").And(builder.Eq{"package_file.lower_name": strings.ToLower(opts.HasFileWithName)})
|
|
|
|
|
|
|
|
cond = cond.And(builder.Exists(builder.Select("package_file.id").From("package_file").Where(fileCond)))
|
|
|
|
}
|
|
|
|
|
2024-03-02 21:12:31 +05:30
|
|
|
if opts.HasFiles.Has() {
|
2022-06-20 15:32:49 +05:30
|
|
|
filesCond := builder.Exists(builder.Select("package_file.id").From("package_file").Where(builder.Expr("package_file.version_id = package_version.id")))
|
2022-04-06 07:02:09 +05:30
|
|
|
|
2024-03-02 21:12:31 +05:30
|
|
|
if !opts.HasFiles.Value() {
|
2022-04-06 07:02:09 +05:30
|
|
|
filesCond = builder.Not{filesCond}
|
|
|
|
}
|
|
|
|
|
|
|
|
cond = cond.And(filesCond)
|
|
|
|
}
|
|
|
|
|
2022-03-30 14:12:47 +05:30
|
|
|
return cond
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts *PackageSearchOptions) configureOrderBy(e db.Engine) {
|
|
|
|
switch opts.Sort {
|
2022-10-23 06:48:15 +05:30
|
|
|
case SortNameAsc:
|
2022-03-30 14:12:47 +05:30
|
|
|
e.Asc("package.name")
|
2022-10-23 06:48:15 +05:30
|
|
|
case SortNameDesc:
|
2022-03-30 14:12:47 +05:30
|
|
|
e.Desc("package.name")
|
2022-10-23 06:48:15 +05:30
|
|
|
case SortVersionDesc:
|
2022-03-30 14:12:47 +05:30
|
|
|
e.Desc("package_version.version")
|
2022-10-23 06:48:15 +05:30
|
|
|
case SortVersionAsc:
|
2022-03-30 14:12:47 +05:30
|
|
|
e.Asc("package_version.version")
|
2022-10-23 06:48:15 +05:30
|
|
|
case SortCreatedAsc:
|
2022-03-30 14:12:47 +05:30
|
|
|
e.Asc("package_version.created_unix")
|
|
|
|
default:
|
|
|
|
e.Desc("package_version.created_unix")
|
|
|
|
}
|
2023-10-29 19:44:47 +05:30
|
|
|
|
|
|
|
// Sort by id for stable order with duplicates in the other field
|
|
|
|
e.Asc("package_version.id")
|
2022-03-30 14:12:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// SearchVersions gets all versions of packages matching the search options
|
|
|
|
func SearchVersions(ctx context.Context, opts *PackageSearchOptions) ([]*PackageVersion, int64, error) {
|
|
|
|
sess := db.GetEngine(ctx).
|
2024-04-20 16:37:00 +05:30
|
|
|
Select("package_version.*").
|
2022-03-30 14:12:47 +05:30
|
|
|
Table("package_version").
|
2024-04-20 16:37:00 +05:30
|
|
|
Join("INNER", "package", "package.id = package_version.package_id").
|
|
|
|
Where(opts.ToConds())
|
2022-03-30 14:12:47 +05:30
|
|
|
|
|
|
|
opts.configureOrderBy(sess)
|
|
|
|
|
|
|
|
if opts.Paginator != nil {
|
|
|
|
sess = db.SetSessionPagination(sess, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
pvs := make([]*PackageVersion, 0, 10)
|
|
|
|
count, err := sess.FindAndCount(&pvs)
|
|
|
|
return pvs, count, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchLatestVersions gets the latest version of every package matching the search options
|
|
|
|
func SearchLatestVersions(ctx context.Context, opts *PackageSearchOptions) ([]*PackageVersion, int64, error) {
|
2024-04-20 16:37:00 +05:30
|
|
|
in := builder.
|
|
|
|
Select("MAX(package_version.id)").
|
|
|
|
From("package_version").
|
|
|
|
InnerJoin("package", "package.id = package_version.package_id").
|
|
|
|
Where(opts.ToConds()).
|
|
|
|
GroupBy("package_version.package_id")
|
2022-12-14 14:46:01 +05:30
|
|
|
|
2022-03-30 14:12:47 +05:30
|
|
|
sess := db.GetEngine(ctx).
|
2024-04-20 16:37:00 +05:30
|
|
|
Select("package_version.*").
|
2022-03-30 14:12:47 +05:30
|
|
|
Table("package_version").
|
|
|
|
Join("INNER", "package", "package.id = package_version.package_id").
|
2024-04-20 16:37:00 +05:30
|
|
|
Where(builder.In("package_version.id", in))
|
2022-03-30 14:12:47 +05:30
|
|
|
|
|
|
|
opts.configureOrderBy(sess)
|
|
|
|
|
|
|
|
if opts.Paginator != nil {
|
|
|
|
sess = db.SetSessionPagination(sess, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
pvs := make([]*PackageVersion, 0, 10)
|
|
|
|
count, err := sess.FindAndCount(&pvs)
|
|
|
|
return pvs, count, err
|
|
|
|
}
|
2022-11-09 12:04:27 +05:30
|
|
|
|
2022-11-20 19:38:38 +05:30
|
|
|
// ExistVersion checks if a version matching the search options exist
|
|
|
|
func ExistVersion(ctx context.Context, opts *PackageSearchOptions) (bool, error) {
|
|
|
|
return db.GetEngine(ctx).
|
2023-08-14 08:20:55 +05:30
|
|
|
Where(opts.ToConds()).
|
2022-11-20 19:38:38 +05:30
|
|
|
Table("package_version").
|
|
|
|
Join("INNER", "package", "package.id = package_version.package_id").
|
|
|
|
Exist(new(PackageVersion))
|
|
|
|
}
|
|
|
|
|
2022-11-09 12:04:27 +05:30
|
|
|
// CountVersions counts all versions of packages matching the search options
|
|
|
|
func CountVersions(ctx context.Context, opts *PackageSearchOptions) (int64, error) {
|
|
|
|
return db.GetEngine(ctx).
|
2023-08-14 08:20:55 +05:30
|
|
|
Where(opts.ToConds()).
|
2022-11-09 12:04:27 +05:30
|
|
|
Table("package_version").
|
|
|
|
Join("INNER", "package", "package.id = package_version.package_id").
|
|
|
|
Count(new(PackageVersion))
|
|
|
|
}
|