code.gitea.io/gitea@v1.21.7/models/packages/debian/search.go (about) 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 packages.GetDistinctPropertyValues( 92 ctx, 93 packages.TypeDebian, 94 ownerID, 95 packages.PropertyTypeFile, 96 debian_module.PropertyDistribution, 97 nil, 98 ) 99 } 100 101 // GetComponents gets all available components for the given distribution 102 func GetComponents(ctx context.Context, ownerID int64, distribution string) ([]string, error) { 103 return packages.GetDistinctPropertyValues( 104 ctx, 105 packages.TypeDebian, 106 ownerID, 107 packages.PropertyTypeFile, 108 debian_module.PropertyComponent, 109 &packages.DistinctPropertyDependency{ 110 Name: debian_module.PropertyDistribution, 111 Value: distribution, 112 }, 113 ) 114 } 115 116 // GetArchitectures gets all available architectures for the given distribution 117 func GetArchitectures(ctx context.Context, ownerID int64, distribution string) ([]string, error) { 118 return packages.GetDistinctPropertyValues( 119 ctx, 120 packages.TypeDebian, 121 ownerID, 122 packages.PropertyTypeFile, 123 debian_module.PropertyArchitecture, 124 &packages.DistinctPropertyDependency{ 125 Name: debian_module.PropertyDistribution, 126 Value: distribution, 127 }, 128 ) 129 }