Skip to content

Commit dae998e

Browse files
committed
Fix/rewrite searchRepositoryByName function
1 parent 8cb7ce2 commit dae998e

File tree

1 file changed

+72
-42
lines changed

1 file changed

+72
-42
lines changed

models/repo_list.go

Lines changed: 72 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -114,53 +114,91 @@ type SearchRepoOptions struct {
114114

115115
// SearchRepositoryByName takes keyword and part of repository name to search,
116116
// it returns results in given range and number of total results.
117-
func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, count int64, err error) {
118-
var cond = builder.NewCond()
117+
func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, _ int64, _ error) {
118+
// Set owner of searched repositories if present in options
119+
searchOwnerID := opts.OwnerID
120+
121+
// Set owner ID to Searcher ID if Owner is not filled
122+
if opts.OwnerID <= 0 && opts.Searcher != nil {
123+
searchOwnerID = opts.Searcher.ID
124+
}
125+
// Check if user with Owner ID exists
126+
if searchOwnerID > 0 {
127+
userExists, err := GetUser(&User{ID: searchOwnerID})
128+
if err != nil {
129+
return nil, 0, err
130+
}
131+
if userExists == false {
132+
return nil, 0, ErrUserNotExist{UID: searchOwnerID}
133+
}
134+
}
135+
136+
// Set public search by default
137+
isPublicSearch := true
138+
139+
// Allow to search for owners private data
140+
if opts.Searcher != nil && (opts.Searcher.ID == searchOwnerID || opts.Searcher.IsAdmin) {
141+
isPublicSearch = false
142+
}
143+
144+
// Check and set page to correct number
119145
if opts.Page <= 0 {
120146
opts.Page = 1
121147
}
122148

123-
var starJoin bool
124-
if opts.Starred && opts.OwnerID > 0 {
149+
var cond = builder.NewCond()
150+
151+
// Include starred repositories by Owner
152+
if opts.Starred && searchOwnerID > 0 {
125153
cond = builder.Eq{
126-
"star.uid": opts.OwnerID,
154+
"star.uid": searchOwnerID,
127155
}
128-
starJoin = true
129156
}
130157

131-
opts.Keyword = strings.ToLower(opts.Keyword)
158+
// Add repository name keyword to search for
132159
if opts.Keyword != "" {
160+
opts.Keyword = strings.ToLower(opts.Keyword)
133161
cond = cond.And(builder.Like{"lower_name", opts.Keyword})
134162
}
135163

136-
// Append conditions
137-
if !opts.Starred && opts.OwnerID > 0 {
138-
var searcherReposCond builder.Cond = builder.Eq{"owner_id": opts.OwnerID}
139-
if opts.Searcher != nil {
140-
var ownerIds []int64
164+
// Exclude private repositories
165+
// if it is set in options
166+
// or is public search
167+
if !opts.Private || isPublicSearch {
168+
cond = cond.And(builder.Eq{"is_private": false})
169+
}
170+
171+
if searchOwnerID > 0 {
172+
// Set user access conditions
173+
var accessCond = builder.NewCond()
141174

142-
ownerIds = append(ownerIds, opts.Searcher.ID)
143-
err = opts.Searcher.GetOrganizations(true)
175+
// Add Owner ID to access conditions
176+
accessCond = builder.Eq{"owner_id": searchOwnerID}
177+
178+
// Include collaborative repositories
179+
if opts.Collaborate {
180+
// Get owner organizations
181+
orgs, err := GetOrgUsersByUserID(searchOwnerID, !isPublicSearch)
144182

145183
if err != nil {
146184
return nil, 0, fmt.Errorf("Organization: %v", err)
147185
}
148186

149-
for _, org := range opts.Searcher.Orgs {
150-
ownerIds = append(ownerIds, org.ID)
187+
var ownerIds []int64
188+
for _, org := range orgs {
189+
ownerIds = append(ownerIds, org.OrgID)
151190
}
152191

153-
searcherReposCond = searcherReposCond.Or(builder.In("owner_id", ownerIds))
154-
if opts.Collaborate {
155-
searcherReposCond = searcherReposCond.Or(builder.Expr("id IN (SELECT repo_id FROM `access` WHERE access.user_id = ? AND owner_id != ?)",
156-
opts.Searcher.ID, opts.Searcher.ID))
157-
}
192+
// Add repositories from related organizations
193+
accessCond = accessCond.Or(builder.In("owner_id", ownerIds))
194+
195+
// Add repositories where user is set as collaborator directly
196+
accessCond = accessCond.Or(builder.Expr("id IN (SELECT repo_id FROM `access` WHERE access.user_id = ? AND owner_id != ?)",
197+
searchOwnerID, searchOwnerID))
158198
}
159-
cond = cond.And(searcherReposCond)
160-
}
161199

162-
if !opts.Private {
163-
cond = cond.And(builder.Eq{"is_private": false})
200+
// Add user access conditions to search
201+
cond = cond.And(accessCond)
164202
}
165203

166204
if len(opts.OrderBy) == 0 {
@@ -170,23 +208,15 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
170208
sess := x.NewSession()
171209
defer sess.Close()
172210

173-
if starJoin {
174-
count, err = sess.
175-
Join("INNER", "star", "star.repo_id = repository.id").
176-
Where(cond).
177-
Count(new(Repository))
178-
if err != nil {
179-
return nil, 0, fmt.Errorf("Count: %v", err)
180-
}
211+
if opts.Starred {
212+
sess = sess.Join("INNER", "star", "star.repo_id = repository.id")
213+
}
181214

182-
sess.Join("INNER", "star", "star.repo_id = repository.id")
183-
} else {
184-
count, err = sess.
185-
Where(cond).
186-
Count(new(Repository))
187-
if err != nil {
188-
return nil, 0, fmt.Errorf("Count: %v", err)
189-
}
215+
count, err := sess.
216+
Where(cond).
217+
Count(new(Repository))
218+
if err != nil {
219+
return nil, 0, fmt.Errorf("Count: %v", err)
190220
}
191221

192222
repos = make([]*Repository, 0, opts.PageSize)
@@ -204,7 +234,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
204234
}
205235
}
206236

207-
return
237+
return repos, count, nil
208238
}
209239

210240
// Repositories returns all repositories

0 commit comments

Comments
 (0)