Browse Source

fix: Wrong repo list on Explore page if user already loggin. (#1009)

* fix: Wrong repo list on Explore page if user already loggin.

* fix: code readable.

* fix: declare variable
Bo-Yi Wu 3 years ago
parent
commit
83b6d03231
4 changed files with 128 additions and 12 deletions
  1. 36 0
      models/fixtures/repository.yml
  2. 15 0
      models/fixtures/user.yml
  3. 25 12
      models/repo.go
  4. 52 0
      models/repo_test.go

+ 36 - 0
models/fixtures/repository.yml

@@ -132,3 +132,39 @@
132 132
   num_pulls: 0
133 133
   num_closed_pulls: 0
134 134
   is_mirror: false
135
+
136
+-
137
+  id: 12
138
+  owner_id: 14
139
+  lower_name: test_repo_12
140
+  name: test_repo_12
141
+  is_private: false
142
+  num_issues: 0
143
+  num_closed_issues: 0
144
+  num_pulls: 0
145
+  num_closed_pulls: 0
146
+  is_mirror: false
147
+
148
+-
149
+  id: 13
150
+  owner_id: 14
151
+  lower_name: test_repo_13
152
+  name: test_repo_13
153
+  is_private: true
154
+  num_issues: 0
155
+  num_closed_issues: 0
156
+  num_pulls: 0
157
+  num_closed_pulls: 0
158
+  is_mirror: false
159
+
160
+-
161
+  id: 14
162
+  owner_id: 14
163
+  lower_name: test_repo_14
164
+  name: test_repo_14
165
+  is_private: false
166
+  num_issues: 0
167
+  num_closed_issues: 0
168
+  num_pulls: 0
169
+  num_closed_pulls: 0
170
+  is_mirror: false

+ 15 - 0
models/fixtures/user.yml

@@ -194,3 +194,18 @@
194 194
   avatar_email: user13@example.com
195 195
   num_repos: 1
196 196
   is_active: true
197
+
198
+-
199
+  id: 14
200
+  lower_name: user14
201
+  name: user14
202
+  full_name: User 14
203
+  email: user14@example.com
204
+  passwd: password
205
+  type: 0 # individual
206
+  salt: salt
207
+  is_admin: false
208
+  avatar: avatar14
209
+  avatar_email: user13@example.com
210
+  num_repos: 3
211
+  is_active: true

+ 25 - 12
models/repo.go

@@ -30,6 +30,7 @@ import (
30 30
 
31 31
 	"github.com/Unknwon/cae/zip"
32 32
 	"github.com/Unknwon/com"
33
+	"github.com/go-xorm/builder"
33 34
 	"github.com/go-xorm/xorm"
34 35
 	version "github.com/mcuadros/go-version"
35 36
 	ini "gopkg.in/ini.v1"
@@ -1797,7 +1798,11 @@ type SearchRepoOptions struct {
1797 1798
 // SearchRepositoryByName takes keyword and part of repository name to search,
1798 1799
 // it returns results in given range and number of total results.
1799 1800
 func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ int64, _ error) {
1800
-	var sess *xorm.Session
1801
+	var (
1802
+		sess *xorm.Session
1803
+		cond builder.Cond = builder.NewCond()
1804
+	)
1805
+
1801 1806
 	if len(opts.Keyword) == 0 {
1802 1807
 		return repos, 0, nil
1803 1808
 	}
@@ -1810,26 +1815,24 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in
1810 1815
 	repos = make([]*Repository, 0, opts.PageSize)
1811 1816
 
1812 1817
 	if opts.Starred && opts.OwnerID > 0 {
1813
-		sess = x.
1814
-			Join("INNER", "star", "star.repo_id = repository.id").
1815
-			Where("star.uid = ?", opts.OwnerID).
1816
-			And("lower_name LIKE ?", "%"+opts.Keyword+"%")
1817
-	} else {
1818
-		sess = x.Where("lower_name LIKE ?", "%"+opts.Keyword+"%")
1818
+		cond = builder.Eq{
1819
+			"star.uid": opts.OwnerID,
1820
+		}
1819 1821
 	}
1822
+	cond = cond.And(builder.Like{"lower_name", opts.Keyword})
1820 1823
 
1821 1824
 	// Append conditions
1822 1825
 	if !opts.Starred && opts.OwnerID > 0 {
1823
-		sess.And("owner_id = ?", opts.OwnerID)
1826
+		cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
1824 1827
 	}
1825 1828
 	if !opts.Private {
1826
-		sess.And("is_private=?", false)
1829
+		cond = cond.And(builder.Eq{"is_private": false})
1827 1830
 	}
1828 1831
 
1829 1832
 	if opts.Searcher != nil {
1833
+		var ownerIds []int64
1830 1834
 
1831
-		sess.Or("owner_id = ?", opts.Searcher.ID)
1832
-
1835
+		ownerIds = append(ownerIds, opts.Searcher.ID)
1833 1836
 		err := opts.Searcher.GetOrganizations(true)
1834 1837
 
1835 1838
 		if err != nil {
@@ -1837,14 +1840,24 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ in
1837 1840
 		}
1838 1841
 
1839 1842
 		for _, org := range opts.Searcher.Orgs {
1840
-			sess.Or("owner_id = ?", org.ID)
1843
+			ownerIds = append(ownerIds, org.ID)
1841 1844
 		}
1845
+
1846
+		cond = cond.Or(builder.And(builder.Like{"lower_name", opts.Keyword}, builder.In("owner_id", ownerIds)))
1842 1847
 	}
1843 1848
 
1844 1849
 	if len(opts.OrderBy) == 0 {
1845 1850
 		opts.OrderBy = "name ASC"
1846 1851
 	}
1847 1852
 
1853
+	if opts.Starred && opts.OwnerID > 0 {
1854
+		sess = x.
1855
+			Join("INNER", "star", "star.repo_id = repository.id").
1856
+			Where(cond)
1857
+	} else {
1858
+		sess = x.Where(cond)
1859
+	}
1860
+
1848 1861
 	var countSess xorm.Session
1849 1862
 	countSess = *sess
1850 1863
 	count, err := countSess.Count(new(Repository))

+ 52 - 0
models/repo_test.go

@@ -125,3 +125,55 @@ func TestForkRepository(t *testing.T) {
125 125
 	assert.Error(t, err)
126 126
 	assert.True(t, IsErrRepoAlreadyExist(err))
127 127
 }
128
+
129
+func TestSearchRepositoryByName(t *testing.T) {
130
+	assert.NoError(t, PrepareTestDatabase())
131
+
132
+	// test search public repository on explore page
133
+	repos, count, err := SearchRepositoryByName(&SearchRepoOptions{
134
+		Keyword:  "repo_12",
135
+		Page:     1,
136
+		PageSize: 10,
137
+		Searcher: nil,
138
+	})
139
+
140
+	assert.NotNil(t, repos)
141
+	assert.NoError(t, err)
142
+	assert.Equal(t, "test_repo_12", repos[0].Name)
143
+	assert.Equal(t, int64(1), count)
144
+
145
+	repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
146
+		Keyword:  "test_repo",
147
+		Page:     1,
148
+		PageSize: 10,
149
+		Searcher: nil,
150
+	})
151
+
152
+	assert.NotNil(t, repos)
153
+	assert.NoError(t, err)
154
+	assert.Equal(t, int64(2), count)
155
+
156
+	// test search private repository on explore page
157
+	repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
158
+		Keyword:  "repo_13",
159
+		Page:     1,
160
+		PageSize: 10,
161
+		Searcher: &User{ID: 14},
162
+	})
163
+
164
+	assert.NotNil(t, repos)
165
+	assert.NoError(t, err)
166
+	assert.Equal(t, "test_repo_13", repos[0].Name)
167
+	assert.Equal(t, int64(1), count)
168
+
169
+	repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
170
+		Keyword:  "test_repo",
171
+		Page:     1,
172
+		PageSize: 10,
173
+		Searcher: &User{ID: 14},
174
+	})
175
+
176
+	assert.NotNil(t, repos)
177
+	assert.NoError(t, err)
178
+	assert.Equal(t, int64(3), count)
179
+}