Skip to content

Commit dff26e2

Browse files
Morlinestbkcsoft
authored andcommitted
Remove redudant functions and code (#2652)
* Remove redudant functions and code
1 parent c2346e4 commit dff26e2

File tree

5 files changed

+57
-117
lines changed

5 files changed

+57
-117
lines changed

models/issue_indexer.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ func InitIssueIndexer() {
2727
func populateIssueIndexer() error {
2828
batch := indexer.IssueIndexerBatch()
2929
for page := 1; ; page++ {
30-
repos, _, err := Repositories(&SearchRepoOptions{
30+
repos, _, err := SearchRepositoryByName(&SearchRepoOptions{
3131
Page: page,
3232
PageSize: 10,
33+
OrderBy: SearchOrderByID,
34+
Private: true,
3335
})
3436
if err != nil {
3537
return fmt.Errorf("Repositories: %v", err)

models/repo_list.go

Lines changed: 12 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ type SearchRepoOptions struct {
105105
Starred bool `json:"-"`
106106
Page int `json:"-"`
107107
IsProfile bool `json:"-"`
108+
AllPublic bool `json:"-"` // Include also all public repositories
108109
// Limit of result
109110
//
110111
// maximum: setting.ExplorePagingNum
@@ -129,6 +130,8 @@ const (
129130
SearchOrderByNewest = "created_unix DESC"
130131
SearchOrderBySize = "size ASC"
131132
SearchOrderBySizeReverse = "size DESC"
133+
SearchOrderByID = "id ASC"
134+
SearchOrderByIDReverse = "id DESC"
132135
)
133136

134137
// SearchRepositoryByName takes keyword and part of repository name to search,
@@ -147,11 +150,6 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
147150
starJoin = true
148151
}
149152

150-
opts.Keyword = strings.ToLower(opts.Keyword)
151-
if opts.Keyword != "" {
152-
cond = cond.And(builder.Like{"lower_name", opts.Keyword})
153-
}
154-
155153
// Append conditions
156154
if !opts.Starred && opts.OwnerID > 0 {
157155
var searcherReposCond builder.Cond = builder.Eq{"owner_id": opts.OwnerID}
@@ -182,6 +180,15 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
182180
cond = cond.And(builder.Eq{"is_private": false})
183181
}
184182

183+
if opts.OwnerID > 0 && opts.AllPublic {
184+
cond = cond.Or(builder.Eq{"is_private": false})
185+
}
186+
187+
opts.Keyword = strings.ToLower(opts.Keyword)
188+
if opts.Keyword != "" {
189+
cond = cond.And(builder.Like{"lower_name", opts.Keyword})
190+
}
191+
185192
if len(opts.OrderBy) == 0 {
186193
opts.OrderBy = SearchOrderByAlphabetically
187194
}
@@ -225,78 +232,3 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
225232

226233
return
227234
}
228-
229-
// Repositories returns all repositories
230-
func Repositories(opts *SearchRepoOptions) (_ RepositoryList, count int64, err error) {
231-
if len(opts.OrderBy) == 0 {
232-
opts.OrderBy = "id ASC"
233-
}
234-
235-
repos := make(RepositoryList, 0, opts.PageSize)
236-
237-
if err = x.
238-
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
239-
OrderBy(opts.OrderBy.String()).
240-
Find(&repos); err != nil {
241-
return nil, 0, fmt.Errorf("Repo: %v", err)
242-
}
243-
244-
if err = repos.loadAttributes(x); err != nil {
245-
return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
246-
}
247-
248-
count = countRepositories(-1, opts.Private)
249-
250-
return repos, count, nil
251-
}
252-
253-
// GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
254-
func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList, _ int64, _ error) {
255-
var cond = builder.NewCond()
256-
257-
if len(opts.OrderBy) == 0 {
258-
opts.OrderBy = SearchOrderByRecentUpdated
259-
}
260-
261-
if !opts.Private {
262-
cond = builder.Eq{
263-
"is_private": false,
264-
}
265-
}
266-
267-
if opts.Searcher != nil && !opts.Searcher.IsAdmin {
268-
var ownerIds []int64
269-
270-
ownerIds = append(ownerIds, opts.Searcher.ID)
271-
err := opts.Searcher.GetOrganizations(true)
272-
273-
if err != nil {
274-
return nil, 0, fmt.Errorf("Organization: %v", err)
275-
}
276-
277-
for _, org := range opts.Searcher.Orgs {
278-
ownerIds = append(ownerIds, org.ID)
279-
}
280-
281-
cond = cond.Or(builder.In("owner_id", ownerIds))
282-
}
283-
284-
count, err := x.Where(cond).Count(new(Repository))
285-
if err != nil {
286-
return nil, 0, fmt.Errorf("Count: %v", err)
287-
}
288-
289-
if err = x.Where(cond).
290-
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
291-
Limit(opts.PageSize).
292-
OrderBy(opts.OrderBy.String()).
293-
Find(&repos); err != nil {
294-
return nil, 0, fmt.Errorf("Repo: %v", err)
295-
}
296-
297-
if err = repos.loadAttributes(x); err != nil {
298-
return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
299-
}
300-
301-
return repos, count, nil
302-
}

models/repo_list_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ func TestSearchRepositoryByName(t *testing.T) {
9898
{name: "PublicAndPrivateRepositoriesOfOrganization",
9999
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Private: true},
100100
count: 2},
101+
{name: "AllPublic/PublicRepositoriesByName",
102+
opts: &SearchRepoOptions{Keyword: "big_test_", PageSize: 10, AllPublic: true},
103+
count: 4},
104+
{name: "AllPublic/PublicAndPrivateRepositoriesByName",
105+
opts: &SearchRepoOptions{Keyword: "big_test_", Page: 1, PageSize: 10, Private: true, AllPublic: true},
106+
count: 8},
107+
{name: "AllPublic/PublicRepositoriesOfUserIncludingCollaborative",
108+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Collaborate: true, AllPublic: true},
109+
count: 12},
110+
{name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
111+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true, AllPublic: true},
112+
count: 16},
113+
{name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborativeByName",
114+
opts: &SearchRepoOptions{Keyword: "test", Page: 1, PageSize: 10, OwnerID: 15, Private: true, Collaborate: true, AllPublic: true},
115+
count: 10},
116+
{name: "AllPublic/PublicRepositoriesOfOrganization",
117+
opts: &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, AllPublic: true},
118+
count: 12},
101119
}
102120

103121
for _, testCase := range testCases {
@@ -126,7 +144,7 @@ func TestSearchRepositoryByName(t *testing.T) {
126144
}
127145

128146
// FIXME: Can't check, need to fix current behaviour (see previous FIXME comments in test cases)
129-
/*if testCase.opts.OwnerID > 0 && !testCase.opts.Collaborate {
147+
/*if testCase.opts.OwnerID > 0 && !testCase.opts.Collaborate && !AllPublic {
130148
assert.Equal(t, testCase.opts.OwnerID, repo.Owner.ID)
131149
}*/
132150

routers/admin/repos.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ func Repos(ctx *context.Context) {
2424
ctx.Data["PageIsAdminRepositories"] = true
2525

2626
routers.RenderRepoSearch(ctx, &routers.RepoSearchOptions{
27-
Ranger: models.Repositories,
2827
Private: true,
2928
PageSize: setting.UI.Admin.RepoPagingNum,
3029
TplName: tplRepos,

routers/home.go

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ func Swagger(ctx *context.Context) {
6060

6161
// RepoSearchOptions when calling search repositories
6262
type RepoSearchOptions struct {
63-
Ranger func(*models.SearchRepoOptions) (models.RepositoryList, int64, error)
64-
Searcher *models.User
63+
OwnerID int64
6564
Private bool
6665
PageSize int
6766
TplName base.TplName
@@ -113,35 +112,21 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
113112
}
114113

115114
keyword := strings.Trim(ctx.Query("q"), " ")
116-
if len(keyword) == 0 {
117-
repos, count, err = opts.Ranger(&models.SearchRepoOptions{
118-
Page: page,
119-
PageSize: opts.PageSize,
120-
Searcher: ctx.User,
121-
OrderBy: orderBy,
122-
Private: opts.Private,
123-
Collaborate: true,
124-
})
125-
if err != nil {
126-
ctx.Handle(500, "opts.Ranger", err)
127-
return
128-
}
129-
} else {
130-
if isKeywordValid(keyword) {
131-
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
132-
Keyword: keyword,
133-
OrderBy: orderBy,
134-
Private: opts.Private,
135-
Page: page,
136-
PageSize: opts.PageSize,
137-
Searcher: ctx.User,
138-
Collaborate: true,
139-
})
140-
if err != nil {
141-
ctx.Handle(500, "SearchRepositoryByName", err)
142-
return
143-
}
144-
}
115+
116+
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
117+
Page: page,
118+
PageSize: opts.PageSize,
119+
OrderBy: orderBy,
120+
Private: opts.Private,
121+
Keyword: keyword,
122+
OwnerID: opts.OwnerID,
123+
Searcher: ctx.User,
124+
Collaborate: true,
125+
AllPublic: true,
126+
})
127+
if err != nil {
128+
ctx.Handle(500, "SearchRepositoryByName", err)
129+
return
145130
}
146131
ctx.Data["Keyword"] = keyword
147132
ctx.Data["Total"] = count
@@ -157,11 +142,15 @@ func ExploreRepos(ctx *context.Context) {
157142
ctx.Data["PageIsExplore"] = true
158143
ctx.Data["PageIsExploreRepositories"] = true
159144

145+
var ownerID int64
146+
if ctx.User != nil && !ctx.User.IsAdmin {
147+
ownerID = ctx.User.ID
148+
}
149+
160150
RenderRepoSearch(ctx, &RepoSearchOptions{
161-
Ranger: models.GetRecentUpdatedRepositories,
162151
PageSize: setting.UI.ExplorePagingNum,
163-
Searcher: ctx.User,
164-
Private: ctx.User != nil && ctx.User.IsAdmin,
152+
OwnerID: ownerID,
153+
Private: ctx.User != nil,
165154
TplName: tplExploreRepos,
166155
})
167156
}

0 commit comments

Comments
 (0)