diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/fixtures/repository.yml | 1 | ||||
-rw-r--r-- | models/repo_list.go | 16 | ||||
-rw-r--r-- | models/repo_list_test.go | 30 |
3 files changed, 44 insertions, 3 deletions
diff --git a/models/fixtures/repository.yml b/models/fixtures/repository.yml index f43fae3d67..16b0045b21 100644 --- a/models/fixtures/repository.yml +++ b/models/fixtures/repository.yml @@ -165,6 +165,7 @@ owner_id: 14 lower_name: test_repo_14 name: test_repo_14 + description: test_description_14 is_private: false num_issues: 0 num_closed_issues: 0 diff --git a/models/repo_list.go b/models/repo_list.go index ee4266d4e6..692d4d002f 100644 --- a/models/repo_list.go +++ b/models/repo_list.go @@ -136,6 +136,8 @@ type SearchRepoOptions struct { Mirror util.OptionalBool // only search topic name TopicOnly bool + // include description in keyword search + IncludeDescription bool } //SearchOrderBy is used to sort the result @@ -163,9 +165,9 @@ const ( SearchOrderByForksReverse SearchOrderBy = "num_forks DESC" ) -// SearchRepositoryByName takes keyword and part of repository name to search, +// SearchRepository returns repositories based on search options, // it returns results in given range and number of total results. -func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, error) { +func SearchRepository(opts *SearchRepoOptions) (RepositoryList, int64, error) { if opts.Page <= 0 { opts.Page = 1 } @@ -264,6 +266,9 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err var likes = builder.NewCond() for _, v := range strings.Split(opts.Keyword, ",") { likes = likes.Or(builder.Like{"lower_name", strings.ToLower(v)}) + if opts.IncludeDescription { + likes = likes.Or(builder.Like{"LOWER(description)", strings.ToLower(v)}) + } } keywordCond = keywordCond.Or(likes) } @@ -311,6 +316,13 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err return repos, count, nil } +// SearchRepositoryByName takes keyword and part of repository name to search, +// it returns results in given range and number of total results. +func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, error) { + opts.IncludeDescription = false + return SearchRepository(opts) +} + // FindUserAccessibleRepoIDs find all accessible repositories' ID by user's id func FindUserAccessibleRepoIDs(userID int64) ([]int64, error) { var accessCond builder.Cond = builder.Eq{"is_private": false} diff --git a/models/repo_list_test.go b/models/repo_list_test.go index 645de2a59a..76ae9ab3d7 100644 --- a/models/repo_list_test.go +++ b/models/repo_list_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestSearchRepositoryByName(t *testing.T) { +func TestSearchRepository(t *testing.T) { assert.NoError(t, PrepareTestDatabase()) // test search public repository on explore page @@ -74,6 +74,34 @@ func TestSearchRepositoryByName(t *testing.T) { assert.Empty(t, repos) assert.Equal(t, int64(0), count) + // Test search within description + repos, count, err = SearchRepository(&SearchRepoOptions{ + Keyword: "description_14", + Page: 1, + PageSize: 10, + Collaborate: util.OptionalBoolFalse, + IncludeDescription: true, + }) + + assert.NoError(t, err) + if assert.Len(t, repos, 1) { + assert.Equal(t, "test_repo_14", repos[0].Name) + } + assert.Equal(t, int64(1), count) + + // Test NOT search within description + repos, count, err = SearchRepository(&SearchRepoOptions{ + Keyword: "description_14", + Page: 1, + PageSize: 10, + Collaborate: util.OptionalBoolFalse, + IncludeDescription: false, + }) + + assert.NoError(t, err) + assert.Empty(t, repos) + assert.Equal(t, int64(0), count) + testCases := []struct { name string opts *SearchRepoOptions |