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

Correctly set the organization num repos #11339

Merged
merged 9 commits into from
May 11, 2020
41 changes: 32 additions & 9 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,19 +712,42 @@ func (u *User) GetOwnedOrganizations() (err error) {

// GetOrganizations returns paginated organizations that user belongs to.
func (u *User) GetOrganizations(opts *SearchOrganizationsOptions) error {
ous, err := GetOrgUsersByUserID(u.ID, opts)
if err != nil {
sess := x.NewSession()
zeripath marked this conversation as resolved.
Show resolved Hide resolved
if err := sess.Begin(); err != nil {
return err
}

u.Orgs = make([]*User, len(ous))
for i, ou := range ous {
u.Orgs[i], err = GetUserByID(ou.OrgID)
if err != nil {
return err
}
sess.Select("`user`.*, count(`repository`.id) as org_count").
Table("user").
Join("INNER", "org_user", "`org_user`.org_id=`user`.id").
Join("INNER", "repository", "`repository`.owner_id = `org_user`.org_id").
zeripath marked this conversation as resolved.
Show resolved Hide resolved
Where(accessibleRepositoryCondition(u)).
And("`org_user`.uid=?", u.ID).
GroupBy("`repository`.owner_id")
if opts.PageSize != 0 {
sess = opts.setSessionPagination(sess)
}
return nil
type OrgCount struct {
User `xorm:"extends"`
OrgCount int
}
orgCounts := make([]*OrgCount, 0, 10)

if err := sess.
Asc("`user`.name").
Find(&orgCounts); err != nil {
return err
}

orgs := make([]*User, len(orgCounts))
for i, orgCount := range orgCounts {
orgCount.User.NumRepos = orgCount.OrgCount
orgs[i] = &orgCount.User
}

u.Orgs = orgs

return sess.Commit()
}

// DisplayName returns full name if it's not empty,
Expand Down