|
| 1 | +// Copyright 2023 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package debian |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/models/db" |
| 11 | + "code.gitea.io/gitea/models/packages" |
| 12 | + debian_module "code.gitea.io/gitea/modules/packages/debian" |
| 13 | + |
| 14 | + "xorm.io/builder" |
| 15 | +) |
| 16 | + |
| 17 | +type PackageSearchOptions struct { |
| 18 | + OwnerID int64 |
| 19 | + Distribution string |
| 20 | + Component string |
| 21 | + Architecture string |
| 22 | +} |
| 23 | + |
| 24 | +// SearchLatestPackages gets the latest packages matching the search options |
| 25 | +func SearchLatestPackages(ctx context.Context, opts *PackageSearchOptions) ([]*packages.PackageFileDescriptor, error) { |
| 26 | + var cond builder.Cond = builder.Eq{ |
| 27 | + "package_file.is_lead": true, |
| 28 | + "package.type": packages.TypeDebian, |
| 29 | + "package.owner_id": opts.OwnerID, |
| 30 | + "package.is_internal": false, |
| 31 | + "package_version.is_internal": false, |
| 32 | + } |
| 33 | + |
| 34 | + props := make(map[string]string) |
| 35 | + if opts.Distribution != "" { |
| 36 | + props[debian_module.PropertyDistribution] = opts.Distribution |
| 37 | + } |
| 38 | + if opts.Component != "" { |
| 39 | + props[debian_module.PropertyComponent] = opts.Component |
| 40 | + } |
| 41 | + if opts.Architecture != "" { |
| 42 | + props[debian_module.PropertyArchitecture] = opts.Architecture |
| 43 | + } |
| 44 | + |
| 45 | + if len(props) > 0 { |
| 46 | + var propsCond builder.Cond = builder.Eq{ |
| 47 | + "package_property.ref_type": packages.PropertyTypeFile, |
| 48 | + } |
| 49 | + propsCond = propsCond.And(builder.Expr("package_property.ref_id = package_file.id")) |
| 50 | + |
| 51 | + propsCondBlock := builder.NewCond() |
| 52 | + for name, value := range props { |
| 53 | + propsCondBlock = propsCondBlock.Or(builder.Eq{ |
| 54 | + "package_property.name": name, |
| 55 | + "package_property.value": value, |
| 56 | + }) |
| 57 | + } |
| 58 | + propsCond = propsCond.And(propsCondBlock) |
| 59 | + |
| 60 | + cond = cond.And(builder.Eq{ |
| 61 | + strconv.Itoa(len(props)): builder.Select("COUNT(*)").Where(propsCond).From("package_property"), |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + cond = cond. |
| 66 | + And(builder.Expr("pv2.id IS NULL")) |
| 67 | + |
| 68 | + joinCond := builder. |
| 69 | + 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))"). |
| 70 | + And(builder.Eq{"pv2.is_internal": false}) |
| 71 | + |
| 72 | + pfs := make([]*packages.PackageFile, 0, 10) |
| 73 | + err := db.GetEngine(ctx). |
| 74 | + Table("package_file"). |
| 75 | + Select("package_file.*"). |
| 76 | + Join("INNER", "package_version", "package_version.id = package_file.version_id"). |
| 77 | + Join("LEFT", "package_version pv2", joinCond). |
| 78 | + Join("INNER", "package", "package.id = package_version.package_id"). |
| 79 | + Where(cond). |
| 80 | + Desc("package_version.created_unix"). |
| 81 | + Find(&pfs) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + |
| 86 | + return packages.GetPackageFileDescriptors(ctx, pfs) |
| 87 | +} |
| 88 | + |
| 89 | +// GetDistributions gets all available distributions |
| 90 | +func GetDistributions(ctx context.Context, ownerID int64) ([]string, error) { |
| 91 | + return getDistinctPropertyValues(ctx, ownerID, "", debian_module.PropertyDistribution) |
| 92 | +} |
| 93 | + |
| 94 | +// GetComponents gets all available components for the given distribution |
| 95 | +func GetComponents(ctx context.Context, ownerID int64, distribution string) ([]string, error) { |
| 96 | + return getDistinctPropertyValues(ctx, ownerID, distribution, debian_module.PropertyComponent) |
| 97 | +} |
| 98 | + |
| 99 | +// GetArchitectures gets all available architectures for the given distribution |
| 100 | +func GetArchitectures(ctx context.Context, ownerID int64, distribution string) ([]string, error) { |
| 101 | + return getDistinctPropertyValues(ctx, ownerID, distribution, debian_module.PropertyArchitecture) |
| 102 | +} |
| 103 | + |
| 104 | +func getDistinctPropertyValues(ctx context.Context, ownerID int64, distribution, propName string) ([]string, error) { |
| 105 | + var cond builder.Cond = builder.Eq{ |
| 106 | + "package_property.ref_type": packages.PropertyTypeFile, |
| 107 | + "package_property.name": propName, |
| 108 | + "package.type": packages.TypeDebian, |
| 109 | + "package.owner_id": ownerID, |
| 110 | + } |
| 111 | + if distribution != "" { |
| 112 | + innerCond := builder. |
| 113 | + Expr("pp.ref_id = package_property.ref_id"). |
| 114 | + And(builder.Eq{ |
| 115 | + "pp.ref_type": packages.PropertyTypeFile, |
| 116 | + "pp.name": debian_module.PropertyDistribution, |
| 117 | + "pp.value": distribution, |
| 118 | + }) |
| 119 | + cond = cond.And(builder.Exists(builder.Select("pp.ref_id").From("package_property pp").Where(innerCond))) |
| 120 | + } |
| 121 | + |
| 122 | + values := make([]string, 0, 5) |
| 123 | + return values, db.GetEngine(ctx). |
| 124 | + Table("package_property"). |
| 125 | + Distinct("package_property.value"). |
| 126 | + Join("INNER", "package_file", "package_file.id = package_property.ref_id"). |
| 127 | + Join("INNER", "package_version", "package_version.id = package_file.version_id"). |
| 128 | + Join("INNER", "package", "package.id = package_version.package_id"). |
| 129 | + Where(cond). |
| 130 | + Find(&values) |
| 131 | +} |
0 commit comments