Skip to content

Commit 57b0887

Browse files
zeripath6543techknowlogick
authored
Correctly return the number of Repositories for Organizations (#16807)
Calculate and return the number of Repositories on the dashboard Organization list. This PR restores some of the logic that was removed in #14032 to calculate the number of repos on the dashboard orgs list. Fix #16648 Replaces #16799 Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: 6543 <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 83640a5 commit 57b0887

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

models/org.go

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -421,23 +421,67 @@ func GetOrgsByUserID(userID int64, showAll bool) ([]*User, error) {
421421
return getOrgsByUserID(sess, userID, showAll)
422422
}
423423

424-
// queryUserOrgIDs returns a condition to return user's organization id
425-
func queryUserOrgIDs(uid int64) *builder.Builder {
426-
return builder.Select("team.org_id").
427-
From("team_user").InnerJoin("team", "team.id = team_user.team_id").
428-
Where(builder.Eq{"team_user.uid": uid})
429-
}
430-
431424
// MinimalOrg represents a simple orgnization with only needed columns
432425
type MinimalOrg = User
433426

434427
// GetUserOrgsList returns one user's all orgs list
435-
func GetUserOrgsList(uid int64) ([]*MinimalOrg, error) {
436-
var orgs = make([]*MinimalOrg, 0, 20)
437-
return orgs, x.Select("id, name, full_name, visibility, avatar, avatar_email, use_custom_avatar").
428+
func GetUserOrgsList(user *User) ([]*MinimalOrg, error) {
429+
sess := x.NewSession()
430+
defer sess.Close()
431+
432+
schema, err := x.TableInfo(new(User))
433+
if err != nil {
434+
return nil, err
435+
}
436+
437+
outputCols := []string{
438+
"id",
439+
"name",
440+
"full_name",
441+
"visibility",
442+
"avatar",
443+
"avatar_email",
444+
"use_custom_avatar",
445+
}
446+
447+
groupByCols := &strings.Builder{}
448+
for _, col := range outputCols {
449+
fmt.Fprintf(groupByCols, "`%s`.%s,", schema.Name, col)
450+
}
451+
groupByStr := groupByCols.String()
452+
groupByStr = groupByStr[0 : len(groupByStr)-1]
453+
454+
sess.Select(groupByStr+", count(repo_id) as org_count").
438455
Table("user").
439-
In("id", queryUserOrgIDs(uid)).
440-
Find(&orgs)
456+
Join("INNER", "team", "`team`.org_id = `user`.id").
457+
Join("INNER", "team_user", "`team`.id = `team_user`.team_id").
458+
Join("LEFT", builder.
459+
Select("id as repo_id, owner_id as repo_owner_id").
460+
From("repository").
461+
Where(accessibleRepositoryCondition(user)), "`repository`.repo_owner_id = `team`.org_id").
462+
Where("`team_user`.uid = ?", user.ID).
463+
GroupBy(groupByStr)
464+
465+
type OrgCount struct {
466+
User `xorm:"extends"`
467+
OrgCount int
468+
}
469+
470+
orgCounts := make([]*OrgCount, 0, 10)
471+
472+
if err := sess.
473+
Asc("`user`.name").
474+
Find(&orgCounts); err != nil {
475+
return nil, err
476+
}
477+
478+
orgs := make([]*MinimalOrg, len(orgCounts))
479+
for i, orgCount := range orgCounts {
480+
orgCount.User.NumRepos = orgCount.OrgCount
481+
orgs[i] = &orgCount.User
482+
}
483+
484+
return orgs, nil
441485
}
442486

443487
func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) {

routers/web/user/home.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func getDashboardContextUser(ctx *context.Context) *models.User {
4949
}
5050
ctx.Data["ContextUser"] = ctxUser
5151

52-
orgs, err := models.GetUserOrgsList(ctx.User.ID)
52+
orgs, err := models.GetUserOrgsList(ctx.User)
5353
if err != nil {
5454
ctx.ServerError("GetUserOrgsList", err)
5555
return nil

0 commit comments

Comments
 (0)