From b461a05cf1aa081444bd884ab218e4f42bc328ab Mon Sep 17 00:00:00 2001 From: a1012112796 <1012112796@qq.com> Date: Tue, 24 Aug 2021 15:57:06 +0800 Subject: [PATCH] fix the number of Repository for Organization on dashboard page fix #16648 Signed-off-by: a1012112796 <1012112796@qq.com> --- models/org.go | 29 ++++++++++++++++++++++++++--- models/repo_list.go | 7 +++++++ routers/web/user/home.go | 3 ++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/models/org.go b/models/org.go index 3de6bd14b273c..8852f6176e883 100644 --- a/models/org.go +++ b/models/org.go @@ -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 + } + } + + return orgs, nil } func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) { diff --git a/models/repo_list.go b/models/repo_list.go index 772bd20be3871..6a1a1302e0286 100644 --- a/models/repo_list.go +++ b/models/repo_list.go @@ -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) diff --git a/routers/web/user/home.go b/routers/web/user/home.go index 397850f18bc96..678b529611b79 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -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