From 8059175a5c0363519ba28fb8fc5f9d494f6a209d Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 27 Dec 2016 22:01:18 +0800 Subject: [PATCH] Fix dashboard issues/pull request counting --- .gopmfile | 2 +- glide.lock | 2 +- models/issue.go | 12 +++++-- models/repo.go | 49 ++++++++++++++++++++++++---- models/user.go | 7 +++- routers/api/v1/repo/repo.go | 7 +++- routers/user/home.go | 31 +++++++++++++++--- routers/user/profile.go | 7 +++- templates/user/dashboard/issues.tmpl | 8 ++++- 9 files changed, 104 insertions(+), 21 deletions(-) diff --git a/.gopmfile b/.gopmfile index 73072b5fd..278fbdff9 100644 --- a/.gopmfile +++ b/.gopmfile @@ -16,7 +16,7 @@ github.com/go-macaron/toolbox = commit:82b5115 github.com/go-sql-driver/mysql = commit:a0583e0 github.com/go-xorm/builder = commit:db75972 github.com/go-xorm/core = commit:87aca22 -github.com/go-xorm/xorm = commit:3ad0b42 +github.com/go-xorm/xorm = commit:d75356f github.com/gogits/chardet = commit:2404f77 github.com/gogits/cron = commit:2fc07a4 github.com/gogits/git-module = commit:df1013f diff --git a/glide.lock b/glide.lock index 3b84c362d..a64816b03 100644 --- a/glide.lock +++ b/glide.lock @@ -37,7 +37,7 @@ imports: - name: github.com/go-xorm/core version: 87aca223378aab7a4bf31ef53f20fde4997ad793 - name: github.com/go-xorm/xorm - version: 3ad0b428ae702d7d3f880c90a7f6d89805fcd2f7 + version: d75356fc733fce7683c6d961fc2aeb16ededc8ef - name: github.com/gogits/chardet version: 2404f777256163ea3eadb273dada5dcb037993c0 - name: github.com/gogits/cron diff --git a/models/issue.go b/models/issue.go index 8c602d785..d17bc2073 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1169,7 +1169,7 @@ func GetIssueStats(opts *IssueStatsOptions) *IssueStats { // GetUserIssueStats returns issue statistic information for dashboard by given conditions. func GetUserIssueStats(repoID, userID int64, repoIDs []int64, filterMode FilterMode, isPull bool) *IssueStats { stats := &IssueStats{} - + hasAnyRepo := repoID > 0 || repoIDs != nil countSession := func(isClosed, isPull bool, repoID int64, repoIDs []int64) *xorm.Session { sess := x.Where("issue.is_closed = ?", isClosed).And("issue.is_pull = ?", isPull) @@ -1190,11 +1190,17 @@ func GetUserIssueStats(repoID, userID int64, repoIDs []int64, filterMode FilterM And("poster_id = ?", userID). Count(new(Issue)) - stats.YourReposCount, _ = countSession(false, isPull, repoID, repoIDs). - Count(new(Issue)) + if hasAnyRepo { + stats.YourReposCount, _ = countSession(false, isPull, repoID, repoIDs). + Count(new(Issue)) + } switch filterMode { case FILTER_MODE_YOUR_REPOS: + if !hasAnyRepo { + break + } + stats.OpenCount, _ = countSession(false, isPull, repoID, repoIDs). Count(new(Issue)) stats.ClosedCount, _ = countSession(true, isPull, repoID, repoIDs). diff --git a/models/repo.go b/models/repo.go index e6f12b2f4..f9e526829 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1055,6 +1055,34 @@ func RepositoriesWithUsers(page, pageSize int) (_ []*Repository, err error) { return repos, nil } +// FilterRepositoryWithIssues selects repositories that are using interal issue tracker +// and has disabled external tracker from given set. +// It returns nil if result set is empty. +func FilterRepositoryWithIssues(repoIDs []int64) ([]int64, error) { + if len(repoIDs) == 0 { + return nil, nil + } + + repos := make([]*Repository, 0, len(repoIDs)) + if err := x.Where("enable_issues=?", true). + And("enable_external_tracker=?", false). + In("id", repoIDs). + Cols("id"). + Find(&repos); err != nil { + return nil, fmt.Errorf("filter valid repositories %v: %v", repoIDs, err) + } + + if len(repos) == 0 { + return nil, nil + } + + repoIDs = make([]int64, len(repos)) + for i := range repos { + repoIDs[i] = repos[i].ID + } + return repoIDs, nil +} + // RepoPath returns repository path by given user and repository name. func RepoPath(userName, repoName string) string { return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".git") @@ -1454,19 +1482,26 @@ func GetRepositoryByID(id int64) (*Repository, error) { return getRepositoryByID(x, id) } +type UserRepoOptions struct { + UserID int64 + Private bool + Page int + PageSize int +} + // GetUserRepositories returns a list of repositories of given user. -func GetUserRepositories(userID int64, private bool, page, pageSize int) ([]*Repository, error) { - sess := x.Where("owner_id = ?", userID).Desc("updated_unix") - if !private { +func GetUserRepositories(opts *UserRepoOptions) ([]*Repository, error) { + sess := x.Where("owner_id=?", opts.UserID).Desc("updated_unix") + if !opts.Private { sess.And("is_private=?", false) } - if page <= 0 { - page = 1 + if opts.Page <= 0 { + opts.Page = 1 } - sess.Limit(pageSize, (page-1)*pageSize) + sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize) - repos := make([]*Repository, 0, pageSize) + repos := make([]*Repository, 0, opts.PageSize) return repos, sess.Find(&repos) } diff --git a/models/user.go b/models/user.go index 7946915df..4b8423395 100644 --- a/models/user.go +++ b/models/user.go @@ -418,7 +418,12 @@ func (u *User) GetOrganizationCount() (int64, error) { // GetRepositories returns repositories that user owns, including private repositories. func (u *User) GetRepositories(page, pageSize int) (err error) { - u.Repos, err = GetUserRepositories(u.ID, true, page, pageSize) + u.Repos, err = GetUserRepositories(&UserRepoOptions{ + UserID: u.ID, + Private: true, + Page: page, + PageSize: pageSize, + }) return err } diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index cc75f5bf4..e36f18b00 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -78,7 +78,12 @@ func Search(ctx *context.APIContext) { // https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories func ListMyRepos(ctx *context.APIContext) { - ownRepos, err := models.GetUserRepositories(ctx.User.ID, true, 1, ctx.User.NumRepos) + ownRepos, err := models.GetUserRepositories(&models.UserRepoOptions{ + UserID: ctx.User.ID, + Private: true, + Page: 1, + PageSize: ctx.User.NumRepos, + }) if err != nil { ctx.Error(500, "GetRepositories", err) return diff --git a/routers/user/home.go b/routers/user/home.go index d1a583562..f413a223b 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -218,6 +218,12 @@ func Issues(ctx *context.Context) { userRepoIDs = make([]int64, 0, len(repos)) for _, repo := range repos { + userRepoIDs = append(userRepoIDs, repo.ID) + + if filterMode != models.FILTER_MODE_YOUR_REPOS { + continue + } + if isPullList { if isShowClosed && repo.NumClosedPulls == 0 || !isShowClosed && repo.NumOpenPulls == 0 { @@ -231,9 +237,15 @@ func Issues(ctx *context.Context) { } } - userRepoIDs = append(userRepoIDs, repo.ID) - if filterMode == models.FILTER_MODE_YOUR_REPOS { - showRepos = append(showRepos, repo) + showRepos = append(showRepos, repo) + } + + // Filter repositories if the page shows issues. + if !isPullList { + userRepoIDs, err = models.FilterRepositoryWithIssues(userRepoIDs) + if err != nil { + ctx.Handle(500, "FilterRepositoryWithIssues", err) + return } } @@ -247,7 +259,11 @@ func Issues(ctx *context.Context) { switch filterMode { case models.FILTER_MODE_YOUR_REPOS: // Get all issues from repositories from this user. - issueOptions.RepoIDs = userRepoIDs + if userRepoIDs == nil { + issueOptions.RepoIDs = []int64{-1} + } else { + issueOptions.RepoIDs = userRepoIDs + } case models.FILTER_MODE_ASSIGN: // Get all issues assigned to this user. @@ -361,7 +377,12 @@ func showOrgProfile(ctx *context.Context) { ctx.Data["Repos"] = repos } else { showPrivate := ctx.IsSigned && ctx.User.IsAdmin - repos, err = models.GetUserRepositories(org.ID, showPrivate, page, setting.UI.User.RepoPagingNum) + repos, err = models.GetUserRepositories(&models.UserRepoOptions{ + UserID: org.ID, + Private: showPrivate, + Page: page, + PageSize: setting.UI.User.RepoPagingNum, + }) if err != nil { ctx.Handle(500, "GetRepositories", err) return diff --git a/routers/user/profile.go b/routers/user/profile.go index 363705dd0..3558e8b0c 100644 --- a/routers/user/profile.go +++ b/routers/user/profile.go @@ -99,7 +99,12 @@ func Profile(ctx *context.Context) { page = 1 } - ctx.Data["Repos"], err = models.GetUserRepositories(ctxUser.ID, ctx.IsSigned && ctx.User.ID == ctxUser.ID, page, setting.UI.User.RepoPagingNum) + ctx.Data["Repos"], err = models.GetUserRepositories(&models.UserRepoOptions{ + UserID: ctxUser.ID, + Private: ctx.IsSigned && ctx.User.ID == ctxUser.ID, + Page: page, + PageSize: setting.UI.User.RepoPagingNum, + }) if err != nil { ctx.Handle(500, "GetRepositories", err) return diff --git a/templates/user/dashboard/issues.tmpl b/templates/user/dashboard/issues.tmpl index adcbfb6d5..c2ebd0a06 100644 --- a/templates/user/dashboard/issues.tmpl +++ b/templates/user/dashboard/issues.tmpl @@ -23,7 +23,13 @@ {{range .Repos}} {{.FullName}} -
{{if $.IsShowClosed}}{{.NumClosedIssues}}{{else}}{{.NumOpenIssues}}{{end}}
+
+ {{if $.PageIsIssues}} + {{if $.IsShowClosed}}{{.NumClosedIssues}}{{else}}{{.NumOpenIssues}}{{end}} + {{else}} + {{if $.IsShowClosed}}{{.NumClosedPulls}}{{else}}{{.NumOpenPulls}}{{end}} + {{end}} +
{{end}}