Browse Source

Fix dashboard issues/pull request counting

Unknwon 8 years ago
parent
commit
8059175a5c
9 changed files with 104 additions and 21 deletions
  1. 1 1
      .gopmfile
  2. 1 1
      glide.lock
  3. 9 3
      models/issue.go
  4. 42 7
      models/repo.go
  5. 6 1
      models/user.go
  6. 6 1
      routers/api/v1/repo/repo.go
  7. 26 5
      routers/user/home.go
  8. 6 1
      routers/user/profile.go
  9. 7 1
      templates/user/dashboard/issues.tmpl

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

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

+ 9 - 3
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).

+ 42 - 7
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)
 }
 

+ 6 - 1
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
 }
 

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

+ 26 - 5
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

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

+ 7 - 1
templates/user/dashboard/issues.tmpl

@@ -23,7 +23,13 @@
 					{{range .Repos}}
 						<a class="{{if eq $.RepoID .ID}}ui basic blue button{{end}} repo name item" href="{{$.Link}}?type={{$.ViewType}}{{if not (eq $.RepoID .ID)}}&repo={{.ID}}{{end}}&sort={{$.SortType}}&state={{$.State}}">
 							<span class="text truncate">{{.FullName}}</span>
-							<div class="floating ui {{if $.IsShowClosed}}red{{else}}green{{end}} label">{{if $.IsShowClosed}}{{.NumClosedIssues}}{{else}}{{.NumOpenIssues}}{{end}}</div>
+							<div class="floating ui {{if $.IsShowClosed}}red{{else}}green{{end}} label">
+							{{if $.PageIsIssues}}
+								{{if $.IsShowClosed}}{{.NumClosedIssues}}{{else}}{{.NumOpenIssues}}{{end}}
+							{{else}}
+								{{if $.IsShowClosed}}{{.NumClosedPulls}}{{else}}{{.NumOpenPulls}}{{end}}
+							{{end}}
+							</div>
 						</a>
 					{{end}}
 				</div>