Skip to content

Remove redudant functions and code #2652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion models/issue_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ func InitIssueIndexer() {
func populateIssueIndexer() error {
batch := indexer.IssueIndexerBatch()
for page := 1; ; page++ {
repos, _, err := Repositories(&SearchRepoOptions{
repos, _, err := SearchRepositoryByName(&SearchRepoOptions{
Page: page,
PageSize: 10,
OrderBy: SearchOrderByID,
Private: true,
})
if err != nil {
return fmt.Errorf("Repositories: %v", err)
Expand Down
92 changes: 12 additions & 80 deletions models/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type SearchRepoOptions struct {
Starred bool `json:"-"`
Page int `json:"-"`
IsProfile bool `json:"-"`
AllPublic bool `json:"-"` // Include also all public repositories
// Limit of result
//
// maximum: setting.ExplorePagingNum
Expand All @@ -129,6 +130,8 @@ const (
SearchOrderByNewest = "created_unix DESC"
SearchOrderBySize = "size ASC"
SearchOrderBySizeReverse = "size DESC"
SearchOrderByID = "id ASC"
SearchOrderByIDReverse = "id DESC"
)

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

opts.Keyword = strings.ToLower(opts.Keyword)
if opts.Keyword != "" {
cond = cond.And(builder.Like{"lower_name", opts.Keyword})
}

// Append conditions
if !opts.Starred && opts.OwnerID > 0 {
var searcherReposCond builder.Cond = builder.Eq{"owner_id": opts.OwnerID}
Expand Down Expand Up @@ -182,6 +180,15 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
cond = cond.And(builder.Eq{"is_private": false})
}

if opts.OwnerID > 0 && opts.AllPublic {
cond = builder.Or(cond, 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 {
opts.OrderBy = SearchOrderByAlphabetically
}
Expand Down Expand Up @@ -225,78 +232,3 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun

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
}
1 change: 0 additions & 1 deletion routers/admin/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func Repos(ctx *context.Context) {
ctx.Data["PageIsAdminRepositories"] = true

routers.RenderRepoSearch(ctx, &routers.RepoSearchOptions{
Ranger: models.Repositories,
Private: true,
PageSize: setting.UI.Admin.RepoPagingNum,
TplName: tplRepos,
Expand Down
57 changes: 23 additions & 34 deletions routers/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ func Swagger(ctx *context.Context) {

// RepoSearchOptions when calling search repositories
type RepoSearchOptions struct {
Ranger func(*models.SearchRepoOptions) (models.RepositoryList, int64, error)
Searcher *models.User
OwnerID int64
Private bool
PageSize int
TplName base.TplName
Expand Down Expand Up @@ -113,35 +112,21 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
}

keyword := strings.Trim(ctx.Query("q"), " ")
if len(keyword) == 0 {
repos, count, err = opts.Ranger(&models.SearchRepoOptions{
Page: page,
PageSize: opts.PageSize,
Searcher: ctx.User,
OrderBy: orderBy,
Private: opts.Private,
Collaborate: true,
})
if err != nil {
ctx.Handle(500, "opts.Ranger", err)
return
}
} else {
if isKeywordValid(keyword) {
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
}
}

repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
Page: page,
PageSize: opts.PageSize,
OrderBy: orderBy,
Private: opts.Private,
Keyword: keyword,
OwnerID: opts.OwnerID,
Searcher: ctx.User,
Collaborate: true,
AllPublic: true,
})
if err != nil {
ctx.Handle(500, "SearchRepositoryByName", err)
return
}
ctx.Data["Keyword"] = keyword
ctx.Data["Total"] = count
Expand All @@ -157,11 +142,15 @@ func ExploreRepos(ctx *context.Context) {
ctx.Data["PageIsExplore"] = true
ctx.Data["PageIsExploreRepositories"] = true

var ownerID int64
if ctx.User != nil && !ctx.User.IsAdmin {
ownerID = ctx.User.ID
}

RenderRepoSearch(ctx, &RepoSearchOptions{
Ranger: models.GetRecentUpdatedRepositories,
PageSize: setting.UI.ExplorePagingNum,
Searcher: ctx.User,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this line removed?

Copy link
Member Author

@Morlinest Morlinest Oct 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed anymore. But now I see I have to make some changes as models.GetRecentUpdatedRepositories() need only Searcher and not OwnerID.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ethantkoenig I looked at it again and I don't need to change anything. Reason is to unify behaviour when you search with/without keyword. I'll post images.

Private: ctx.User != nil && ctx.User.IsAdmin,
OwnerID: ownerID,
Private: ctx.User != nil,
TplName: tplExploreRepos,
})
}
Expand Down