Browse Source

Minor code fix for PR #3560

Unknwon 8 years ago
parent
commit
d528704503
8 changed files with 117 additions and 147 deletions
  1. 1 1
      README.md
  2. 1 1
      gogs.go
  3. 35 34
      models/issue.go
  4. 2 2
      models/repo.go
  5. 5 5
      routers/repo/issue.go
  6. 68 99
      routers/user/home.go
  7. 1 1
      templates/.VERSION
  8. 4 4
      templates/user/dashboard/issues.tmpl

+ 1 - 1
README.md

@@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra
 
 ![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true)
 
-##### Current tip version: 0.9.113 (see [Releases](https://github.com/gogits/gogs/releases) for binary versions ~~or submit a task on [alpha stage automated binary building system](https://build.gogs.io/)~~)
+##### Current tip version: 0.9.114 (see [Releases](https://github.com/gogits/gogs/releases) for binary versions ~~or submit a task on [alpha stage automated binary building system](https://build.gogs.io/)~~)
 
 | Web | UI  | Preview  |
 |:-------------:|:-------:|:-------:|

+ 1 - 1
gogs.go

@@ -17,7 +17,7 @@ import (
 	"github.com/gogits/gogs/modules/setting"
 )
 
-const APP_VER = "0.9.113.1223"
+const APP_VER = "0.9.114.1227"
 
 func init() {
 	runtime.GOMAXPROCS(runtime.NumCPU())

+ 35 - 34
models/issue.go

@@ -1004,17 +1004,17 @@ func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*Issue
 }
 
 // GetIssueUserPairsByMode returns issue-user pairs by given repository and user.
