Skip to content

Commit

Permalink
Fix missing collabrative repos (#2367)
Browse files Browse the repository at this point in the history
* fix missing collabrative repos

* fix bug of collabrative

* fix SQL quotes
  • Loading branch information
lunny committed Aug 24, 2017
1 parent da230a2 commit f61a1d2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 42 deletions.
50 changes: 26 additions & 24 deletions models/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,12 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
opts.Page = 1
}

var starJoin bool
if opts.Starred && opts.OwnerID > 0 {
cond = builder.Eq{
"star.uid": opts.OwnerID,
}
starJoin = true
}

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

// Append conditions
if !opts.Starred && opts.OwnerID > 0 {
cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
var searcherReposCond builder.Cond = builder.Eq{"owner_id": opts.OwnerID}
if opts.Searcher != nil {
var ownerIds []int64

ownerIds = append(ownerIds, opts.Searcher.ID)
err = opts.Searcher.GetOrganizations(true)

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

for _, org := range opts.Searcher.Orgs {
ownerIds = append(ownerIds, org.ID)
}

searcherReposCond = searcherReposCond.Or(builder.In("owner_id", ownerIds))
if opts.Collaborate {
searcherReposCond = searcherReposCond.Or(builder.Expr("id IN (SELECT repo_id FROM `access` WHERE access.user_id = ? AND owner_id != ?)",
opts.Searcher.ID, opts.Searcher.ID))
}
}
cond = cond.And(searcherReposCond)
}

if !opts.Private {
cond = cond.And(builder.Eq{"is_private": false})
}

if opts.Searcher != nil {
var ownerIds []int64

ownerIds = append(ownerIds, opts.Searcher.ID)
err = opts.Searcher.GetOrganizations(true)

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

for _, org := range opts.Searcher.Orgs {
ownerIds = append(ownerIds, org.ID)
}

searcherReposCond := builder.In("owner_id", ownerIds)
if opts.Collaborate {
searcherReposCond = searcherReposCond.Or(builder.Expr(`id IN (SELECT repo_id FROM "access" WHERE access.user_id = ? AND owner_id != ?)`,
opts.Searcher.ID, opts.Searcher.ID))
}
cond = cond.And(searcherReposCond)
}

if len(opts.OrderBy) == 0 {
opts.OrderBy = "name ASC"
}

sess := x.NewSession()
defer sess.Close()

if opts.Starred && opts.OwnerID > 0 {
if starJoin {
count, err = sess.
Join("INNER", "star", "star.repo_id = repository.id").
Where(cond).
Expand Down
24 changes: 13 additions & 11 deletions routers/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,12 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
keyword := strings.Trim(ctx.Query("q"), " ")
if len(keyword) == 0 {
repos, count, err = opts.Ranger(&models.SearchRepoOptions{
Page: page,
PageSize: opts.PageSize,
Searcher: ctx.User,
OrderBy: orderBy,
Private: opts.Private,
Page: page,
PageSize: opts.PageSize,
Searcher: ctx.User,
OrderBy: orderBy,
Private: opts.Private,
Collaborate: true,
})
if err != nil {
ctx.Handle(500, "opts.Ranger", err)
Expand All @@ -125,12 +126,13 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
} else {
if isKeywordValid(keyword) {
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
Keyword: keyword,
OrderBy: orderBy,
Private: opts.Private,
Page: page,
PageSize: opts.PageSize,
Searcher: ctx.User,
Keyword: keyword,
OrderBy: orderBy,
Private: opts.Private,
Page: page,
PageSize: opts.PageSize,
Searcher: ctx.User,
Collaborate: true,
})
if err != nil {
ctx.Handle(500, "SearchRepositoryByName", err)
Expand Down
15 changes: 8 additions & 7 deletions routers/user/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,14 @@ func Profile(ctx *context.Context) {
ctx.Data["Total"] = total
} else {
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
Keyword: keyword,
OwnerID: ctxUser.ID,
OrderBy: orderBy,
Private: showPrivate,
Page: page,
IsProfile: true,
PageSize: setting.UI.User.RepoPagingNum,
Keyword: keyword,
OwnerID: ctxUser.ID,
OrderBy: orderBy,
Private: showPrivate,
Page: page,
IsProfile: true,
PageSize: setting.UI.User.RepoPagingNum,
Collaborate: true,
})
if err != nil {
ctx.Handle(500, "SearchRepositoryByName", err)
Expand Down

0 comments on commit f61a1d2

Please sign in to comment.