Skip to content

Commit f949f9e

Browse files
authored
Correctly return the number of Repositories for Organizations (#16807) (#16911)
Backport #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 <art27@cantab.net>
1 parent cbe3ca5 commit f949f9e

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

models/org.go

+56-12
Original file line numberDiff line numberDiff line change
@@ -425,23 +425,67 @@ func GetOrgsByUserID(userID int64, showAll bool) ([]*User, error) {
425425
return getOrgsByUserID(sess, userID, showAll)
426426
}
427427

428-
// queryUserOrgIDs returns a condition to return user's organization id
429-
func queryUserOrgIDs(uid int64) *builder.Builder {
430-
return builder.Select("team.org_id").
431-
From("team_user").InnerJoin("team", "team.id = team_user.team_id").
432-
Where(builder.Eq{"team_user.uid": uid})
433-
}
434-
435428
// MinimalOrg represents a simple orgnization with only needed columns
436429
type MinimalOrg = User
437430

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

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

routers/web/user/home.go

+1-1
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)