Skip to content

Commit f61a1d2

Browse files
authored
Fix missing collabrative repos (#2367)
* fix missing collabrative repos * fix bug of collabrative * fix SQL quotes
1 parent da230a2 commit f61a1d2

File tree

3 files changed

+47
-42
lines changed

3 files changed

+47
-42
lines changed

models/repo_list.go

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,12 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
120120
opts.Page = 1
121121
}
122122

123+
var starJoin bool
123124
if opts.Starred && opts.OwnerID > 0 {
124125
cond = builder.Eq{
125126
"star.uid": opts.OwnerID,
126127
}
128+
starJoin = true
127129
}
128130

129131
opts.Keyword = strings.ToLower(opts.Keyword)
@@ -133,42 +135,42 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
133135

134136
// Append conditions
135137
if !opts.Starred && opts.OwnerID > 0 {
136-
cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
138+
var searcherReposCond builder.Cond = builder.Eq{"owner_id": opts.OwnerID}
139+
if opts.Searcher != nil {
140+
var ownerIds []int64
141+
142+
ownerIds = append(ownerIds, opts.Searcher.ID)
143+
err = opts.Searcher.GetOrganizations(true)
144+
145+
if err != nil {
146+
return nil, 0, fmt.Errorf("Organization: %v", err)
147+
}
148+
149+
for _, org := range opts.Searcher.Orgs {
150+
ownerIds = append(ownerIds, org.ID)
151+
}
152+
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+
}
158+
}
159+
cond = cond.And(searcherReposCond)
137160
}
161+
138162
if !opts.Private {
139163
cond = cond.And(builder.Eq{"is_private": false})
140164
}
141165

142-
if opts.Searcher != nil {
143-
var ownerIds []int64
144-
145-
ownerIds = append(ownerIds, opts.Searcher.ID)
146-
err = opts.Searcher.GetOrganizations(true)
147-
148-
if err != nil {
149-
return nil, 0, fmt.Errorf("Organization: %v", err)
150-
}
151-
152-
for _, org := range opts.Searcher.Orgs {
153-
ownerIds = append(ownerIds, org.ID)
154-
}
155-
156-
searcherReposCond := builder.In("owner_id", ownerIds)
157-
if opts.Collaborate {
158-
searcherReposCond = searcherReposCond.Or(builder.Expr(`id IN (SELECT repo_id FROM "access" WHERE access.user_id = ? AND owner_id != ?)`,
159-
opts.Searcher.ID, opts.Searcher.ID))
160-
}
161-
cond = cond.And(searcherReposCond)
162-
}
163-
164166
if len(opts.OrderBy) == 0 {
165167
opts.OrderBy = "name ASC"
166168
}
167169

168170
sess := x.NewSession()
169171
defer sess.Close()
170172

171-
if opts.Starred && opts.OwnerID > 0 {
173+
if starJoin {
172174
count, err = sess.
173175
Join("INNER", "star", "star.repo_id = repository.id").
174176
Where(cond).

routers/home.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,12 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
112112
keyword := strings.Trim(ctx.Query("q"), " ")
113113
if len(keyword) == 0 {
114114
repos, count, err = opts.Ranger(&models.SearchRepoOptions{
115-
Page: page,
116-
PageSize: opts.PageSize,
117-
Searcher: ctx.User,
118-
OrderBy: orderBy,
119-
Private: opts.Private,
115+
Page: page,
116+
PageSize: opts.PageSize,
117+
Searcher: ctx.User,
118+
OrderBy: orderBy,
119+
Private: opts.Private,
120+
Collaborate: true,
120121
})
121122
if err != nil {
122123
ctx.Handle(500, "opts.Ranger", err)
@@ -125,12 +126,13 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
125126
} else {
126127
if isKeywordValid(keyword) {
127128
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
128-
Keyword: keyword,
129-
OrderBy: orderBy,
130-
Private: opts.Private,
131-
Page: page,
132-
PageSize: opts.PageSize,
133-
Searcher: ctx.User,
129+
Keyword: keyword,
130+
OrderBy: orderBy,
131+
Private: opts.Private,
132+
Page: page,
133+
PageSize: opts.PageSize,
134+
Searcher: ctx.User,
135+
Collaborate: true,
134136
})
135137
if err != nil {
136138
ctx.Handle(500, "SearchRepositoryByName", err)

routers/user/profile.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,14 @@ func Profile(ctx *context.Context) {
205205
ctx.Data["Total"] = total
206206
} else {
207207
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
208-
Keyword: keyword,
209-
OwnerID: ctxUser.ID,
210-
OrderBy: orderBy,
211-
Private: showPrivate,
212-
Page: page,
213-
IsProfile: true,
214-
PageSize: setting.UI.User.RepoPagingNum,
208+
Keyword: keyword,
209+
OwnerID: ctxUser.ID,
210+
OrderBy: orderBy,
211+
Private: showPrivate,
212+
Page: page,
213+
IsProfile: true,
214+
PageSize: setting.UI.User.RepoPagingNum,
215+
Collaborate: true,
215216
})
216217
if err != nil {
217218
ctx.Handle(500, "SearchRepositoryByName", err)

0 commit comments

Comments
 (0)