2017-02-27 06:15:03 +05:30
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
import (
"fmt"
"strings"
"github.com/go-xorm/builder"
)
// RepositoryList contains a list of repositories
type RepositoryList [ ] * Repository
2017-08-03 10:39:16 +05:30
// RepositoryListOfMap make list from values of map
func RepositoryListOfMap ( repoMap map [ int64 ] * Repository ) RepositoryList {
return RepositoryList ( valuesRepository ( repoMap ) )
}
2017-02-27 06:15:03 +05:30
func ( repos RepositoryList ) loadAttributes ( e Engine ) error {
if len ( repos ) == 0 {
return nil
}
// Load owners.
2017-03-11 14:20:12 +05:30
set := make ( map [ int64 ] struct { } )
2017-02-27 06:15:03 +05:30
for i := range repos {
2017-03-11 14:20:12 +05:30
set [ repos [ i ] . OwnerID ] = struct { } { }
2017-02-27 06:15:03 +05:30
}
2017-03-11 14:20:12 +05:30
users := make ( map [ int64 ] * User , len ( set ) )
2017-02-27 06:15:03 +05:30
if err := e .
Where ( "id > 0" ) .
2017-03-11 14:20:12 +05:30
In ( "id" , keysInt64 ( set ) ) .
2017-02-27 06:15:03 +05:30
Find ( & users ) ; err != nil {
return fmt . Errorf ( "find users: %v" , err )
}
for i := range repos {
2017-03-11 14:20:12 +05:30
repos [ i ] . Owner = users [ repos [ i ] . OwnerID ]
2017-02-27 06:15:03 +05:30
}
return nil
}
// LoadAttributes loads the attributes for the given RepositoryList
func ( repos RepositoryList ) LoadAttributes ( ) error {
return repos . loadAttributes ( x )
}
// MirrorRepositoryList contains the mirror repositories
type MirrorRepositoryList [ ] * Repository
func ( repos MirrorRepositoryList ) loadAttributes ( e Engine ) error {
if len ( repos ) == 0 {
return nil
}
// Load mirrors.
repoIDs := make ( [ ] int64 , 0 , len ( repos ) )
for i := range repos {
if ! repos [ i ] . IsMirror {
continue
}
repoIDs = append ( repoIDs , repos [ i ] . ID )
}
mirrors := make ( [ ] * Mirror , 0 , len ( repoIDs ) )
if err := e .
Where ( "id > 0" ) .
In ( "repo_id" , repoIDs ) .
Find ( & mirrors ) ; err != nil {
return fmt . Errorf ( "find mirrors: %v" , err )
}
set := make ( map [ int64 ] * Mirror )
for i := range mirrors {
set [ mirrors [ i ] . RepoID ] = mirrors [ i ]
}
for i := range repos {
repos [ i ] . Mirror = set [ repos [ i ] . ID ]
}
return nil
}
// LoadAttributes loads the attributes for the given MirrorRepositoryList
func ( repos MirrorRepositoryList ) LoadAttributes ( ) error {
return repos . loadAttributes ( x )
}
// SearchRepoOptions holds the search options
2017-05-02 19:05:59 +05:30
// swagger:parameters repoSearch
2017-02-27 06:15:03 +05:30
type SearchRepoOptions struct {
2017-05-02 19:05:59 +05:30
// Keyword to search
//
// in: query
Keyword string ` json:"q" `
// Owner in we search search
//
// in: query
2017-09-22 18:23:21 +05:30
OwnerID int64 ` json:"uid" `
OrderBy SearchOrderBy ` json:"-" `
Private bool ` json:"-" ` // Include private repositories in results
Collaborate bool ` json:"-" ` // Include collaborative repositories
Starred bool ` json:"-" `
Page int ` json:"-" `
IsProfile bool ` json:"-" `
2017-10-11 02:07:18 +05:30
AllPublic bool ` json:"-" ` // Include also all public repositories
2017-05-02 19:05:59 +05:30
// Limit of result
//
// maximum: setting.ExplorePagingNum
// in: query
PageSize int ` json:"limit" ` // Can be smaller than or equal to setting.ExplorePagingNum
2017-02-27 06:15:03 +05:30
}
2017-09-22 18:23:21 +05:30
//SearchOrderBy is used to sort the result
type SearchOrderBy string
func ( s SearchOrderBy ) String ( ) string {
return string ( s )
}
// Strings for sorting result
const (
SearchOrderByAlphabetically SearchOrderBy = "name ASC"
SearchOrderByAlphabeticallyReverse = "name DESC"
SearchOrderByLeastUpdated = "updated_unix ASC"
SearchOrderByRecentUpdated = "updated_unix DESC"
SearchOrderByOldest = "created_unix ASC"
SearchOrderByNewest = "created_unix DESC"
SearchOrderBySize = "size ASC"
SearchOrderBySizeReverse = "size DESC"
2017-10-11 02:07:18 +05:30
SearchOrderByID = "id ASC"
SearchOrderByIDReverse = "id DESC"
2017-09-22 18:23:21 +05:30
)
2017-02-27 06:15:03 +05:30
// SearchRepositoryByName takes keyword and part of repository name to search,
// it returns results in given range and number of total results.
2017-10-17 20:50:22 +05:30
func SearchRepositoryByName ( opts * SearchRepoOptions ) ( RepositoryList , int64 , error ) {
2017-02-27 06:15:03 +05:30
if opts . Page <= 0 {
opts . Page = 1
}
2017-10-17 20:50:22 +05:30
var cond = builder . NewCond ( )
2017-08-24 19:31:03 +05:30
2017-10-17 20:50:22 +05:30
if ! opts . Private {
cond = cond . And ( builder . Eq { "is_private" : false } )
}
2017-08-24 19:31:03 +05:30
2017-10-17 20:50:22 +05:30
starred := false
if opts . OwnerID > 0 {
if opts . Starred {
starred = true
cond = builder . Eq {
"star.uid" : opts . OwnerID ,
2017-08-24 19:31:03 +05:30
}
2017-10-17 20:50:22 +05:30
} else {
var accessCond builder . Cond = builder . Eq { "owner_id" : opts . OwnerID }
2017-08-24 19:31:03 +05:30
if opts . Collaborate {
2017-10-17 20:50:22 +05:30
collaborateCond := builder . And (
builder . Expr ( "id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?)" , opts . OwnerID ) ,
builder . Neq { "owner_id" : opts . OwnerID } )
if ! opts . Private {
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 ) )
}
accessCond = accessCond . Or ( collaborateCond )
2017-08-24 19:31:03 +05:30
}
2017-10-17 20:50:22 +05:30
cond = cond . And ( accessCond )
}
2017-02-27 06:15:03 +05:30
}
2017-10-11 02:07:18 +05:30
if opts . OwnerID > 0 && opts . AllPublic {
cond = cond . Or ( builder . Eq { "is_private" : false } )
}
if opts . Keyword != "" {
2017-10-17 20:50:22 +05:30
cond = cond . And ( builder . Like { "lower_name" , strings . ToLower ( opts . Keyword ) } )
2017-10-11 02:07:18 +05:30
}
2017-02-27 06:15:03 +05:30
if len ( opts . OrderBy ) == 0 {
2017-09-22 18:23:21 +05:30
opts . OrderBy = SearchOrderByAlphabetically
2017-02-27 06:15:03 +05:30
}
2017-08-23 07:00:54 +05:30
sess := x . NewSession ( )
defer sess . Close ( )
2017-10-17 20:50:22 +05:30
if starred {
sess . Join ( "INNER" , "star" , "star.repo_id = repository.id" )
}
2017-08-23 07:00:54 +05:30
2017-10-17 20:50:22 +05:30
count , err := sess .
Where ( cond ) .
Count ( new ( Repository ) )
if err != nil {
return nil , 0 , fmt . Errorf ( "Count: %v" , err )
}
// Set again after reset by Count()
if starred {
2017-08-23 07:00:54 +05:30
sess . Join ( "INNER" , "star" , "star.repo_id = repository.id" )
2017-02-27 06:15:03 +05:30
}
2017-10-17 20:50:22 +05:30
repos := make ( RepositoryList , 0 , opts . PageSize )
2017-02-27 06:15:03 +05:30
if err = sess .
2017-08-23 07:00:54 +05:30
Where ( cond ) .
2017-02-27 06:15:03 +05:30
Limit ( opts . PageSize , ( opts . Page - 1 ) * opts . PageSize ) .
2017-09-22 18:23:21 +05:30
OrderBy ( opts . OrderBy . String ( ) ) .
2017-02-27 06:15:03 +05:30
Find ( & repos ) ; err != nil {
return nil , 0 , fmt . Errorf ( "Repo: %v" , err )
}
if ! opts . IsProfile {
2017-08-23 07:00:54 +05:30
if err = repos . loadAttributes ( sess ) ; err != nil {
2017-02-27 06:15:03 +05:30
return nil , 0 , fmt . Errorf ( "LoadAttributes: %v" , err )
}
}
2017-10-17 20:50:22 +05:30
return repos , count , nil
2017-02-27 06:15:03 +05:30
}