@@ -12,16 +12,13 @@ import (
12
12
13
13
"code.gitea.io/gitea/models/db"
14
14
"code.gitea.io/gitea/modules/timeutil"
15
+ "code.gitea.io/gitea/modules/util"
15
16
16
17
"xorm.io/builder"
17
18
)
18
19
19
- var (
20
- // ErrDuplicatePackageVersion indicates a duplicated package version error
21
- ErrDuplicatePackageVersion = errors .New ("Package version does exist already" )
22
- // ErrPackageVersionNotExist indicates a package version not exist error
23
- ErrPackageVersionNotExist = errors .New ("Package version does not exist" )
24
- )
20
+ // ErrDuplicatePackageVersion indicates a duplicated package version error
21
+ var ErrDuplicatePackageVersion = errors .New ("Package version already exists" )
25
22
26
23
func init () {
27
24
db .RegisterModel (new (PackageVersion ))
@@ -99,75 +96,49 @@ func GetInternalVersionByNameAndVersion(ctx context.Context, ownerID int64, pack
99
96
}
100
97
101
98
func getVersionByNameAndVersion (ctx context.Context , ownerID int64 , packageType Type , name , version string , isInternal bool ) (* PackageVersion , error ) {
102
- var cond builder.Cond = builder.Eq {
103
- "package.owner_id" : ownerID ,
104
- "package.type" : packageType ,
105
- "package.lower_name" : strings .ToLower (name ),
106
- "package_version.is_internal" : isInternal ,
107
- }
108
- pv := & PackageVersion {
109
- LowerVersion : strings .ToLower (version ),
110
- }
111
- has , err := db .GetEngine (ctx ).
112
- Join ("INNER" , "package" , "package.id = package_version.package_id" ).
113
- Where (cond ).
114
- Get (pv )
99
+ pvs , _ , err := SearchVersions (ctx , & PackageSearchOptions {
100
+ OwnerID : ownerID ,
101
+ Type : packageType ,
102
+ Name : SearchValue {
103
+ ExactMatch : true ,
104
+ Value : name ,
105
+ },
106
+ Version : SearchValue {
107
+ ExactMatch : true ,
108
+ Value : version ,
109
+ },
110
+ IsInternal : isInternal ,
111
+ Paginator : db .NewAbsoluteListOptions (0 , 1 ),
112
+ })
115
113
if err != nil {
116
114
return nil , err
117
115
}
118
- if ! has {
116
+ if len ( pvs ) == 0 {
119
117
return nil , ErrPackageNotExist
120
118
}
121
-
122
- return pv , nil
119
+ return pvs [0 ], nil
123
120
}
124
121
125
122
// GetVersionsByPackageType gets all versions of a specific type
126
123
func GetVersionsByPackageType (ctx context.Context , ownerID int64 , packageType Type ) ([]* PackageVersion , error ) {
127
- var cond builder.Cond = builder.Eq {
128
- "package.owner_id" : ownerID ,
129
- "package.type" : packageType ,
130
- "package_version.is_internal" : false ,
131
- }
132
-
133
- pvs := make ([]* PackageVersion , 0 , 10 )
134
- return pvs , db .GetEngine (ctx ).
135
- Where (cond ).
136
- Join ("INNER" , "package" , "package.id = package_version.package_id" ).
137
- Find (& pvs )
124
+ pvs , _ , err := SearchVersions (ctx , & PackageSearchOptions {
125
+ OwnerID : ownerID ,
126
+ Type : packageType ,
127
+ })
128
+ return pvs , err
138
129
}
139
130
140
131
// GetVersionsByPackageName gets all versions of a specific package
141
132
func GetVersionsByPackageName (ctx context.Context , ownerID int64 , packageType Type , name string ) ([]* PackageVersion , error ) {
142
- var cond builder.Cond = builder.Eq {
143
- "package.owner_id" : ownerID ,
144
- "package.type" : packageType ,
145
- "package.lower_name" : strings .ToLower (name ),
146
- "package_version.is_internal" : false ,
147
- }
148
-
149
- pvs := make ([]* PackageVersion , 0 , 10 )
150
- return pvs , db .GetEngine (ctx ).
151
- Where (cond ).
152
- Join ("INNER" , "package" , "package.id = package_version.package_id" ).
153
- Find (& pvs )
154
- }
155
-
156
- // GetVersionsByFilename gets all versions which are linked to a filename
157
- func GetVersionsByFilename (ctx context.Context , ownerID int64 , packageType Type , filename string ) ([]* PackageVersion , error ) {
158
- var cond builder.Cond = builder.Eq {
159
- "package.owner_id" : ownerID ,
160
- "package.type" : packageType ,
161
- "package_file.lower_name" : strings .ToLower (filename ),
162
- "package_version.is_internal" : false ,
163
- }
164
-
165
- pvs := make ([]* PackageVersion , 0 , 10 )
166
- return pvs , db .GetEngine (ctx ).
167
- Where (cond ).
168
- Join ("INNER" , "package_file" , "package_file.version_id = package_version.id" ).
169
- Join ("INNER" , "package" , "package.id = package_version.package_id" ).
170
- Find (& pvs )
133
+ pvs , _ , err := SearchVersions (ctx , & PackageSearchOptions {
134
+ OwnerID : ownerID ,
135
+ Type : packageType ,
136
+ Name : SearchValue {
137
+ ExactMatch : true ,
138
+ Value : name ,
139
+ },
140
+ })
141
+ return pvs , err
171
142
}
172
143
173
144
// DeleteVersionByID deletes a version by id
@@ -183,21 +154,32 @@ func HasVersionFileReferences(ctx context.Context, versionID int64) (bool, error
183
154
})
184
155
}
185
156
157
+ // SearchValue describes a value to search
158
+ // If ExactMatch is true, the field must match the value otherwise a LIKE search is performed.
159
+ type SearchValue struct {
160
+ Value string
161
+ ExactMatch bool
162
+ }
163
+
186
164
// PackageSearchOptions are options for SearchXXX methods
165
+ // Besides IsInternal are all fields optional and are not used if they have their default value (nil, "", 0)
187
166
type PackageSearchOptions struct {
188
- OwnerID int64
189
- RepoID int64
190
- Type string
191
- PackageID int64
192
- QueryName string
193
- QueryVersion string
194
- Properties map [string ]string
195
- Sort string
167
+ OwnerID int64
168
+ RepoID int64
169
+ Type Type
170
+ PackageID int64
171
+ Name SearchValue // only results with the specific name are found
172
+ Version SearchValue // only results with the specific version are found
173
+ Properties map [string ]string // only results are found which contain all listed version properties with the specific value
174
+ IsInternal bool
175
+ HasFileWithName string // only results are found which are associated with a file with the specific name
176
+ HasFiles util.OptionalBool // only results are found which have associated files
177
+ Sort string
196
178
db.Paginator
197
179
}
198
180
199
181
func (opts * PackageSearchOptions ) toConds () builder.Cond {
200
- var cond builder.Cond = builder.Eq {"package_version.is_internal" : false }
182
+ var cond builder.Cond = builder.Eq {"package_version.is_internal" : opts . IsInternal }
201
183
202
184
if opts .OwnerID != 0 {
203
185
cond = cond .And (builder.Eq {"package.owner_id" : opts .OwnerID })
@@ -211,11 +193,19 @@ func (opts *PackageSearchOptions) toConds() builder.Cond {
211
193
if opts .PackageID != 0 {
212
194
cond = cond .And (builder.Eq {"package.id" : opts .PackageID })
213
195
}
214
- if opts .QueryName != "" {
215
- cond = cond .And (builder.Like {"package.lower_name" , strings .ToLower (opts .QueryName )})
196
+ if opts .Name .Value != "" {
197
+ if opts .Name .ExactMatch {
198
+ cond = cond .And (builder.Eq {"package.lower_name" : strings .ToLower (opts .Name .Value )})
199
+ } else {
200
+ cond = cond .And (builder.Like {"package.lower_name" , strings .ToLower (opts .Name .Value )})
201
+ }
216
202
}
217
- if opts .QueryVersion != "" {
218
- cond = cond .And (builder.Like {"package_version.lower_version" , strings .ToLower (opts .QueryVersion )})
203
+ if opts .Version .Value != "" {
204
+ if opts .Version .ExactMatch {
205
+ cond = cond .And (builder.Eq {"package_version.lower_version" : strings .ToLower (opts .Version .Value )})
206
+ } else {
207
+ cond = cond .And (builder.Like {"package_version.lower_version" , strings .ToLower (opts .Version .Value )})
208
+ }
219
209
}
220
210
221
211
if len (opts .Properties ) != 0 {
@@ -238,6 +228,22 @@ func (opts *PackageSearchOptions) toConds() builder.Cond {
238
228
})
239
229
}
240
230
231
+ if opts .HasFileWithName != "" {
232
+ fileCond := builder .Expr ("package_file.version_id = package_version.id" ).And (builder.Eq {"package_file.lower_name" : strings .ToLower (opts .HasFileWithName )})
233
+
234
+ cond = cond .And (builder .Exists (builder .Select ("package_file.id" ).From ("package_file" ).Where (fileCond )))
235
+ }
236
+
237
+ if ! opts .HasFiles .IsNone () {
238
+ var filesCond builder.Cond = builder .Exists (builder .Select ("package_file.id" ).From ("package_file" ).Where (builder .Expr ("package_file.version_id = package_version.id" )))
239
+
240
+ if opts .HasFiles .IsFalse () {
241
+ filesCond = builder.Not {filesCond }
242
+ }
243
+
244
+ cond = cond .And (filesCond )
245
+ }
246
+
241
247
return cond
242
248
}
243
249
@@ -297,20 +303,3 @@ func SearchLatestVersions(ctx context.Context, opts *PackageSearchOptions) ([]*P
297
303
count , err := sess .FindAndCount (& pvs )
298
304
return pvs , count , err
299
305
}
300
-
301
- // FindVersionsByPropertyNameAndValue gets all package versions which are associated with a specific property + value
302
- func FindVersionsByPropertyNameAndValue (ctx context.Context , packageID int64 , name , value string ) ([]* PackageVersion , error ) {
303
- var cond builder.Cond = builder.Eq {
304
- "package_property.ref_type" : PropertyTypeVersion ,
305
- "package_property.name" : name ,
306
- "package_property.value" : value ,
307
- "package_version.package_id" : packageID ,
308
- "package_version.is_internal" : false ,
309
- }
310
-
311
- pvs := make ([]* PackageVersion , 0 , 5 )
312
- return pvs , db .GetEngine (ctx ).
313
- Where (cond ).
314
- Join ("INNER" , "package_property" , "package_property.ref_id = package_version.id" ).
315
- Find (& pvs )
316
- }
0 commit comments