-func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) {
+func GetIssueUserPairsByMode(userID, repoID int64, filterMode FilterMode, isClosed bool, page int) ([]*IssueUser, error) {
 	ius := make([]*IssueUser, 0, 10)
-	sess := x.Limit(20, (page-1)*20).Where("uid=?", uid).And("is_closed=?", isClosed)
-	if rid > 0 {
-		sess.And("repo_id=?", rid)
+	sess := x.Limit(20, (page-1)*20).Where("uid=?", userID).And("is_closed=?", isClosed)
+	if repoID > 0 {
+		sess.And("repo_id=?", repoID)
 	}
 
 	switch filterMode {
-	case FM_ASSIGN:
+	case FILTER_MODE_ASSIGN:
 		sess.And("is_assigned=?", true)
-	case FM_CREATE:
+	case FILTER_MODE_CREATE:
 		sess.And("is_poster=?", true)
 	default:
 		return ius, nil
@@ -1069,18 +1069,19 @@ func updateIssueMentions(e Engine, issueID int64, mentions []string) error {
 // IssueStats represents issue statistic information.
 type IssueStats struct {
 	OpenCount, ClosedCount int64
-	YourRepositoriesCount  int64
+	YourReposCount         int64
 	AssignCount            int64
 	CreateCount            int64
 	MentionCount           int64
 }
 
-// Filter modes.
+type FilterMode string
+
 const (
-	FM_YOUR_REPOSITORIES = iota
-	FM_ASSIGN
-	FM_CREATE
-	FM_MENTION
+	FILTER_MODE_YOUR_REPOS FilterMode = "your_repositories"
+	FILTER_MODE_ASSIGN     FilterMode = "assigned"
+	FILTER_MODE_CREATE     FilterMode = "created_by"
+	FILTER_MODE_MENTION    FilterMode = "mentioned"
 )
 
 func parseCountResult(results []map[string][]byte) int64 {
@@ -1099,7 +1100,7 @@ type IssueStatsOptions struct {
 	Labels      string
 	MilestoneID int64
 	AssigneeID  int64
-	FilterMode  int
+	FilterMode  FilterMode
 	IsPull      bool
 }
 
@@ -1129,7 +1130,7 @@ func GetIssueStats(opts *IssueStatsOptions) *IssueStats {
 	}
 
 	switch opts.FilterMode {
-	case FM_YOUR_REPOSITORIES, FM_ASSIGN:
+	case FILTER_MODE_YOUR_REPOS, FILTER_MODE_ASSIGN:
 		stats.OpenCount, _ = countSession(opts).
 			And("is_closed = ?", false).
 			Count(new(Issue))
@@ -1137,7 +1138,7 @@ func GetIssueStats(opts *IssueStatsOptions) *IssueStats {
 		stats.ClosedCount, _ = countSession(opts).
 			And("is_closed = ?", true).
 			Count(new(Issue))
-	case FM_CREATE:
+	case FILTER_MODE_CREATE:
 		stats.OpenCount, _ = countSession(opts).
 			And("poster_id = ?", opts.UserID).
 			And("is_closed = ?", false).
@@ -1147,7 +1148,7 @@ func GetIssueStats(opts *IssueStatsOptions) *IssueStats {
 			And("poster_id = ?", opts.UserID).
 			And("is_closed = ?", true).
 			Count(new(Issue))
-	case FM_MENTION:
+	case FILTER_MODE_MENTION:
 		stats.OpenCount, _ = countSession(opts).
 			Join("INNER", "issue_user", "issue.id = issue_user.issue_id").
 			And("issue_user.uid = ?", opts.UserID).
@@ -1166,7 +1167,7 @@ func GetIssueStats(opts *IssueStatsOptions) *IssueStats {
 }
 
 // GetUserIssueStats returns issue statistic information for dashboard by given conditions.
-func GetUserIssueStats(repoID, uid int64, repoIDs []int64, filterMode int, isPull bool) *IssueStats {
+func GetUserIssueStats(repoID, userID int64, repoIDs []int64, filterMode FilterMode, isPull bool) *IssueStats {
 	stats := &IssueStats{}
 
 	countSession := func(isClosed, isPull bool, repoID int64, repoIDs []int64) *xorm.Session {
@@ -1182,35 +1183,35 @@ func GetUserIssueStats(repoID, uid int64, repoIDs []int64, filterMode int, isPul
 	}
 
 	stats.AssignCount, _ = countSession(false, isPull, repoID, nil).
-		And("assignee_id = ?", uid).
+		And("assignee_id = ?", userID).
 		Count(new(Issue))
 
 	stats.CreateCount, _ = countSession(false, isPull, repoID, nil).
-		And("poster_id = ?", uid).
+		And("poster_id = ?", userID).
 		Count(new(Issue))
 
-	stats.YourRepositoriesCount, _ = countSession(false, isPull, repoID, repoIDs).
+	stats.YourReposCount, _ = countSession(false, isPull, repoID, repoIDs).
 		Count(new(Issue))
 
 	switch filterMode {
-	case FM_YOUR_REPOSITORIES:
+	case FILTER_MODE_YOUR_REPOS:
 		stats.OpenCount, _ = countSession(false, isPull, repoID, repoIDs).
 			Count(new(Issue))
 		stats.ClosedCount, _ = countSession(true, isPull, repoID, repoIDs).
 			Count(new(Issue))
-	case FM_ASSIGN:
+	case FILTER_MODE_ASSIGN:
 		stats.OpenCount, _ = countSession(false, isPull, repoID, nil).
-			And("assignee_id = ?", uid).
+			And("assignee_id = ?", userID).
 			Count(new(Issue))
 		stats.ClosedCount, _ = countSession(true, isPull, repoID, nil).
-			And("assignee_id = ?", uid).
+			And("assignee_id = ?", userID).
 			Count(new(Issue))
-	case FM_CREATE:
+	case FILTER_MODE_CREATE:
 		stats.OpenCount, _ = countSession(false, isPull, repoID, nil).
-			And("poster_id = ?", uid).
+			And("poster_id = ?", userID).
 			Count(new(Issue))
 		stats.ClosedCount, _ = countSession(true, isPull, repoID, nil).
-			And("poster_id = ?", uid).
+			And("poster_id = ?", userID).
 			Count(new(Issue))
 	}
 
@@ -1218,7 +1219,7 @@ func GetUserIssueStats(repoID, uid int64, repoIDs []int64, filterMode int, isPul
 }
 
 // GetRepoIssueStats returns number of open and closed repository issues by given filter mode.
-func GetRepoIssueStats(repoID, uid int64, filterMode int, isPull bool) (numOpen int64, numClosed int64) {
+func GetRepoIssueStats(repoID, userID int64, filterMode FilterMode, isPull bool) (numOpen int64, numClosed int64) {
 	countSession := func(isClosed, isPull bool, repoID int64) *xorm.Session {
 		sess := x.Where("issue.repo_id = ?", isClosed).
 			And("is_pull = ?", isPull).
@@ -1231,12 +1232,12 @@ func GetRepoIssueStats(repoID, uid int64, filterMode int, isPull bool) (numOpen
 	closedCountSession := countSession(true, isPull, repoID)
 
 	switch filterMode {
-	case FM_ASSIGN:
-		openCountSession.And("assignee_id = ?", uid)
-		closedCountSession.And("assignee_id = ?", uid)
-	case FM_CREATE:
-		openCountSession.And("poster_id = ?", uid)
-		closedCountSession.And("poster_id = ?", uid)
+	case FILTER_MODE_ASSIGN:
+		openCountSession.And("assignee_id = ?", userID)
+		closedCountSession.And("assignee_id = ?", userID)
+	case FILTER_MODE_CREATE:
+		openCountSession.And("poster_id = ?", userID)
+		closedCountSession.And("poster_id = ?", userID)
 	}
 
 	openResult, _ := openCountSession.Count(new(Issue))

+ 2 - 2
models/repo.go

@@ -376,8 +376,8 @@ func (repo *Repository) GetMilestoneByID(milestoneID int64) (*Milestone, error)
 }
 
 // IssueStats returns number of open and closed repository issues by given filter mode.
-func (repo *Repository) IssueStats(uid int64, filterMode int, isPull bool) (int64, int64) {
-	return GetRepoIssueStats(repo.ID, uid, filterMode, isPull)
+func (repo *Repository) IssueStats(userID int64, filterMode FilterMode, isPull bool) (int64, int64) {
+	return GetRepoIssueStats(repo.ID, userID, filterMode, isPull)
 }
 
 func (repo *Repository) GetMirror() (err error) {

+ 5 - 5
routers/repo/issue.go

@@ -121,16 +121,16 @@ func Issues(ctx *context.Context) {
 		assigneeID = ctx.QueryInt64("assignee")
 		posterID   int64
 	)
-	filterMode := models.FM_YOUR_REPOSITORIES
+	filterMode := models.FILTER_MODE_YOUR_REPOS
 	switch viewType {
 	case "assigned":
-		filterMode = models.FM_ASSIGN
+		filterMode = models.FILTER_MODE_ASSIGN
 		assigneeID = ctx.User.ID
 	case "created_by":
-		filterMode = models.FM_CREATE
+		filterMode = models.FILTER_MODE_CREATE
 		posterID = ctx.User.ID
 	case "mentioned":
-		filterMode = models.FM_MENTION
+		filterMode = models.FILTER_MODE_MENTION
 	}
 
 	var uid int64 = -1
@@ -174,7 +174,7 @@ func Issues(ctx *context.Context) {
 		MilestoneID: milestoneID,
 		Page:        pager.Current(),
 		IsClosed:    isShowClosed,
-		IsMention:   filterMode == models.FM_MENTION,
+		IsMention:   filterMode == models.FILTER_MODE_MENTION,
 		IsPull:      isPullList,
 		Labels:      selectLabels,
 		SortType:    sortType,

+ 68 - 99
routers/user/home.go

@@ -168,29 +168,23 @@ func Issues(ctx *context.Context) {
 		return
 	}
 
-	// Organization does not have view type and filter mode.
 	var (
-		viewType   string
 		sortType   = ctx.Query("sort")
-		filterMode = models.FM_YOUR_REPOSITORIES
+		filterMode = models.FILTER_MODE_YOUR_REPOS
 	)
-	if ctxUser.IsOrganization() {
-		viewType = "your_repositories"
-	} else {
-		viewType = ctx.Query("type")
-		types := []string{"your_repositories", "assigned", "created_by"}
-		if !com.IsSliceContainsStr(types, viewType) {
-			viewType = "your_repositories"
-		}
 
-		switch viewType {
-		case "your_repositories":
-			filterMode = models.FM_YOUR_REPOSITORIES
-		case "assigned":
-			filterMode = models.FM_ASSIGN
-		case "created_by":
-			filterMode = models.FM_CREATE
+	// Note: Organization does not have view type and filter mode.
+	if !ctxUser.IsOrganization() {
+		viewType := ctx.Query("type")
+		types := []string{
+			string(models.FILTER_MODE_YOUR_REPOS),
+			string(models.FILTER_MODE_ASSIGN),
+			string(models.FILTER_MODE_CREATE),
+		}
+		if !com.IsSliceContainsStr(types, viewType) {
+			viewType = string(models.FILTER_MODE_YOUR_REPOS)
 		}
+		filterMode = models.FilterMode(viewType)
 	}
 
 	page := ctx.QueryInt("page")
@@ -202,86 +196,83 @@ func Issues(ctx *context.Context) {
 	isShowClosed := ctx.Query("state") == "closed"
 
 	// Get repositories.
-	var err error
-	var repos []*models.Repository
-	userRepoIDs := make([]int64, 0, len(repos))
-	if ctxUser.IsOrganization() {
-		repos, _, err = ctxUser.GetUserRepositories(ctx.User.ID, 1, ctxUser.NumRepos)
-		if err != nil {
-			ctx.Handle(500, "GetRepositories", err)
-			return
-		}
-	} else {
-		if err := ctxUser.GetRepositories(1, ctx.User.NumRepos); err != nil {
-			ctx.Handle(500, "GetRepositories", err)
-			return
+	var (
+		err         error
+		repos       []*models.Repository
+		userRepoIDs []int64
+		showRepos   = make([]*models.Repository, 0, 10)
+	)
+	if filterMode == models.FILTER_MODE_YOUR_REPOS {
+		if ctxUser.IsOrganization() {
+			repos, _, err = ctxUser.GetUserRepositories(ctx.User.ID, 1, ctxUser.NumRepos)
+			if err != nil {
+				ctx.Handle(500, "GetRepositories", err)
+				return
+			}
+		} else {
+			if err := ctxUser.GetRepositories(1, ctx.User.NumRepos); err != nil {
+				ctx.Handle(500, "GetRepositories", err)
+				return
+			}
+			repos = ctxUser.Repos
 		}
-		repos = ctxUser.Repos
-	}
 
-	for _, repo := range repos {
-		if (isPullList && repo.NumPulls == 0) ||
-			(!isPullList &&
-				(!repo.EnableIssues || repo.EnableExternalTracker || repo.NumIssues == 0)) {
-			continue
-		}
+		userRepoIDs = make([]int64, 0, len(repos))
+		for _, repo := range repos {
+			if isPullList {
+				if isShowClosed && repo.NumClosedPulls == 0 ||
+					!isShowClosed && repo.NumOpenPulls == 0 {
+					continue
+				}
+			} else {
+				if !repo.EnableIssues || repo.EnableExternalTracker ||
+					isShowClosed && repo.NumClosedIssues == 0 ||
+					!isShowClosed && repo.NumOpenIssues == 0 {
+					continue
+				}
+			}
 
-		userRepoIDs = append(userRepoIDs, repo.ID)
+			userRepoIDs = append(userRepoIDs, repo.ID)
+			showRepos = append(showRepos, repo)
+		}
 	}
 
-	var issues []*models.Issue
+	issueOptions := &models.IssuesOptions{
+		RepoID:   repoID,
+		Page:     page,
+		IsClosed: isShowClosed,
+		IsPull:   isPullList,
+		SortType: sortType,
+	}
 	switch filterMode {
-	case models.FM_YOUR_REPOSITORIES:
+	case models.FILTER_MODE_YOUR_REPOS:
 		// Get all issues from repositories from this user.
-		issues, err = models.Issues(&models.IssuesOptions{
-			RepoIDs:  userRepoIDs,
-			RepoID:   repoID,
-			Page:     page,
-			IsClosed: isShowClosed,
-			IsPull:   isPullList,
-			SortType: sortType,
-		})
-
-	case models.FM_ASSIGN:
+		issueOptions.RepoIDs = userRepoIDs
+
+	case models.FILTER_MODE_ASSIGN:
 		// Get all issues assigned to this user.
-		issues, err = models.Issues(&models.IssuesOptions{
-			RepoID:     repoID,
-			AssigneeID: ctxUser.ID,
-			Page:       page,
-			IsClosed:   isShowClosed,
-			IsPull:     isPullList,
-			SortType:   sortType,
-		})
-
-	case models.FM_CREATE:
+		issueOptions.AssigneeID = ctxUser.ID
+
+	case models.FILTER_MODE_CREATE:
 		// Get all issues created by this user.
-		issues, err = models.Issues(&models.IssuesOptions{
-			RepoID:   repoID,
-			PosterID: ctxUser.ID,
-			Page:     page,
-			IsClosed: isShowClosed,
-			IsPull:   isPullList,
-			SortType: sortType,
-		})
+		issueOptions.PosterID = ctxUser.ID
 	}
 
+	issues, err := models.Issues(issueOptions)
 	if err != nil {
 		ctx.Handle(500, "Issues", err)
 		return
 	}
 
-	showRepos := make([]*models.Repository, 0, len(issues))
-	showReposSet := make(map[int64]bool)
-
 	if repoID > 0 {
 		repo, err := models.GetRepositoryByID(repoID)
 		if err != nil {
-			ctx.Handle(500, "GetRepositoryByID", fmt.Errorf("[#%d]%v", repoID, err))
+			ctx.Handle(500, "GetRepositoryByID", fmt.Errorf("[#%d] %v", repoID, err))
 			return
 		}
 
 		if err = repo.GetOwner(); err != nil {
-			ctx.Handle(500, "GetOwner", fmt.Errorf("[#%d]%v", repoID, err))
+			ctx.Handle(500, "GetOwner", fmt.Errorf("[#%d] %v", repoID, err))
 			return
 		}
 
@@ -290,35 +281,13 @@ func Issues(ctx *context.Context) {
 			ctx.Handle(404, "Issues", fmt.Errorf("#%d", repoID))
 			return
 		}
-
-		showReposSet[repoID] = true
-		showRepos = append(showRepos, repo)
 	}
 
 	for _, issue := range issues {
-		// Get Repository data.
-		issue.Repo, err = models.GetRepositoryByID(issue.RepoID)
-		if err != nil {
-			ctx.Handle(500, "GetRepositoryByID", fmt.Errorf("[#%d]%v", issue.RepoID, err))
-			return
-		}
-
-		// Get Owner data.
 		if err = issue.Repo.GetOwner(); err != nil {
-			ctx.Handle(500, "GetOwner", fmt.Errorf("[#%d]%v", issue.RepoID, err))
+			ctx.Handle(500, "GetOwner", fmt.Errorf("[#%d] %v", issue.RepoID, err))
 			return
 		}
-
-		// Append repo to list of shown repos
-		if filterMode == models.FM_YOUR_REPOSITORIES {
-			// Use a map to make sure we don't add the same Repository twice.
-			_, ok := showReposSet[issue.RepoID]
-			if !ok {
-				showReposSet[issue.RepoID] = true
-				// Append to list of shown Repositories.
-				showRepos = append(showRepos, issue.Repo)
-			}
-		}
 	}
 
 	issueStats := models.GetUserIssueStats(repoID, ctxUser.ID, userRepoIDs, filterMode, isPullList)
@@ -334,7 +303,7 @@ func Issues(ctx *context.Context) {
 	ctx.Data["Repos"] = showRepos
 	ctx.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5)
 	ctx.Data["IssueStats"] = issueStats
-	ctx.Data["ViewType"] = viewType
+	ctx.Data["ViewType"] = string(filterMode)
 	ctx.Data["SortType"] = sortType
 	ctx.Data["RepoID"] = repoID
 	ctx.Data["IsShowClosed"] = isShowClosed

+ 1 - 1
templates/.VERSION

@@ -1 +1 @@
-0.9.113.1223
+0.9.114.1227

+ 4 - 4
templates/user/dashboard/issues.tmpl

@@ -7,7 +7,7 @@
 				<div class="ui secondary vertical filter menu">
 					<a class="{{if eq .ViewType "your_repositories"}}ui basic blue button{{end}} item" href="{{.Link}}?type=your_repositories&repo={{.RepoID}}&sort={{$.SortType}}&state={{.State}}">
 						{{.i18n.Tr "home.issues.in_your_repos"}}
-						<strong class="ui right">{{.IssueStats.YourRepositoriesCount}}</strong>
+						<strong class="ui right">{{.IssueStats.YourReposCount}}</strong>
 					</a>
 					{{if not .ContextUser.IsOrganization}}
 						<a class="{{if eq .ViewType "assigned"}}ui basic blue button{{end}} item" href="{{.Link}}?type=assigned&repo={{.RepoID}}&sort={{$.SortType}}&state={{.State}}">
@@ -83,17 +83,17 @@
 						{{if gt .TotalPages 1}}
 							<div class="center page buttons">
 								<div class="ui borderless pagination menu">
-									<a class="{{if not .HasPrevious}}disabled{{end}} item" {{if .HasPrevious}}href="{{$.Link}}?type={{$.ViewType}}&state={{$.State}}&labels={{$.SelectLabels}}&milestone={{$.MilestoneID}}&assignee={{$.AssigneeID}}&page={{.Previous}}"{{end}}>
+									<a class="{{if not .HasPrevious}}disabled{{end}} item" {{if .HasPrevious}}href="{{$.Link}}?type={{$.ViewType}}&sort={{$.SortType}}&state={{$.State}}&labels={{$.SelectLabels}}&milestone={{$.MilestoneID}}&assignee={{$.AssigneeID}}&page={{.Previous}}"{{end}}>
 										<i class="left arrow icon"></i> {{$.i18n.Tr "repo.issues.previous"}}
 									</a>
 									{{range .Pages}}
 										{{if eq .Num -1}}
 											<a class="disabled item">...</a>
 										{{else}}
-											<a class="{{if .IsCurrent}}active{{end}} item" {{if not .IsCurrent}}href="{{$.Link}}?type={{$.ViewType}}&state={{$.State}}&labels={{$.SelectLabels}}&milestone={{$.MilestoneID}}&assignee={{$.AssigneeID}}&page={{.Num}}"{{end}}>{{.Num}}</a>
+											<a class="{{if .IsCurrent}}active{{end}} item" {{if not .IsCurrent}}href="{{$.Link}}?type={{$.ViewType}}&sort={{$.SortType}}&state={{$.State}}&labels={{$.SelectLabels}}&milestone={{$.MilestoneID}}&assignee={{$.AssigneeID}}&page={{.Num}}"{{end}}>{{.Num}}</a>
 										{{end}}
 									{{end}}
-									<a class="{{if not .HasNext}}disabled{{end}} item" {{if .HasNext}}href="{{$.Link}}?type={{$.ViewType}}&state={{$.State}}&labels={{$.SelectLabels}}&milestone={{$.MilestoneID}}&assignee={{$.AssigneeID}}&page={{.Next}}"{{end}}>
+									<a class="{{if not .HasNext}}disabled{{end}} item" {{if .HasNext}}href="{{$.Link}}?type={{$.ViewType}}&sort={{$.SortType}}&state={{$.State}}&labels={{$.SelectLabels}}&milestone={{$.MilestoneID}}&assignee={{$.AssigneeID}}&page={{.Next}}"{{end}}>
 										{{$.i18n.Tr "repo.issues.next"}} <i class="icon right arrow"></i>
 									</a>
 								</div>