@@ -131,6 +131,8 @@ type SearchRepoOptions struct {
131131 // True -> include just mirrors
132132 // False -> include just non-mirrors
133133 Mirror util.OptionalBool
134+ // only search topic name
135+ TopicOnly bool
134136}
135137
136138//SearchOrderBy is used to sort the result
@@ -184,7 +186,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
184186
185187 if opts .Collaborate != util .OptionalBoolFalse {
186188 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 ),
188190 builder.Neq {"owner_id" : opts .OwnerID })
189191 if ! opts .Private {
190192 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
202204 }
203205
204206 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 )
206215 }
207216
208217 if opts .Fork != util .OptionalBoolNone {
@@ -224,9 +233,15 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
224233 sess .Join ("INNER" , "star" , "star.repo_id = repository.id" )
225234 }
226235
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+
227241 count , err := sess .
228242 Where (cond ).
229243 Count (new (Repository ))
244+
230245 if err != nil {
231246 return nil , 0 , fmt .Errorf ("Count: %v" , err )
232247 }
@@ -236,11 +251,23 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
236251 sess .Join ("INNER" , "star" , "star.repo_id = repository.id" )
237252 }
238253
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+
239267 repos := make (RepositoryList , 0 , opts .PageSize )
240268 if err = sess .
241269 Where (cond ).
242270 Limit (opts .PageSize , (opts .Page - 1 )* opts .PageSize ).
243- OrderBy (opts .OrderBy .String ()).
244271 Find (& repos ); err != nil {
245272 return nil , 0 , fmt .Errorf ("Repo: %v" , err )
246273 }
0 commit comments