Skip to content
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 the number of Repository for Organization on dashboard page #16799

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,35 @@ func queryUserOrgIDs(uid int64) *builder.Builder {
type MinimalOrg = User

// GetUserOrgsList returns one user's all orgs list
func GetUserOrgsList(uid int64) ([]*MinimalOrg, error) {
func GetUserOrgsList(doer *User) ([]*MinimalOrg, error) {
var orgs = make([]*MinimalOrg, 0, 20)
return orgs, x.Select("id, name, full_name, visibility, avatar, avatar_email, use_custom_avatar").
sess := x.NewSession()
defer sess.Close()

err := sess.Select("id, name, full_name, visibility, avatar, avatar_email, use_custom_avatar").
Table("user").
In("id", queryUserOrgIDs(uid)).
In("id", queryUserOrgIDs(doer.ID)).
Find(&orgs)

if err != nil {
return nil, err
}

for _, org := range orgs {
cond := SearchRepositoryCondition(&SearchRepoOptions{
Actor: doer,
Private: true,
OwnerID: org.ID,
AllPublic: false, // Include also all public repositories of users and public organisations
AllLimited: false, // Include also all public repositories of limited organisations
})
org.NumRepos, err = countRepositoryByCondition(x, cond)
if err != nil {
return nil, err
}
}
Comment on lines +449 to +461
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this is wildly inefficient - imagine you're the kind of person that has 500 organizations.

We should just do this in one query with a couple of joins - likely including the in at 442.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@a1012112796 a1012112796 Aug 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if too many organizations exist, maybe should paging...
Because of my poor sql knowledge, I don't know how to jon the count result with Organization. feel free to do it if you like. I have to stop this pull request now. Thanks.


return orgs, nil
}

func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) {
Expand Down
7 changes: 7 additions & 0 deletions models/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,13 @@ func SearchRepository(opts *SearchRepoOptions) (RepositoryList, int64, error) {
return SearchRepositoryByCondition(opts, cond, true)
}

func countRepositoryByCondition(e Engine, cond builder.Cond) (int, error) {
count, err := e.
Where(cond).
Count(new(Repository))
return int(count), err
}

// SearchRepositoryByCondition search repositories by condition
func SearchRepositoryByCondition(opts *SearchRepoOptions, cond builder.Cond, loadAttributes bool) (RepositoryList, int64, error) {
sess, count, err := searchRepositoryByCondition(opts, cond)
Expand Down
3 changes: 2 additions & 1 deletion routers/web/user/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ func getDashboardContextUser(ctx *context.Context) *models.User {
}
ctx.Data["ContextUser"] = ctxUser

orgs, err := models.GetUserOrgsList(ctx.User.ID)
orgs, err := models.GetUserOrgsList(ctx.User)
if err != nil {
ctx.ServerError("GetUserOrgsList", err)
return nil
}

ctx.Data["Orgs"] = orgs

return ctxUser
Expand Down