2017-02-27 09:45:03 +09:00
// 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"
2019-02-19 01:00:27 +09:00
"code.gitea.io/gitea/modules/structs"
2017-10-27 06:16:13 +09:00
"code.gitea.io/gitea/modules/util"
2019-06-24 00:22:43 +09:00
"xorm.io/builder"
2017-02-27 09:45:03 +09:00
)
2017-12-31 23:45:46 +09:00
// RepositoryListDefaultPageSize is the default number of repositories
// to load in memory when running administrative tasks on all (or almost
// all) of them.
// The number should be low enough to avoid filling up all RAM with
// repository data...
const RepositoryListDefaultPageSize = 64
2017-02-27 09:45:03 +09:00
// RepositoryList contains a list of repositories
type RepositoryList [ ] * Repository
2017-12-04 13:39:01 +09:00
func ( repos RepositoryList ) Len ( ) int {
return len ( repos )
}
func ( repos RepositoryList ) Less ( i , j int ) bool {
return repos [ i ] . FullName ( ) < repos [ j ] . FullName ( )
}
func ( repos RepositoryList ) Swap ( i , j int ) {
repos [ i ] , repos [ j ] = repos [ j ] , repos [ i ]
}
2017-08-03 14:09:16 +09:00
// RepositoryListOfMap make list from values of map
func RepositoryListOfMap ( repoMap map [ int64 ] * Repository ) RepositoryList {
return RepositoryList ( valuesRepository ( repoMap ) )
}
2017-02-27 09:45:03 +09:00
func ( repos RepositoryList ) loadAttributes ( e Engine ) error {
if len ( repos ) == 0 {
return nil
}
// Load owners.
2017-03-11 17:50:12 +09:00
set := make ( map [ int64 ] struct { } )
2017-02-27 09:45:03 +09:00
for i := range repos {
2017-03-11 17:50:12 +09:00
set [ repos [ i ] . OwnerID ] = struct { } { }
2017-02-27 09:45:03 +09:00
}
2017-03-11 17:50:12 +09:00
users := make ( map [ int64 ] * User , len ( set ) )
2017-02-27 09:45:03 +09:00
if err := e .
Where ( "id > 0" ) .
2017-03-11 17:50:12 +09:00
In ( "id" , keysInt64 ( set ) ) .
2017-02-27 09:45:03 +09:00
Find ( & users ) ; err != nil {
return fmt . Errorf ( "find users: %v" , err )
}
for i := range repos {
2017-03-11 17:50:12 +09:00
repos [ i ] . Owner = users [ repos [ i ] . OwnerID ]
2017-02-27 09:45:03 +09:00
}
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
type SearchRepoOptions struct {
2019-11-12 00:15:29 +09:00
UserID int64
UserIsAdmin bool
Keyword string
OwnerID int64
PriorityOwnerID int64
OrderBy SearchOrderBy
Private bool // Include private repositories in results
StarredByID int64
Page int
IsProfile bool
AllPublic bool // Include also all public repositories
PageSize int // Can be smaller than or equal to setting.ExplorePagingNum
2017-10-27 06:16:13 +09:00
// None -> include collaborative AND non-collaborative
// True -> include just collaborative
// False -> incude just non-collaborative
Collaborate util . OptionalBool
// None -> include forks AND non-forks
// True -> include just forks
// False -> include just non-forks
Fork util . OptionalBool
2019-11-12 00:15:29 +09:00
// None -> include templates AND non-templates
// True -> include just templates
// False -> include just non-templates
Template util . OptionalBool
2017-10-27 06:16:13 +09:00
// None -> include mirrors AND non-mirrors
// True -> include just mirrors
// False -> include just non-mirrors
Mirror util . OptionalBool
2018-09-13 11:33:48 +09:00
// only search topic name
TopicOnly bool
2019-08-26 02:06:36 +09:00
// include description in keyword search
IncludeDescription bool
2017-02-27 09:45:03 +09:00
}
2017-09-22 21:53:21 +09:00
//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"
2019-06-13 04:41:28 +09:00
SearchOrderByAlphabeticallyReverse SearchOrderBy = "name DESC"
SearchOrderByLeastUpdated SearchOrderBy = "updated_unix ASC"
SearchOrderByRecentUpdated SearchOrderBy = "updated_unix DESC"
SearchOrderByOldest SearchOrderBy = "created_unix ASC"
SearchOrderByNewest SearchOrderBy = "created_unix DESC"
SearchOrderBySize SearchOrderBy = "size ASC"
SearchOrderBySizeReverse SearchOrderBy = "size DESC"
SearchOrderByID SearchOrderBy = "id ASC"
SearchOrderByIDReverse SearchOrderBy = "id DESC"
SearchOrderByStars SearchOrderBy = "num_stars ASC"
SearchOrderByStarsReverse SearchOrderBy = "num_stars DESC"
SearchOrderByForks SearchOrderBy = "num_forks ASC"
SearchOrderByForksReverse SearchOrderBy = "num_forks DESC"
2017-09-22 21:53:21 +09:00
)
2019-08-26 02:06:36 +09:00
// SearchRepository returns repositories based on search options,
2017-02-27 09:45:03 +09:00
// it returns results in given range and number of total results.
2019-08-26 02:06:36 +09:00
func SearchRepository ( opts * SearchRepoOptions ) ( RepositoryList , int64 , error ) {
2017-02-27 09:45:03 +09:00
if opts . Page <= 0 {
opts . Page = 1
}
2017-10-18 00:20:22 +09:00
var cond = builder . NewCond ( )
2017-08-24 23:01:03 +09:00
2019-05-16 00:24:39 +09:00
if opts . Private {
if ! opts . UserIsAdmin && opts . UserID != 0 && opts . UserID != opts . OwnerID {
// OK we're in the context of a User
2019-10-29 03:31:55 +09:00
cond = cond . And ( accessibleRepositoryCondition ( opts . UserID ) )
2019-05-16 00:24:39 +09:00
}
} else {
// Not looking at private organisations
// We should be able to see all non-private repositories that either:
2017-10-18 00:20:22 +09:00
cond = cond . And ( builder . Eq { "is_private" : false } )
2019-02-19 01:00:27 +09:00
accessCond := builder . Or (
2019-05-16 00:24:39 +09:00
// A. Aren't in organisations __OR__
builder . NotIn ( "owner_id" , builder . Select ( "id" ) . From ( "`user`" ) . Where ( builder . Eq { "type" : UserTypeOrganization } ) ) ,
// B. Isn't a private or limited organisation.
builder . NotIn ( "owner_id" , builder . Select ( "id" ) . From ( "`user`" ) . Where ( builder . Or ( builder . Eq { "visibility" : structs . VisibleTypeLimited } , builder . Eq { "visibility" : structs . VisibleTypePrivate } ) ) ) )
2019-02-19 01:00:27 +09:00
cond = cond . And ( accessCond )
2017-10-18 00:20:22 +09:00
}
2017-08-24 23:01:03 +09:00
2019-11-12 00:15:29 +09:00
if opts . Template != util . OptionalBoolNone {
cond = cond . And ( builder . Eq { "is_template" : opts . Template == util . OptionalBoolTrue } )
}
2019-05-16 00:24:39 +09:00
// Restrict to starred repositories
if opts . StarredByID > 0 {
cond = cond . And ( builder . In ( "id" , builder . Select ( "repo_id" ) . From ( "star" ) . Where ( builder . Eq { "uid" : opts . StarredByID } ) ) )
}
// Restrict repositories to those the OwnerID owns or contributes to as per opts.Collaborate
2017-10-18 00:20:22 +09:00
if opts . OwnerID > 0 {
2019-05-16 00:24:39 +09:00
var accessCond = builder . NewCond ( )
if opts . Collaborate != util . OptionalBoolTrue {
accessCond = builder . Eq { "owner_id" : opts . OwnerID }
}
2017-08-24 23:01:03 +09:00
2019-05-16 00:24:39 +09:00
if opts . Collaborate != util . OptionalBoolFalse {
collaborateCond := builder . And (
builder . Or (
2018-09-13 11:33:48 +09:00
builder . Expr ( "repository.id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?)" , opts . OwnerID ) ,
2019-05-16 00:24:39 +09:00
builder . In ( "id" , builder . Select ( "`team_repo`.repo_id" ) .
From ( "team_repo" ) .
Where ( builder . Eq { "`team_user`.uid" : opts . OwnerID } ) .
Join ( "INNER" , "team_user" , "`team_user`.team_id = `team_repo`.team_id" ) ) ) ,
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 ) )
2017-08-24 23:01:03 +09:00
}
2019-05-16 00:24:39 +09:00
accessCond = accessCond . Or ( collaborateCond )
}
2017-10-27 06:16:13 +09:00
2019-05-16 00:24:39 +09:00
if opts . AllPublic {
accessCond = accessCond . Or ( builder . Eq { "is_private" : false } )
2017-10-18 00:20:22 +09:00
}
2019-05-16 00:24:39 +09:00
cond = cond . And ( accessCond )
2017-02-27 09:45:03 +09:00
}
2017-10-11 05:37:18 +09:00
if opts . Keyword != "" {
2018-10-18 12:14:28 +09:00
// separate keyword
2018-10-31 06:48:37 +09:00
var subQueryCond = builder . NewCond ( )
2018-10-18 12:14:28 +09:00
for _ , v := range strings . Split ( opts . Keyword , "," ) {
2019-08-23 11:37:35 +09:00
if opts . TopicOnly {
subQueryCond = subQueryCond . Or ( builder . Eq { "topic.name" : strings . ToLower ( v ) } )
} else {
subQueryCond = subQueryCond . Or ( builder . Like { "topic.name" , strings . ToLower ( v ) } )
}
2018-10-31 06:48:37 +09:00
}
subQuery := builder . Select ( "repo_topic.repo_id" ) . From ( "repo_topic" ) .
Join ( "INNER" , "topic" , "topic.id = repo_topic.topic_id" ) .
Where ( subQueryCond ) .
GroupBy ( "repo_topic.repo_id" )
var keywordCond = builder . In ( "id" , subQuery )
if ! opts . TopicOnly {
var likes = builder . NewCond ( )
for _ , v := range strings . Split ( opts . Keyword , "," ) {
likes = likes . Or ( builder . Like { "lower_name" , strings . ToLower ( v ) } )
2019-08-26 02:06:36 +09:00
if opts . IncludeDescription {
likes = likes . Or ( builder . Like { "LOWER(description)" , strings . ToLower ( v ) } )
}
2018-10-18 12:14:28 +09:00
}
2018-10-31 06:48:37 +09:00
keywordCond = keywordCond . Or ( likes )
2018-09-13 11:33:48 +09:00
}
cond = cond . And ( keywordCond )
2017-10-11 05:37:18 +09:00
}
2017-10-27 06:16:13 +09:00
if opts . Fork != util . OptionalBoolNone {
cond = cond . And ( builder . Eq { "is_fork" : opts . Fork == util . OptionalBoolTrue } )
}
if opts . Mirror != util . OptionalBoolNone {
cond = cond . And ( builder . Eq { "is_mirror" : opts . Mirror == util . OptionalBoolTrue } )
}
2017-02-27 09:45:03 +09:00
if len ( opts . OrderBy ) == 0 {
2017-09-22 21:53:21 +09:00
opts . OrderBy = SearchOrderByAlphabetically
2017-02-27 09:45:03 +09:00
}
2019-11-12 00:15:29 +09:00
if opts . PriorityOwnerID > 0 {
opts . OrderBy = SearchOrderBy ( fmt . Sprintf ( "CASE WHEN owner_id = %d THEN 0 ELSE owner_id END, %s" , opts . PriorityOwnerID , opts . OrderBy ) )
}
2017-08-23 10:30:54 +09:00
sess := x . NewSession ( )
defer sess . Close ( )
2017-10-18 00:20:22 +09:00
count , err := sess .
Where ( cond ) .
Count ( new ( Repository ) )
2018-09-13 11:33:48 +09:00
2017-10-18 00:20:22 +09:00
if err != nil {
return nil , 0 , fmt . Errorf ( "Count: %v" , err )
}
repos := make ( RepositoryList , 0 , opts . PageSize )
2017-02-27 09:45:03 +09:00
if err = sess .
2017-08-23 10:30:54 +09:00
Where ( cond ) .
2018-10-31 06:48:37 +09:00
OrderBy ( opts . OrderBy . String ( ) ) .
2017-02-27 09:45:03 +09:00
Limit ( opts . PageSize , ( opts . Page - 1 ) * opts . PageSize ) .
Find ( & repos ) ; err != nil {
return nil , 0 , fmt . Errorf ( "Repo: %v" , err )
}
if ! opts . IsProfile {
2017-08-23 10:30:54 +09:00
if err = repos . loadAttributes ( sess ) ; err != nil {
2017-02-27 09:45:03 +09:00
return nil , 0 , fmt . Errorf ( "LoadAttributes: %v" , err )
}
}
2017-10-18 00:20:22 +09:00
return repos , count , nil
2017-02-27 09:45:03 +09:00
}
2018-03-16 23:04:33 +09:00
2019-10-29 03:31:55 +09:00
// accessibleRepositoryCondition takes a user a returns a condition for checking if a repository is accessible
func accessibleRepositoryCondition ( userID int64 ) builder . Cond {
return builder . Or (
// 1. Be able to see all non-private repositories that either:
builder . And (
builder . Eq { "`repository`.is_private" : false } ,
builder . Or (
// A. Aren't in organisations __OR__
builder . NotIn ( "`repository`.owner_id" , builder . Select ( "id" ) . From ( "`user`" ) . Where ( builder . Eq { "type" : UserTypeOrganization } ) ) ,
// B. Isn't a private organisation. (Limited is OK because we're logged in)
builder . NotIn ( "`repository`.owner_id" , builder . Select ( "id" ) . From ( "`user`" ) . Where ( builder . Eq { "visibility" : structs . VisibleTypePrivate } ) ) ) ,
) ,
// 2. Be able to see all repositories that we have access to
2019-11-12 00:15:29 +09:00
builder . Or (
builder . In ( "`repository`.id" , builder . Select ( "repo_id" ) .
From ( "`access`" ) .
Where ( builder . And (
builder . Eq { "user_id" : userID } ,
builder . Gt { "mode" : int ( AccessModeNone ) } ) ) ) ,
builder . In ( "`repository`.id" , builder . Select ( "id" ) .
From ( "`repository`" ) .
Where ( builder . Eq { "owner_id" : userID } ) ) ) ,
2019-10-29 03:31:55 +09:00
// 3. Be able to see all repositories that we are in a team
builder . In ( "`repository`.id" , builder . Select ( "`team_repo`.repo_id" ) .
From ( "team_repo" ) .
Where ( builder . Eq { "`team_user`.uid" : userID } ) .
Join ( "INNER" , "team_user" , "`team_user`.team_id = `team_repo`.team_id" ) ) )
}
2019-08-26 02:06:36 +09:00
// SearchRepositoryByName takes keyword and part of repository name to search,
// it returns results in given range and number of total results.
func SearchRepositoryByName ( opts * SearchRepoOptions ) ( RepositoryList , int64 , error ) {
opts . IncludeDescription = false
return SearchRepository ( opts )
}
2018-03-16 23:04:33 +09:00
// FindUserAccessibleRepoIDs find all accessible repositories' ID by user's id
func FindUserAccessibleRepoIDs ( userID int64 ) ( [ ] int64 , error ) {
var accessCond builder . Cond = builder . Eq { "is_private" : false }
if userID > 0 {
accessCond = accessCond . Or (
builder . Eq { "owner_id" : userID } ,
builder . And (
builder . Expr ( "id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?)" , userID ) ,
builder . Neq { "owner_id" : userID } ,
) ,
)
}
repoIDs := make ( [ ] int64 , 0 , 10 )
if err := x .
Table ( "repository" ) .
Cols ( "id" ) .
Where ( accessCond ) .
Find ( & repoIDs ) ; err != nil {
return nil , fmt . Errorf ( "FindUserAccesibleRepoIDs: %v" , err )
}
return repoIDs , nil
}