Remove redudant functions and code (#2652)

* Remove redudant functions and code
This commit is contained in:
Morlinest 2017-10-10 22:37:18 +02:00 committed by Kim "BKC" Carlbäcker
parent c2346e4469
commit dff26e25ca
5 changed files with 57 additions and 117 deletions

View File

@ -27,9 +27,11 @@ func InitIssueIndexer() {
func populateIssueIndexer() error { func populateIssueIndexer() error {
batch := indexer.IssueIndexerBatch() batch := indexer.IssueIndexerBatch()
for page := 1; ; page++ { for page := 1; ; page++ {
repos, _, err := Repositories(&SearchRepoOptions{ repos, _, err := SearchRepositoryByName(&SearchRepoOptions{
Page: page, Page: page,
PageSize: 10, PageSize: 10,
OrderBy: SearchOrderByID,
Private: true,
}) })
if err != nil { if err != nil {
return fmt.Errorf("Repositories: %v", err) return fmt.Errorf("Repositories: %v", err)

View File

@ -105,6 +105,7 @@ type SearchRepoOptions struct {
Starred bool `json:"-"` Starred bool `json:"-"`
Page int `json:"-"` Page int `json:"-"`
IsProfile bool `json:"-"` IsProfile bool `json:"-"`
AllPublic bool `json:"-"` // Include also all public repositories
// Limit of result // Limit of result
// //
// maximum: setting.ExplorePagingNum // maximum: setting.ExplorePagingNum
@ -129,6 +130,8 @@ const (
SearchOrderByNewest = "created_unix DESC" SearchOrderByNewest = "created_unix DESC"
SearchOrderBySize = "size ASC" SearchOrderBySize = "size ASC"
SearchOrderBySizeReverse = "size DESC" SearchOrderBySizeReverse = "size DESC"
SearchOrderByID = "id ASC"
SearchOrderByIDReverse = "id DESC"
) )
// SearchRepositoryByName takes keyword and part of repository name to search, // SearchRepositoryByName takes keyword and part of repository name to search,
@ -147,11 +150,6 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
starJoin = true starJoin = true
} }
opts.Keyword = strings.ToLower(opts.Keyword)
if opts.Keyword != "" {
cond = cond.And(builder.Like{"lower_name", opts.Keyword})
}
// Append conditions // Append conditions
if !opts.Starred && opts.OwnerID > 0 { if !opts.Starred && opts.OwnerID > 0 {
var searcherReposCond builder.Cond = builder.Eq{"owner_id": opts.OwnerID} var searcherReposCond builder.Cond = builder.Eq{"owner_id": opts.OwnerID}
@ -182,6 +180,15 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
cond = cond.And(builder.Eq{"is_private": false}) cond = cond.And(builder.Eq{"is_private": false})
} }
if opts.OwnerID > 0 && opts.AllPublic {
cond = cond.Or(builder.Eq{"is_private": false})
}
opts.Keyword = strings.ToLower(opts.Keyword)
if opts.Keyword != "" {
cond = cond.And(builder.Like{"lower_name", opts.Keyword})
}
if len(opts.OrderBy) == 0 { if len(opts.OrderBy) == 0 {
opts.OrderBy = SearchOrderByAlphabetically opts.OrderBy = SearchOrderByAlphabetically
} }
@ -225,78 +232,3 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
return return
} }
// Repositories returns all repositories
func Repositories(opts *SearchRepoOptions) (_ RepositoryList, count int64, err error) {
if len(opts.OrderBy) == 0 {
opts.OrderBy = "id ASC"
}
repos := make(RepositoryList, 0, opts.PageSize)
if err = x.
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
OrderBy(opts.OrderBy.String()).
Find(&repos); err != nil {
return nil, 0, fmt.Errorf("Repo: %v", err)
}
if err = repos.loadAttributes(x); err != nil {
return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
}
count = countRepositories(-1, opts.Private)
return repos, count, nil
}
// GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList, _ int64, _ error) {
var cond = builder.NewCond()
if len(opts.OrderBy) == 0 {
opts.OrderBy = SearchOrderByRecentUpdated
}
if !opts.Private {
cond = builder.Eq{
"is_private": false,
}
}
if opts.Searcher != nil && !opts.Searcher.IsAdmin {
var ownerIds []int64
ownerIds = append(ownerIds, opts.Searcher.ID)
err := opts.Searcher.GetOrganizations(true)
if err != nil {
return nil, 0, fmt.Errorf("Organization: %v", err)
}
for _, org := range opts.Searcher.Orgs {
ownerIds = append(ownerIds, org.ID)
}
cond = cond.Or(builder.In("owner_id", ownerIds))
}
count, err := x.Where(cond).Count(new(Repository))
if err != nil {
return nil, 0, fmt.Errorf("Count: %v", err)
}
if err = x.Where(cond).
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
Limit(opts.PageSize).
OrderBy(opts.OrderBy.String()).
Find(&repos); err != nil {
return nil, 0, fmt.Errorf("Repo: %v", err)
}
if err = repos.loadAttributes(x); err != nil {
return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
}
return repos, count, nil
}

View File

@ -98,6 +98,24 @@ func TestSearchRepositoryByName(t *testing.T) {
{name: "PublicAndPrivateRepositoriesOfOrganization", {name: "PublicAndPrivateRepositoriesOfOrganization",
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Private: true}, opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Private: true},
count: 2}, count: 2},
{name: "AllPublic/PublicRepositoriesByName",
opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10, AllPublic: true},
count: 4},
{name: "AllPublic/PublicAndPrivateRepositoriesByName",
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true, AllPublic: true},
count: 8},
{name: "AllPublic/PublicRepositoriesOfUserIncludingCollaborative",
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Collaborate: true, AllPublic: true},
count: 12},
{name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true, AllPublic: true},
count: 16},
{name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborativeByName",
opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true, AllPublic: true},
count: 10},
{name: "AllPublic/PublicRepositoriesOfOrganization",
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, AllPublic: true},
count: 12},
} }
for _, testCase := range testCases { for _, testCase := range testCases {
@ -126,7 +144,7 @@ func TestSearchRepositoryByName(t *testing.T) {
} }
// FIXME: Can't check, need to fix current behaviour (see previous FIXME comments in test cases) // FIXME: Can't check, need to fix current behaviour (see previous FIXME comments in test cases)
/*if testCase.opts.OwnerID > 0 && !testCase.opts.Collaborate { /*if testCase.opts.OwnerID > 0 && !testCase.opts.Collaborate && !AllPublic {
assert.Equal(t, testCase.opts.OwnerID, repo.Owner.ID) assert.Equal(t, testCase.opts.OwnerID, repo.Owner.ID)
}*/ }*/

