-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Fix repo search result #2600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix repo search result #2600
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -350,8 +350,11 @@ func GetOrgsByUserID(userID int64, showAll bool) ([]*User, error) { | |
return getOrgsByUserID(sess, userID, showAll) | ||
} | ||
|
||
func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) { | ||
func getOwnedOrgsByUserID(sess *xorm.Session, userID int64, showAll bool) ([]*User, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: could we call it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I called it showAll to be consistent with other methods used similarly. But I can change that at least for my PR. |
||
orgs := make([]*User, 0, 10) | ||
if !showAll { | ||
sess.And("`org_user`.is_public=?", true) | ||
} | ||
return orgs, sess. | ||
Where("`org_user`.uid=?", userID). | ||
And("`org_user`.is_owner=?", true). | ||
|
@@ -361,16 +364,16 @@ func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) { | |
} | ||
|
||
// GetOwnedOrgsByUserID returns a list of organizations are owned by given user ID. | ||
func GetOwnedOrgsByUserID(userID int64) ([]*User, error) { | ||
func GetOwnedOrgsByUserID(userID int64, showAll bool) ([]*User, error) { | ||
sess := x.NewSession() | ||
defer sess.Close() | ||
return getOwnedOrgsByUserID(sess, userID) | ||
return getOwnedOrgsByUserID(sess, userID, showAll) | ||
} | ||
|
||
// GetOwnedOrgsByUserIDDesc returns a list of organizations are owned by | ||
// given user ID, ordered descending by the given condition. | ||
func GetOwnedOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) { | ||
return getOwnedOrgsByUserID(x.Desc(desc), userID) | ||
func GetOwnedOrgsByUserIDDesc(userID int64, desc string, showAll bool) ([]*User, error) { | ||
return getOwnedOrgsByUserID(x.Desc(desc), userID, showAll) | ||
} | ||
|
||
// GetOrgUsersByUserID returns all organization-user relations by user ID. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,26 +155,44 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun | |
// Append conditions | ||
if !opts.Starred && opts.OwnerID > 0 { | ||
var searcherReposCond builder.Cond = builder.Eq{"owner_id": opts.OwnerID} | ||
var ownerIds []int64 | ||
// Searcher == Owner | ||
if opts.Searcher != nil { | ||
var ownerIds []int64 | ||
|
||
ownerIds = append(ownerIds, opts.Searcher.ID) | ||
err = opts.Searcher.GetOrganizations(true) | ||
err = opts.Searcher.GetOwnedOrganizations(true) | ||
|
||
if err != nil { | ||
return nil, 0, fmt.Errorf("Organization: %v", err) | ||
} | ||
|
||
for _, org := range opts.Searcher.Orgs { | ||
for _, org := range opts.Searcher.OwnedOrgs { | ||
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 != ?)", | ||
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)) | ||
} | ||
} else if opts.OwnerID > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not needed. It's already inside |
||
ownerIds = append(ownerIds, opts.OwnerID) | ||
|
||
owner, err := GetUserByID(opts.OwnerID) | ||
if err != nil { | ||
return nil, 0, fmt.Errorf("User: %v", err) | ||
} | ||
|
||
err = owner.GetOwnedOrganizations(false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Authorization decision making. |
||
|
||
for _, org := range owner.OwnedOrgs { | ||
ownerIds = append(ownerIds, org.ID) | ||
} | ||
|
||
if err != nil { | ||
return nil, 0, fmt.Errorf("Organization: %v", err) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
} | ||
searcherReposCond = searcherReposCond.Or(builder.In("owner_id", ownerIds)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already in condition, line 157. |
||
cond = cond.And(searcherReposCond) | ||
} | ||
|
||
|
@@ -268,17 +286,21 @@ func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList | |
var ownerIds []int64 | ||
|
||
ownerIds = append(ownerIds, opts.Searcher.ID) | ||
err := opts.Searcher.GetOrganizations(true) | ||
err := opts.Searcher.GetOwnedOrganizations(true) | ||
|
||
if err != nil { | ||
return nil, 0, fmt.Errorf("Organization: %v", err) | ||
} | ||
|
||
for _, org := range opts.Searcher.Orgs { | ||
for _, org := range opts.Searcher.OwnedOrgs { | ||
ownerIds = append(ownerIds, org.ID) | ||
} | ||
|
||
cond = cond.Or(builder.In("owner_id", ownerIds)) | ||
if opts.Collaborate { | ||
cond = cond.Or(builder.Expr("id IN (SELECT repo_id FROM `access` WHERE access.user_id = ?) AND owner_id != ?", | ||
opts.Searcher.ID, opts.Searcher.ID)) | ||
} | ||
} | ||
|
||
count, err := x.Where(cond).Count(new(Repository)) | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a lot of duplicated code. Maybe we should add a helper function
Even better, add a session parameter, so you can use it for the logged-in tests too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ethantkoenig If you want that you can review #2575 as it rewrites tests. I wanted to change as few as possible to make reviewing easier, but if it is desired the mentioned PR does this ;-)