Browse Source

Remove redudant functions and code (#2652)

* Remove redudant functions and code
Morlinest 2 years ago
parent
commit
dff26e25ca
5 changed files with 57 additions and 117 deletions
  1. 3 1
      models/issue_indexer.go
  2. 12 80
      models/repo_list.go
  3. 19 1
      models/repo_list_test.go
  4. 0 1
      routers/admin/repos.go
  5. 23 34
      routers/home.go

+ 3 - 1
models/issue_indexer.go

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

+ 12 - 80
models/repo_list.go

@@ -105,6 +105,7 @@ type SearchRepoOptions struct {
105 105
 	Starred     bool          `json:"-"`
106 106
 	Page        int           `json:"-"`
107 107
 	IsProfile   bool          `json:"-"`
108
+	AllPublic   bool          `json:"-"` // Include also all public repositories
108 109
 	// Limit of result
109 110
 	//
110 111
 	// maximum: setting.ExplorePagingNum
@@ -129,6 +130,8 @@ const (
129 130
 	SearchOrderByNewest                              = "created_unix DESC"
130 131
 	SearchOrderBySize                                = "size ASC"
131 132
 	SearchOrderBySizeReverse                         = "size DESC"
133
+	SearchOrderByID                                  = "id ASC"
134
+	SearchOrderByIDReverse                           = "id DESC"
132 135
 )
133 136
 
134 137
 // SearchRepositoryByName takes keyword and part of repository name to search,
@@ -147,11 +150,6 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
147 150
 		starJoin = true
148 151
 	}
149 152
 
150
-	opts.Keyword = strings.ToLower(opts.Keyword)
151
-	if opts.Keyword != "" {
152
-		cond = cond.And(builder.Like{"lower_name", opts.Keyword})
153
-	}
154
-
155 153
 	// Append conditions
156 154
 	if !opts.Starred && opts.OwnerID > 0 {
157 155
 		var searcherReposCond builder.Cond = builder.Eq{"owner_id": opts.OwnerID}
@@ -182,6 +180,15 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
182 180
 		cond = cond.And(builder.Eq{"is_private": false})
183 181
 	}
184 182
 
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
+
185 192
 	if len(opts.OrderBy) == 0 {
186 193
 		opts.OrderBy = SearchOrderByAlphabetically
187 194
 	}
@@ -225,78 +232,3 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
225 232
 
226 233
 	return
227 234
 }
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
-}

+ 19 - 1
models/repo_list_test.go

@@ -98,6 +98,24 @@ func TestSearchRepositoryByName(t *testing.T) {
98 98
 		{name: "PublicAndPrivateRepositoriesOfOrganization",
99 99
 			opts:  &SearchRepoOptions{Page: 1, PageSize: 10, OwnerID: 17, Private: true},
100 100
 			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},
101 119
 	}
102 120
 
103 121
 	for _, testCase := range testCases {
@@ -126,7 +144,7 @@ func TestSearchRepositoryByName(t *testing.T) {
126 144
 				}
127 145
 
128 146
 				// 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 {
130 148
 					assert.Equal(t, testCase.opts.OwnerID, repo.Owner.ID)
131 149
 				}*/
132 150
 

+ 0 - 1
routers/admin/repos.go

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

+ 23 - 34
routers/home.go

@@ -60,8 +60,7 @@ func Swagger(ctx *context.Context) {
60 60
 
61 61
 // RepoSearchOptions when calling search repositories
62 62
 type RepoSearchOptions struct {
63
-	Ranger   func(*models.SearchRepoOptions) (models.RepositoryList, int64, error)
64
-	Searcher *models.User
63
+	OwnerID  int64
65 64
 	Private  bool
66 65
 	PageSize int
67 66
 	TplName  base.TplName
@@ -113,35 +112,21 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
113 112
 	}
114 113
 
115 114
 	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
145 130
 	}
146 131
 	ctx.Data["Keyword"] = keyword
147 132
 	ctx.Data["Total"] = count
@@ -157,11 +142,15 @@ func ExploreRepos(ctx *context.Context) {
157 142
 	ctx.Data["PageIsExplore"] = true
158 143
 	ctx.Data["PageIsExploreRepositories"] = true
159 144
 
145
+	var ownerID int64
146
+	if ctx.User != nil && !ctx.User.IsAdmin {
147
+		ownerID = ctx.User.ID
148
+	}
149
+
160 150
 	RenderRepoSearch(ctx, &RepoSearchOptions{
161
-		Ranger:   models.GetRecentUpdatedRepositories,
162 151
 		PageSize: setting.UI.ExplorePagingNum,
163
-		Searcher: ctx.User,
164
-		Private:  ctx.User != nil && ctx.User.IsAdmin,
152
+		OwnerID:  ownerID,
153
+		Private:  ctx.User != nil,
165 154
 		TplName:  tplExploreRepos,
166 155
 	})
167 156
 }