View File

@ -24,7 +24,6 @@ func Repos(ctx *context.Context) {
ctx.Data["PageIsAdminRepositories"] = true ctx.Data["PageIsAdminRepositories"] = true
routers.RenderRepoSearch(ctx, &routers.RepoSearchOptions{ routers.RenderRepoSearch(ctx, &routers.RepoSearchOptions{
Ranger: models.Repositories,
Private: true, Private: true,
PageSize: setting.UI.Admin.RepoPagingNum, PageSize: setting.UI.Admin.RepoPagingNum,
TplName: tplRepos, TplName: tplRepos,

View File

@ -60,8 +60,7 @@ func Swagger(ctx *context.Context) {
// RepoSearchOptions when calling search repositories // RepoSearchOptions when calling search repositories
type RepoSearchOptions struct { type RepoSearchOptions struct {
Ranger func(*models.SearchRepoOptions) (models.RepositoryList, int64, error) OwnerID int64
Searcher *models.User
Private bool Private bool
PageSize int PageSize int
TplName base.TplName TplName base.TplName
@ -113,35 +112,21 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
} }
keyword := strings.Trim(ctx.Query("q"), " ") keyword := strings.Trim(ctx.Query("q"), " ")
if len(keyword) == 0 {
repos, count, err = opts.Ranger(&models.SearchRepoOptions{ repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
Page: page, Page: page,
PageSize: opts.PageSize, PageSize: opts.PageSize,
Searcher: ctx.User, OrderBy: orderBy,
OrderBy: orderBy, Private: opts.Private,
Private: opts.Private, Keyword: keyword,
Collaborate: true, OwnerID: opts.OwnerID,
}) Searcher: ctx.User,
if err != nil { Collaborate: true,
ctx.Handle(500, "opts.Ranger", err) AllPublic: true,
return })
} if err != nil {
} else { ctx.Handle(500, "SearchRepositoryByName", err)
if isKeywordValid(keyword) { return
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
Keyword: keyword,
OrderBy: orderBy,
Private: opts.Private,
Page: page,
PageSize: opts.PageSize,
Searcher: ctx.User,
Collaborate: true,
})
if err != nil {
ctx.Handle(500, "SearchRepositoryByName", err)
return
}
}
} }
ctx.Data["Keyword"] = keyword ctx.Data["Keyword"] = keyword
ctx.Data["Total"] = count ctx.Data["Total"] = count
@ -157,11 +142,15 @@ func ExploreRepos(ctx *context.Context) {
ctx.Data["PageIsExplore"] = true ctx.Data["PageIsExplore"] = true
ctx.Data["PageIsExploreRepositories"] = true ctx.Data["PageIsExploreRepositories"] = true
var ownerID int64
if ctx.User != nil && !ctx.User.IsAdmin {
ownerID = ctx.User.ID
}
RenderRepoSearch(ctx, &RepoSearchOptions{ RenderRepoSearch(ctx, &RepoSearchOptions{
Ranger: models.GetRecentUpdatedRepositories,
PageSize: setting.UI.ExplorePagingNum, PageSize: setting.UI.ExplorePagingNum,
Searcher: ctx.User, OwnerID: ownerID,
Private: ctx.User != nil && ctx.User.IsAdmin, Private: ctx.User != nil,
TplName: tplExploreRepos, TplName: tplExploreRepos,
}) })
} }