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"
2019-02-18 21:30:27 +05:30
"code.gitea.io/gitea/modules/structs"
2017-10-27 02:46:13 +05:30
"code.gitea.io/gitea/modules/util"
2019-06-23 20:52:43 +05:30
"xorm.io/builder"
2017-02-27 06:15:03 +05:30
)
2017-12-31 20:15:46 +05:30
// 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 06:15:03 +05:30
// RepositoryList contains a list of repositories
type RepositoryList [ ] * Repository
2017-12-04 10:09:01 +05:30
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 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
}
2017-03-11 14:20:12 +05:30
set := make ( map [ int64 ] struct { } )
2020-02-11 15:04:17 +05:30
repoIDs := make ( [ ] int64 , len ( repos ) )
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 { } { }
2020-02-11 15:04:17 +05:30
repoIDs [ i ] = repos [ i ] . ID
2017-02-27 06:15:03 +05:30
}
2020-02-11 15:04:17 +05:30
// Load owners.
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
}
2020-02-11 15:04:17 +05:30
// Load primary language.
stats := make ( LanguageStatList , 0 , len ( repos ) )
if err := e .
Where ( "`is_primary` = ? AND `language` != ?" , true , "other" ) .
In ( "`repo_id`" , repoIDs ) .
Find ( & stats ) ; err != nil {
return fmt . Errorf ( "find primary languages: %v" , err )
}
stats . loadAttributes ( )
for i := range repos {
for _ , st := range stats {
if st . RepoID == repos [ i ] . ID {
repos [ i ] . PrimaryLanguage = st
break
}
}
}
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
type SearchRepoOptions struct {
2020-01-25 00:30:29 +05:30
ListOptions
2020-01-13 23:03:46 +05:30
Actor * User
2019-11-11 20:45:29 +05:30
Keyword string
OwnerID int64
PriorityOwnerID int64
OrderBy SearchOrderBy
Private bool // Include private repositories in results
2020-05-17 01:37:01 +05:30
OnlyPrivate bool // Include only private repositories in results
2019-11-11 20:45:29 +05:30
StarredByID int64
2020-01-06 00:18:47 +05:30
AllPublic bool // Include also all public repositories of users and public organisations
AllLimited bool // Include also all public repositories of limited organisations
2017-10-27 02:46:13 +05:30
// 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-11 20:45:29 +05:30
// None -> include templates AND non-templates
// True -> include just templates
// False -> include just non-templates
Template util . OptionalBool
2017-10-27 02:46:13 +05:30
// None -> include mirrors AND non-mirrors
// True -> include just mirrors
// False -> include just non-mirrors
Mirror util . OptionalBool
2020-05-17 01:37:01 +05:30
// None -> include archived AND non-archived
// True -> include just archived
// False -> include just non-archived
Archived util . OptionalBool
2018-09-13 08:03:48 +05:30
// only search topic name
TopicOnly bool
2019-08-25 22:36:36 +05:30
// include description in keyword search
IncludeDescription bool
2020-03-31 13:17:00 +05:30
// None -> include has milestones AND has no milestone
// True -> include just has milestones
// False -> include just has no milestone
HasMilestones util . OptionalBool
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"
2019-06-13 01:11:28 +05:30
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 18:23:21 +05:30
)
2020-03-29 10:57:06 +05:30
// SearchRepositoryCondition creates a query condition according search repository options
func SearchRepositoryCondition ( opts * SearchRepoOptions ) builder . Cond {
2017-10-17 20:50:22 +05:30
var cond = builder . NewCond ( )
2017-08-24 19:31:03 +05:30
2019-05-15 20:54:39 +05:30
if opts . Private {
2020-01-13 23:03:46 +05:30
if opts . Actor != nil && ! opts . Actor . IsAdmin && opts . Actor . ID != opts . OwnerID {
2019-05-15 20:54:39 +05:30
// OK we're in the context of a User
2020-01-13 23:03:46 +05:30
cond = cond . And ( accessibleRepositoryCondition ( opts . Actor ) )
2019-05-15 20:54:39 +05:30
}
} else {
// Not looking at private organisations
2020-05-17 01:37:01 +05:30
// We should be able to see all non-private repositories that
// isn't in a private or limited organisation.
cond = cond . And (
builder . Eq { "is_private" : false } ,
builder . NotIn ( "owner_id" , builder . Select ( "id" ) . From ( "`user`" ) . Where (
builder . And (
builder . Eq { "type" : UserTypeOrganization } ,
builder . Or ( builder . Eq { "visibility" : structs . VisibleTypeLimited } , builder . Eq { "visibility" : structs . VisibleTypePrivate } ) ,
) ) ) )
}
if opts . OnlyPrivate {
cond = cond . And (
builder . Or (
builder . Eq { "is_private" : true } ,
builder . In ( "owner_id" , builder . Select ( "id" ) . From ( "`user`" ) . Where (
builder . And (
builder . Eq { "type" : UserTypeOrganization } ,
builder . Or ( builder . Eq { "visibility" : structs . VisibleTypeLimited } , builder . Eq { "visibility" : structs . VisibleTypePrivate } ) ,
) ) ) ) )
2017-10-17 20:50:22 +05:30
}
2017-08-24 19:31:03 +05:30
2019-11-11 20:45:29 +05:30
if opts . Template != util . OptionalBoolNone {
cond = cond . And ( builder . Eq { "is_template" : opts . Template == util . OptionalBoolTrue } )
}
2019-05-15 20:54:39 +05:30
// 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-17 20:50:22 +05:30
if opts . OwnerID > 0 {
2019-05-15 20:54:39 +05:30
var accessCond = builder . NewCond ( )
if opts . Collaborate != util . OptionalBoolTrue {
accessCond = builder . Eq { "owner_id" : opts . OwnerID }
}
2017-08-24 19:31:03 +05:30
2019-05-15 20:54:39 +05:30
if opts . Collaborate != util . OptionalBoolFalse {
collaborateCond := builder . And (
builder . Or (
2018-09-13 08:03:48 +05:30
builder . Expr ( "repository.id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?)" , opts . OwnerID ) ,
2019-05-15 20:54:39 +05:30
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 19:31:03 +05:30
}
2019-05-15 20:54:39 +05:30
accessCond = accessCond . Or ( collaborateCond )
}
2017-10-27 02:46:13 +05:30
2019-05-15 20:54:39 +05:30
if opts . AllPublic {
2020-01-06 00:18:47 +05:30
accessCond = accessCond . Or ( builder . Eq { "is_private" : false } . And ( builder . In ( "owner_id" , builder . Select ( "`user`.id" ) . From ( "`user`" ) . Where ( builder . Eq { "`user`.visibility" : structs . VisibleTypePublic } ) ) ) )
}
if opts . AllLimited {
accessCond = accessCond . Or ( builder . Eq { "is_private" : false } . And ( builder . In ( "owner_id" , builder . Select ( "`user`.id" ) . From ( "`user`" ) . Where ( builder . Eq { "`user`.visibility" : structs . VisibleTypeLimited } ) ) ) )
2017-10-17 20:50:22 +05:30
}
2019-05-15 20:54:39 +05:30
cond = cond . And ( accessCond )
2017-02-27 06:15:03 +05:30
}
2017-10-11 02:07:18 +05:30
if opts . Keyword != "" {
2018-10-18 08:44:28 +05:30
// separate keyword
2018-10-31 03:18:37 +05:30
var subQueryCond = builder . NewCond ( )
2018-10-18 08:44:28 +05:30
for _ , v := range strings . Split ( opts . Keyword , "," ) {
2019-08-23 08:07:35 +05:30
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 03:18:37 +05:30
}
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-25 22:36:36 +05:30
if opts . IncludeDescription {
likes = likes . Or ( builder . Like { "LOWER(description)" , strings . ToLower ( v ) } )
}
2018-10-18 08:44:28 +05:30
}
2018-10-31 03:18:37 +05:30
keywordCond = keywordCond . Or ( likes )
2018-09-13 08:03:48 +05:30
}
cond = cond . And ( keywordCond )
2017-10-11 02:07:18 +05:30
}
2017-10-27 02:46:13 +05:30
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 } )
}
2020-01-13 23:03:46 +05:30
if opts . Actor != nil && opts . Actor . IsRestricted {
cond = cond . And ( accessibleRepositoryCondition ( opts . Actor ) )
}
2020-03-31 13:17:00 +05:30
2020-05-17 01:37:01 +05:30
if opts . Archived != util . OptionalBoolNone {
cond = cond . And ( builder . Eq { "is_archived" : opts . Archived == util . OptionalBoolTrue } )
}
2020-03-31 13:17:00 +05:30
switch opts . HasMilestones {
case util . OptionalBoolTrue :
cond = cond . And ( builder . Gt { "num_milestones" : 0 } )
case util . OptionalBoolFalse :
cond = cond . And ( builder . Eq { "num_milestones" : 0 } . Or ( builder . IsNull { "num_milestones" } ) )
}
2020-03-29 10:57:06 +05:30
return cond
}
// SearchRepository returns repositories based on search options,
// it returns results in given range and number of total results.
func SearchRepository ( opts * SearchRepoOptions ) ( RepositoryList , int64 , error ) {
cond := SearchRepositoryCondition ( opts )
2020-03-31 13:17:00 +05:30
return SearchRepositoryByCondition ( opts , cond , true )
}
2020-03-29 10:57:06 +05:30
2020-03-31 13:17:00 +05:30
// SearchRepositoryByCondition search repositories by condition
func SearchRepositoryByCondition ( opts * SearchRepoOptions , cond builder . Cond , loadAttributes bool ) ( RepositoryList , int64 , error ) {
2020-03-29 10:57:06 +05:30
if opts . Page <= 0 {
opts . Page = 1
}
2020-01-13 23:03:46 +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
}
2019-11-11 20:45:29 +05:30
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 07:00:54 +05:30
sess := x . NewSession ( )
defer sess . Close ( )
2017-10-17 20:50:22 +05:30
count , err := sess .
Where ( cond ) .
Count ( new ( Repository ) )
2018-09-13 08:03:48 +05:30
2017-10-17 20:50:22 +05:30
if err != nil {
return nil , 0 , fmt . Errorf ( "Count: %v" , err )
}
repos := make ( RepositoryList , 0 , opts . PageSize )
2020-03-31 13:17:00 +05:30
sess . Where ( cond ) . OrderBy ( opts . OrderBy . String ( ) )
if opts . PageSize > 0 {
sess . Limit ( opts . PageSize , ( opts . Page - 1 ) * opts . PageSize )
}
if err = sess . Find ( & repos ) ; err != nil {
2017-02-27 06:15:03 +05:30
return nil , 0 , fmt . Errorf ( "Repo: %v" , err )
}
2020-03-31 13:17:00 +05:30
if loadAttributes {
if err = repos . loadAttributes ( sess ) ; err != nil {
return nil , 0 , fmt . Errorf ( "LoadAttributes: %v" , err )
}
2017-02-27 06:15:03 +05:30
}
2017-10-17 20:50:22 +05:30
return repos , count , nil
2017-02-27 06:15:03 +05:30
}
2018-03-16 19:34:33 +05:30
2019-10-29 00:01:55 +05:30
// accessibleRepositoryCondition takes a user a returns a condition for checking if a repository is accessible
2020-01-13 23:03:46 +05:30
func accessibleRepositoryCondition ( user * User ) builder . Cond {
var cond = builder . NewCond ( )
2020-01-28 17:09:37 +05:30
if user == nil || ! user . IsRestricted || user . ID <= 0 {
2020-01-13 23:03:46 +05:30
orgVisibilityLimit := [ ] structs . VisibleType { structs . VisibleTypePrivate }
2020-01-28 17:09:37 +05:30
if user == nil || user . ID <= 0 {
2020-01-13 23:03:46 +05:30
orgVisibilityLimit = append ( orgVisibilityLimit , structs . VisibleTypeLimited )
}
2019-10-29 00:01:55 +05:30
// 1. Be able to see all non-private repositories that either:
2020-01-13 23:03:46 +05:30
cond = cond . Or ( builder . And (
2019-10-29 00:01:55 +05:30
builder . Eq { "`repository`.is_private" : false } ,
2020-03-29 20:01:10 +05:30
// 2. Aren't in an private organisation or limited organisation if we're not logged in
builder . NotIn ( "`repository`.owner_id" , builder . Select ( "id" ) . From ( "`user`" ) . Where (
builder . And (
builder . Eq { "type" : UserTypeOrganization } ,
builder . In ( "visibility" , orgVisibilityLimit ) ) ,
) ) ) )
2020-01-13 23:03:46 +05:30
}
if user != nil {
2019-10-29 00:01:55 +05:30
// 2. Be able to see all repositories that we have access to
2020-01-13 23:03:46 +05:30
cond = cond . Or ( builder . Or (
2019-11-11 20:45:29 +05:30
builder . In ( "`repository`.id" , builder . Select ( "repo_id" ) .
From ( "`access`" ) .
Where ( builder . And (
2020-01-13 23:03:46 +05:30
builder . Eq { "user_id" : user . ID } ,
2019-11-11 20:45:29 +05:30
builder . Gt { "mode" : int ( AccessModeNone ) } ) ) ) ,
builder . In ( "`repository`.id" , builder . Select ( "id" ) .
From ( "`repository`" ) .
2020-01-13 23:03:46 +05:30
Where ( builder . Eq { "owner_id" : user . ID } ) ) ) )
2019-10-29 00:01:55 +05:30
// 3. Be able to see all repositories that we are in a team
2020-01-13 23:03:46 +05:30
cond = cond . Or ( builder . In ( "`repository`.id" , builder . Select ( "`team_repo`.repo_id" ) .
2019-10-29 00:01:55 +05:30
From ( "team_repo" ) .
2020-01-13 23:03:46 +05:30
Where ( builder . Eq { "`team_user`.uid" : user . ID } ) .
2019-10-29 00:01:55 +05:30
Join ( "INNER" , "team_user" , "`team_user`.team_id = `team_repo`.team_id" ) ) )
2020-01-13 23:03:46 +05:30
}
return cond
2019-10-29 00:01:55 +05:30
}
2019-08-25 22:36:36 +05:30
// 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 )
}
2020-01-13 23:03:46 +05:30
// AccessibleRepoIDsQuery queries accessible repository ids. Usable as a subquery wherever repo ids need to be filtered.
2020-01-28 17:09:37 +05:30
func AccessibleRepoIDsQuery ( user * User ) * builder . Builder {
// NB: Please note this code needs to still work if user is nil
2020-01-13 23:03:46 +05:30
return builder . Select ( "id" ) . From ( "repository" ) . Where ( accessibleRepositoryCondition ( user ) )
}
2018-03-16 19:34:33 +05:30
2020-01-13 23:03:46 +05:30
// FindUserAccessibleRepoIDs find all accessible repositories' ID by user's id
func FindUserAccessibleRepoIDs ( user * User ) ( [ ] int64 , error ) {
2018-03-16 19:34:33 +05:30
repoIDs := make ( [ ] int64 , 0 , 10 )
if err := x .
Table ( "repository" ) .
Cols ( "id" ) .
2020-01-13 23:03:46 +05:30
Where ( accessibleRepositoryCondition ( user ) ) .
2018-03-16 19:34:33 +05:30
Find ( & repoIDs ) ; err != nil {
return nil , fmt . Errorf ( "FindUserAccesibleRepoIDs: %v" , err )
}
return repoIDs , nil
}