diff options
author | Eekle <96976531+Eekle@users.noreply.github.com> | 2022-05-21 10:15:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-21 17:15:40 +0800 |
commit | 876cad0064b8799da1ed82c2d7d2bf689ba5cfe7 (patch) | |
tree | a1653e973de1cf1f80515af4f81db3546485f696 /models/repo_list.go | |
parent | ba7750d6e74f689ed045e9926bffa402c2e23a70 (diff) | |
download | gitea-876cad0064b8799da1ed82c2d7d2bf689ba5cfe7.tar.gz gitea-876cad0064b8799da1ed82c2d7d2bf689ba5cfe7.zip |
Allows repo search to match against "owner/repo" pattern strings (#19754)
* Allows repo search to match against "owner/repo" pattern strings
* Gofumpt
* Adds test case for "owner/repo" style repo search
* With "owner/repo" search terms, prioritise results which match the owner field
* Fixes unquoted SQL string in repo search
Diffstat (limited to 'models/repo_list.go')
-rw-r--r-- | models/repo_list.go | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/models/repo_list.go b/models/repo_list.go index d1974d77e7..906b7548d4 100644 --- a/models/repo_list.go +++ b/models/repo_list.go @@ -459,6 +459,15 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond { likes := builder.NewCond() for _, v := range strings.Split(opts.Keyword, ",") { likes = likes.Or(builder.Like{"lower_name", strings.ToLower(v)}) + + // If the string looks like "org/repo", match against that pattern too + if opts.TeamID == 0 && strings.Count(opts.Keyword, "/") == 1 { + pieces := strings.Split(opts.Keyword, "/") + ownerName := pieces[0] + repoName := pieces[1] + likes = likes.Or(builder.And(builder.Like{"owner_name", strings.ToLower(ownerName)}, builder.Like{"lower_name", strings.ToLower(repoName)})) + } + if opts.IncludeDescription { likes = likes.Or(builder.Like{"LOWER(description)", strings.ToLower(v)}) } @@ -549,6 +558,10 @@ func searchRepositoryByCondition(ctx context.Context, opts *SearchRepoOptions, c if opts.PriorityOwnerID > 0 { opts.OrderBy = db.SearchOrderBy(fmt.Sprintf("CASE WHEN owner_id = %d THEN 0 ELSE owner_id END, %s", opts.PriorityOwnerID, opts.OrderBy)) + } else if strings.Count(opts.Keyword, "/") == 1 { + // With "owner/repo" search times, prioritise results which match the owner field + orgName := strings.Split(opts.Keyword, "/")[0] + opts.OrderBy = db.SearchOrderBy(fmt.Sprintf("CASE WHEN owner_name LIKE '%s' THEN 0 ELSE 1 END, %s", orgName, opts.OrderBy)) } sess := db.GetEngine(ctx) |