diff options
author | zeripath <art27@cantab.net> | 2020-05-11 23:04:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-12 01:04:08 +0300 |
commit | c42c31a111756c931292a404feabf24750d886e0 (patch) | |
tree | 6fa3a7bd324d1d4037d26d67415159990c0b4e19 /models/user.go | |
parent | 45968b9f44516d940841b16fc833111f2b5ffbdb (diff) | |
download | gitea-c42c31a111756c931292a404feabf24750d886e0.tar.gz gitea-c42c31a111756c931292a404feabf24750d886e0.zip |
Correctly set the organization num repos (#11339)
* Correctly set the organization num repos
Correctly set the organization num repos to the number of
accessible repos for the user
Fix #11194
Signed-off-by: Andrew Thornton <art27@cantab.net>
* as per @lunny
Signed-off-by: Andrew Thornton <art27@cantab.net>
* attempt to fix mssql
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Update models/user.go
* Explicit columns
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Add test and fix 0 counted orgs
Signed-off-by: Andrew Thornton <art27@cantab.net>
* remove orgname from api
Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'models/user.go')
-rw-r--r-- | models/user.go | 48 |
1 files changed, 41 insertions, 7 deletions
diff --git a/models/user.go b/models/user.go index a89eb144ce..01cce2506d 100644 --- a/models/user.go +++ b/models/user.go @@ -712,18 +712,52 @@ 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) + sess := x.NewSession() + defer sess.Close() + + schema, err := x.TableInfo(new(User)) if err != nil { return err } + groupByCols := &strings.Builder{} + for _, col := range schema.Columns() { + fmt.Fprintf(groupByCols, "`%s`.%s,", schema.Name, col.Name) + } + groupByStr := groupByCols.String() + groupByStr = groupByStr[0 : len(groupByStr)-1] - 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(repo_id) as org_count"). + Table("user"). + Join("INNER", "org_user", "`org_user`.org_id=`user`.id"). + Join("LEFT", builder. + Select("id as repo_id, owner_id as repo_owner_id"). + From("repository"). + Where(accessibleRepositoryCondition(u)), "`repository`.repo_owner_id = `org_user`.org_id"). + And("`org_user`.uid=?", u.ID). + GroupBy(groupByStr) + if opts.PageSize != 0 { + sess = opts.setSessionPagination(sess) } + 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 nil } |