Skip to content
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

[Refactor] Unify repo search order by logic #30876

Merged
merged 11 commits into from
Jun 13, 2024
6 changes: 0 additions & 6 deletions models/db/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ const (
SearchOrderByRecentUpdated SearchOrderBy = "updated_unix DESC"
SearchOrderByOldest SearchOrderBy = "created_unix ASC"
SearchOrderByNewest SearchOrderBy = "created_unix DESC"
SearchOrderBySize SearchOrderBy = "size ASC"
lunny marked this conversation as resolved.
Show resolved Hide resolved
SearchOrderBySizeReverse SearchOrderBy = "size DESC"
SearchOrderByGitSize SearchOrderBy = "git_size ASC"
SearchOrderByGitSizeReverse SearchOrderBy = "git_size DESC"
SearchOrderByLFSSize SearchOrderBy = "lfs_size ASC"
SearchOrderByLFSSizeReverse SearchOrderBy = "lfs_size DESC"
SearchOrderByID SearchOrderBy = "id ASC"
SearchOrderByIDReverse SearchOrderBy = "id DESC"
SearchOrderByStars SearchOrderBy = "num_stars ASC"
Expand Down
25 changes: 0 additions & 25 deletions models/repo/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,31 +207,6 @@ type SearchRepoOptions struct {
OnlyShowRelevant bool
}

// 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"
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"
)

