@@ -131,6 +131,8 @@ type SearchRepoOptions struct {
131
131
// True -> include just mirrors
132
132
// False -> include just non-mirrors
133
133
Mirror util.OptionalBool
134
+ // only search topic name
135
+ TopicOnly bool
134
136
}
135
137
136
138
//SearchOrderBy is used to sort the result
@@ -184,7 +186,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
184
186
185
187
if opts .Collaborate != util .OptionalBoolFalse {
186
188
collaborateCond := builder .And (
187
- builder .Expr ("id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?)" , opts .OwnerID ),
189
+ builder .Expr ("repository. id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?)" , opts .OwnerID ),
188
190
builder.Neq {"owner_id" : opts .OwnerID })
189
191
if ! opts .Private {
190
192
collaborateCond = collaborateCond .And (builder .Expr ("owner_id NOT IN (SELECT org_id FROM org_user WHERE org_user.uid = ? AND org_user.is_public = ?)" , opts .OwnerID , false ))
@@ -202,7 +204,14 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
202
204
}
203
205
204
206
if opts .Keyword != "" {
205
- cond = cond .And (builder.Like {"lower_name" , strings .ToLower (opts .Keyword )})
207
+ var keywordCond = builder .NewCond ()
208
+ if opts .TopicOnly {
209
+ keywordCond = keywordCond .Or (builder.Like {"topic.name" , strings .ToLower (opts .Keyword )})
210
+ } else {
211
+ keywordCond = keywordCond .Or (builder.Like {"lower_name" , strings .ToLower (opts .Keyword )})
212
+ keywordCond = keywordCond .Or (builder.Like {"topic.name" , strings .ToLower (opts .Keyword )})
213
+ }
214
+ cond = cond .And (keywordCond )
206
215
}
207
216
208
217
if opts .Fork != util .OptionalBoolNone {
@@ -224,9 +233,15 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
224
233
sess .Join ("INNER" , "star" , "star.repo_id = repository.id" )
225
234
}
226
235
236
+ if opts .Keyword != "" {
237
+ sess .Join ("LEFT" , "repo_topic" , "repo_topic.repo_id = repository.id" )
238
+ sess .Join ("LEFT" , "topic" , "repo_topic.topic_id = topic.id" )
239
+ }
240
+
227
241
count , err := sess .
228
242
Where (cond ).
229
243
Count (new (Repository ))
244
+
230
245
if err != nil {
231
246
return nil , 0 , fmt .Errorf ("Count: %v" , err )
232
247
}
@@ -236,11 +251,23 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
236
251
sess .Join ("INNER" , "star" , "star.repo_id = repository.id" )
237
252
}
238
253
254
+ if opts .Keyword != "" {
255
+ sess .Join ("LEFT" , "repo_topic" , "repo_topic.repo_id = repository.id" )
256
+ sess .Join ("LEFT" , "topic" , "repo_topic.topic_id = topic.id" )
257
+ }
258
+
259
+ if opts .Keyword != "" {
260
+ sess .Select ("repository.*" )
261
+ sess .GroupBy ("repository.id" )
262
+ sess .OrderBy ("repository." + opts .OrderBy .String ())
263
+ } else {
264
+ sess .OrderBy (opts .OrderBy .String ())
265
+ }
266
+
239
267
repos := make (RepositoryList , 0 , opts .PageSize )
240
268
if err = sess .
241
269
Where (cond ).
242
270
Limit (opts .PageSize , (opts .Page - 1 )* opts .PageSize ).
243
- OrderBy (opts .OrderBy .String ()).
244
271
Find (& repos ); err != nil {
245
272
return nil , 0 , fmt .Errorf ("Repo: %v" , err )
246
273
}
0 commit comments