diff --git a/models/fixtures/repo_topic.yml b/models/fixtures/repo_topic.yml index 58937031c..3a27442d4 100644 --- a/models/fixtures/repo_topic.yml +++ b/models/fixtures/repo_topic.yml @@ -9,3 +9,7 @@ - repo_id: 1 topic_id: 3 + +- + repo_id: 2 + topic_id: 2 diff --git a/models/repo_list.go b/models/repo_list.go index b1527b73c..c9183166d 100644 --- a/models/repo_list.go +++ b/models/repo_list.go @@ -184,7 +184,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err if opts.Collaborate != util.OptionalBoolFalse { collaborateCond := builder.And( - builder.Expr("id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?)", opts.OwnerID), + builder.Expr("repository.id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?)", opts.OwnerID), builder.Neq{"owner_id": opts.OwnerID}) if !opts.Private { collaborateCond = collaborateCond.And(builder.Expr("owner_id NOT IN (SELECT org_id FROM org_user WHERE org_user.uid = ? AND org_user.is_public = ?)", opts.OwnerID, false)) @@ -202,7 +202,10 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err } if opts.Keyword != "" { - cond = cond.And(builder.Like{"lower_name", strings.ToLower(opts.Keyword)}) + var keywordCond = builder.NewCond() + keywordCond = keywordCond.Or(builder.Like{"lower_name", strings.ToLower(opts.Keyword)}) + keywordCond = keywordCond.Or(builder.Like{"topic.name", strings.ToLower(opts.Keyword)}) + cond = cond.And(keywordCond) } if opts.Fork != util.OptionalBoolNone { @@ -224,9 +227,15 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err sess.Join("INNER", "star", "star.repo_id = repository.id") } + if opts.Keyword != "" { + sess.Join("LEFT", "repo_topic", "repo_topic.repo_id = repository.id") + sess.Join("LEFT", "topic", "repo_topic.topic_id = topic.id") + } + count, err := sess. Where(cond). Count(new(Repository)) + if err != nil { return nil, 0, fmt.Errorf("Count: %v", err) } @@ -236,11 +245,23 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err sess.Join("INNER", "star", "star.repo_id = repository.id") } + if opts.Keyword != "" { + sess.Join("LEFT", "repo_topic", "repo_topic.repo_id = repository.id") + sess.Join("LEFT", "topic", "repo_topic.topic_id = topic.id") + } + + if opts.Keyword != "" { + sess.Select("repository.*") + sess.GroupBy("repository.id") + sess.OrderBy("repository." + opts.OrderBy.String()) + } else { + sess.OrderBy(opts.OrderBy.String()) + } + repos := make(RepositoryList, 0, opts.PageSize) if err = sess. Where(cond). Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). - OrderBy(opts.OrderBy.String()). Find(&repos); err != nil { return nil, 0, fmt.Errorf("Repo: %v", err) } diff --git a/models/repo_list_test.go b/models/repo_list_test.go index 164bc19bf..e9287e56f 100644 --- a/models/repo_list_test.go +++ b/models/repo_list_test.go @@ -222,3 +222,28 @@ func TestSearchRepositoryByName(t *testing.T) { }) } } + +func TestSearchRepositoryByTopicName(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + testCases := []struct { + name string + opts *SearchRepoOptions + count int + }{ + {name: "AllPublic/SearchPublicRepositoriesFromTopic", + opts: &SearchRepoOptions{OwnerID: 2, AllPublic: true, Keyword: "golang"}, + count: 1}, + {name: "AllPublic/SearchPrivateRepositoriesFromTopic", + opts: &SearchRepoOptions{OwnerID: 2, AllPublic: true, Keyword: "database", Private: true}, + count: 2}, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + _, count, err := SearchRepositoryByName(testCase.opts) + assert.NoError(t, err) + assert.Equal(t, int64(testCase.count), count) + }) + } +} diff --git a/templates/explore/repo_list.tmpl b/templates/explore/repo_list.tmpl index b8f4490c1..4b5135ed0 100644 --- a/templates/explore/repo_list.tmpl +++ b/templates/explore/repo_list.tmpl @@ -20,7 +20,7 @@ {{if .Topics }}
{{range .Topics}} - {{if ne . "" }}
{{.}}
{{end}} + {{if ne . "" }}
{{.}}
{{end}} {{end}}
{{end}} diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl index 685351ecb..2ed681ccc 100644 --- a/templates/repo/home.tmpl +++ b/templates/repo/home.tmpl @@ -24,8 +24,8 @@ {{end}}
- {{range .Topics}}
{{.Name}}
{{end}} - {{if .IsRepositoryAdmin}}{{.i18n.Tr "repo.topic.manage_topics"}}{{end}} + {{range .Topics}}{{.Name}}{{end}} + {{if .IsRepositoryAdmin}}{{.i18n.Tr "repo.topic.manage_topics"}}{{end}}
{{if .IsRepositoryAdmin}}
@@ -34,7 +34,7 @@