// UserOwnedRepoCond returns user ownered repositories
func UserOwnedRepoCond(userID int64) builder.Cond {
return builder.Eq{
Expand Down
72 changes: 62 additions & 10 deletions models/repo/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,72 @@ package repo

import "code.gitea.io/gitea/models/db"

// Strings for sorting result
const (
// only used for repos
SearchOrderByAlphabetically db.SearchOrderBy = "owner_name ASC, name ASC"
6543 marked this conversation as resolved.
Show resolved Hide resolved
6543 marked this conversation as resolved.
Show resolved Hide resolved
6543 marked this conversation as resolved.
Show resolved Hide resolved
SearchOrderByAlphabeticallyReverse db.SearchOrderBy = "owner_name DESC, name DESC"
SearchOrderBySize db.SearchOrderBy = "size ASC"
SearchOrderBySizeReverse db.SearchOrderBy = "size DESC"
SearchOrderByGitSize db.SearchOrderBy = "git_size ASC"
SearchOrderByGitSizeReverse db.SearchOrderBy = "git_size DESC"
SearchOrderByLFSSize db.SearchOrderBy = "lfs_size ASC"
SearchOrderByLFSSizeReverse db.SearchOrderBy = "lfs_size DESC"
// alias as also used elsewhere
SearchOrderByLeastUpdated db.SearchOrderBy = db.SearchOrderByLeastUpdated
SearchOrderByRecentUpdated db.SearchOrderBy = db.SearchOrderByRecentUpdated
SearchOrderByOldest db.SearchOrderBy = db.SearchOrderByOldest
SearchOrderByNewest db.SearchOrderBy = db.SearchOrderByNewest
SearchOrderByID db.SearchOrderBy = db.SearchOrderByID
SearchOrderByIDReverse db.SearchOrderBy = db.SearchOrderByIDReverse
SearchOrderByStars db.SearchOrderBy = db.SearchOrderByStars
SearchOrderByStarsReverse db.SearchOrderBy = db.SearchOrderByStarsReverse
SearchOrderByForks db.SearchOrderBy = db.SearchOrderByForks
SearchOrderByForksReverse db.SearchOrderBy = db.SearchOrderByForksReverse
)

// SearchOrderByMap represents all possible search order
var SearchOrderByMap = map[string]map[string]db.SearchOrderBy{
"asc": {
"alpha": "owner_name ASC, name ASC",
"created": db.SearchOrderByOldest,
"updated": db.SearchOrderByLeastUpdated,
"size": db.SearchOrderBySize,
"id": db.SearchOrderByID,
"alpha": SearchOrderByAlphabetically,
"created": SearchOrderByOldest,
"updated": SearchOrderByLeastUpdated,
"size": SearchOrderBySize,
"git_size": SearchOrderByGitSize,
"lfs_size": SearchOrderByLFSSize,
"id": SearchOrderByID,
"stars": SearchOrderByStars,
"forks": SearchOrderByForks,
},
"desc": {
"alpha": "owner_name DESC, name DESC",
"created": db.SearchOrderByNewest,
"updated": db.SearchOrderByRecentUpdated,
"size": db.SearchOrderBySizeReverse,
"id": db.SearchOrderByIDReverse,
"alpha": SearchOrderByAlphabeticallyReverse,
"created": SearchOrderByNewest,
"updated": SearchOrderByRecentUpdated,
"size": SearchOrderBySizeReverse,
"git_size": SearchOrderByGitSizeReverse,
"lfs_size": SearchOrderByLFSSizeReverse,
"id": SearchOrderByIDReverse,
"stars": SearchOrderByStarsReverse,
"forks": SearchOrderByForksReverse,
},
}

// SearchOrderByFlatMap is similar to SearchOrderByMap but use human language keywords
// to decide between asc and desc
var SearchOrderByFlatMap = map[string]db.SearchOrderBy{
"newest": SearchOrderByMap["desc"]["created"],
6543 marked this conversation as resolved.
Show resolved Hide resolved
"oldest": SearchOrderByMap["asc"]["created"],
"leastupdate": SearchOrderByMap["asc"]["updated"],
"reversealphabetically": SearchOrderByMap["desc"]["alpha"],
"alphabetically": SearchOrderByMap["asc"]["alpha"],
"reversesize": SearchOrderByMap["desc"]["size"],
"size": SearchOrderByMap["asc"]["size"],
"reversegitsize": SearchOrderByMap["desc"]["git_size"],
"gitsize": SearchOrderByMap["asc"]["git_size"],
"reverselfssize": SearchOrderByMap["desc"]["lfs_size"],
"lfssize": SearchOrderByMap["asc"]["lfs_size"],
"moststars": SearchOrderByMap["desc"]["stars"],
"feweststars": SearchOrderByMap["asc"]["stars"],
"mostforks": SearchOrderByMap["desc"]["forks"],
"fewestforks": SearchOrderByMap["asc"]["forks"],
}
2 changes: 1 addition & 1 deletion routers/api/v1/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func Search(ctx *context.APIContext) {
// - name: sort
// in: query
// description: sort repos by attribute. Supported values are
// "alpha", "created", "updated", "size", and "id".
// "alpha", "created", "updated", "size", "git_size", "lfs_size", "stars", "forks" and "id".
// Default is "alpha"
// type: string
// - name: order
Expand Down
39 changes: 5 additions & 34 deletions routers/web/explore/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,43 +61,14 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
if sortOrder == "" {
sortOrder = setting.UI.ExploreDefaultSort
}
ctx.Data["SortType"] = sortOrder

switch sortOrder {
case "newest":
orderBy = db.SearchOrderByNewest
case "oldest":
orderBy = db.SearchOrderByOldest
case "leastupdate":
orderBy = db.SearchOrderByLeastUpdated
case "reversealphabetically":
orderBy = db.SearchOrderByAlphabeticallyReverse
case "alphabetically":
orderBy = db.SearchOrderByAlphabetically
case "reversesize":
orderBy = db.SearchOrderBySizeReverse
case "size":
orderBy = db.SearchOrderBySize
case "reversegitsize":
orderBy = db.SearchOrderByGitSizeReverse
case "gitsize":
orderBy = db.SearchOrderByGitSize
case "reverselfssize":
orderBy = db.SearchOrderByLFSSizeReverse
case "lfssize":
orderBy = db.SearchOrderByLFSSize
case "moststars":
orderBy = db.SearchOrderByStarsReverse
case "feweststars":
orderBy = db.SearchOrderByStars
case "mostforks":
orderBy = db.SearchOrderByForksReverse
case "fewestforks":
orderBy = db.SearchOrderByForks
default:
ctx.Data["SortType"] = "recentupdate"
if order, ok := repo_model.SearchOrderByFlatMap[sortOrder]; ok {
6543 marked this conversation as resolved.
Show resolved Hide resolved
orderBy = order
} else {
sortOrder = "recentupdate"
orderBy = db.SearchOrderByRecentUpdated
}
ctx.Data["SortType"] = sortOrder

keyword := ctx.FormTrim("q")

Expand Down
2 changes: 1 addition & 1 deletion templates/swagger/v1_json.tmpl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.