Skip to content

Commit 928f95d

Browse files
authored
Merge branch 'main' into fix-19743-improve-encoding-detection
2 parents 72a0a45 + 876cad0 commit 928f95d

8 files changed

+3415
-2001
lines changed

models/repo_list.go

+13
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,15 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
459459
likes := builder.NewCond()
460460
for _, v := range strings.Split(opts.Keyword, ",") {
461461
likes = likes.Or(builder.Like{"lower_name", strings.ToLower(v)})
462+
463+
// If the string looks like "org/repo", match against that pattern too
464+
if opts.TeamID == 0 && strings.Count(opts.Keyword, "/") == 1 {
465+
pieces := strings.Split(opts.Keyword, "/")
466+
ownerName := pieces[0]
467+
repoName := pieces[1]
468+
likes = likes.Or(builder.And(builder.Like{"owner_name", strings.ToLower(ownerName)}, builder.Like{"lower_name", strings.ToLower(repoName)}))
469+
}
470+
462471
if opts.IncludeDescription {
463472
likes = likes.Or(builder.Like{"LOWER(description)", strings.ToLower(v)})
464473
}
@@ -549,6 +558,10 @@ func searchRepositoryByCondition(ctx context.Context, opts *SearchRepoOptions, c
549558

550559
if opts.PriorityOwnerID > 0 {
551560
opts.OrderBy = db.SearchOrderBy(fmt.Sprintf("CASE WHEN owner_id = %d THEN 0 ELSE owner_id END, %s", opts.PriorityOwnerID, opts.OrderBy))
561+
} else if strings.Count(opts.Keyword, "/") == 1 {
562+
// With "owner/repo" search times, prioritise results which match the owner field
563+
orgName := strings.Split(opts.Keyword, "/")[0]
564+
opts.OrderBy = db.SearchOrderBy(fmt.Sprintf("CASE WHEN owner_name LIKE '%s' THEN 0 ELSE 1 END, %s", orgName, opts.OrderBy))
552565
}
553566

554567
sess := db.GetEngine(ctx)

models/repo_list_test.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package models
66

77
import (
8+
"strings"
89
"testing"
910

1011
"code.gitea.io/gitea/models/db"
@@ -261,6 +262,16 @@ func TestSearchRepository(t *testing.T) {
261262
opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Template: util.OptionalBoolTrue},
262263
count: 2,
263264
},
265+
{
266+
name: "OwnerSlashRepoSearch",
267+
opts: &SearchRepoOptions{Keyword: "user/repo2", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Private: true, OwnerID: 0},
268+
count: 3,
269+
},
270+
{
271+
name: "OwnerSlashSearch",
272+
opts: &SearchRepoOptions{Keyword: "user20/", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Private: true, OwnerID: 0},
273+
count: 4,
274+
},
264275
}
265276

266277
for _, testCase := range testCases {
@@ -285,7 +296,21 @@ func TestSearchRepository(t *testing.T) {
285296
assert.NotEmpty(t, repo.Name)
286297

287298
if len(testCase.opts.Keyword) > 0 {
288-
assert.Contains(t, repo.Name, testCase.opts.Keyword)
299+
// Keyword match condition is different for search terms of form "owner/repo"
300+
if strings.Count(testCase.opts.Keyword, "/") == 1 {
301+
// May still match as a whole...
302+
wholeMatch := strings.Contains(repo.Name, testCase.opts.Keyword)
303+
304+
pieces := strings.Split(testCase.opts.Keyword, "/")
305+
ownerName := pieces[0]
306+
repoName := pieces[1]
307+
// ... or match in parts
308+
splitMatch := strings.Contains(repo.OwnerName, ownerName) && strings.Contains(repo.Name, repoName)
309+
310+
assert.True(t, wholeMatch || splitMatch, "Keyword '%s' does not match repo '%s/%s'", testCase.opts.Keyword, repo.Owner.Name, repo.Name)
311+
} else {
312+
assert.Contains(t, repo.Name, testCase.opts.Keyword)
313+
}
289314
}
290315

291316
if !testCase.opts.Private {

0 commit comments

Comments
 (0)