mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Fix counts on issues dashboard (#2215)
* Fix counts on issues dashboard * setupSess -> setupSession * Unit test * Load repo owners for issues
This commit is contained in:
		@@ -1057,6 +1057,7 @@ type IssuesOptions struct {
 | 
			
		||||
	MilestoneID int64
 | 
			
		||||
	RepoIDs     []int64
 | 
			
		||||
	Page        int
 | 
			
		||||
	PageSize    int
 | 
			
		||||
	IsClosed    util.OptionalBool
 | 
			
		||||
	IsPull      util.OptionalBool
 | 
			
		||||
	Labels      string
 | 
			
		||||
@@ -1085,21 +1086,16 @@ func sortIssuesSession(sess *xorm.Session, sortType string) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Issues returns a list of issues by given conditions.
 | 
			
		||||
func Issues(opts *IssuesOptions) ([]*Issue, error) {
 | 
			
		||||
	var sess *xorm.Session
 | 
			
		||||
	if opts.Page >= 0 {
 | 
			
		||||
func (opts *IssuesOptions) setupSession(sess *xorm.Session) error {
 | 
			
		||||
	if opts.Page >= 0 && opts.PageSize > 0 {
 | 
			
		||||
		var start int
 | 
			
		||||
		if opts.Page == 0 {
 | 
			
		||||
			start = 0
 | 
			
		||||
		} else {
 | 
			
		||||
			start = (opts.Page - 1) * setting.UI.IssuePagingNum
 | 
			
		||||
			start = (opts.Page - 1) * opts.PageSize
 | 
			
		||||
		}
 | 
			
		||||
		sess = x.Limit(setting.UI.IssuePagingNum, start)
 | 
			
		||||
	} else {
 | 
			
		||||
		sess = x.NewSession()
 | 
			
		||||
		sess.Limit(opts.PageSize, start)
 | 
			
		||||
	}
 | 
			
		||||
	defer sess.Close()
 | 
			
		||||
 | 
			
		||||
	if len(opts.IssueIDs) > 0 {
 | 
			
		||||
		sess.In("issue.id", opts.IssueIDs)
 | 
			
		||||
@@ -1144,12 +1140,10 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
 | 
			
		||||
		sess.And("issue.is_pull=?", false)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	sortIssuesSession(sess, opts.SortType)
 | 
			
		||||
 | 
			
		||||
	if len(opts.Labels) > 0 && opts.Labels != "0" {
 | 
			
		||||
		labelIDs, err := base.StringsToInt64s(strings.Split(opts.Labels, ","))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		if len(labelIDs) > 0 {
 | 
			
		||||
			sess.
 | 
			
		||||
@@ -1157,6 +1151,45 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
 | 
			
		||||
				In("issue_label.label_id", labelIDs)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CountIssuesByRepo map from repoID to number of issues matching the options
 | 
			
		||||
func CountIssuesByRepo(opts *IssuesOptions) (map[int64]int64, error) {
 | 
			
		||||
	sess := x.NewSession()
 | 
			
		||||
	defer sess.Close()
 | 
			
		||||
 | 
			
		||||
	if err := opts.setupSession(sess); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	countsSlice := make([]*struct {
 | 
			
		||||
		RepoID int64
 | 
			
		||||
		Count  int64
 | 
			
		||||
	}, 0, 10)
 | 
			
		||||
	if err := sess.GroupBy("issue.repo_id").
 | 
			
		||||
		Select("issue.repo_id AS repo_id, COUNT(*) AS count").
 | 
			
		||||
		Table("issue").
 | 
			
		||||
		Find(&countsSlice); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	countMap := make(map[int64]int64, len(countsSlice))
 | 
			
		||||
	for _, c := range countsSlice {
 | 
			
		||||
		countMap[c.RepoID] = c.Count
 | 
			
		||||
	}
 | 
			
		||||
	return countMap, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Issues returns a list of issues by given conditions.
 | 
			
		||||
func Issues(opts *IssuesOptions) ([]*Issue, error) {
 | 
			
		||||
	sess := x.NewSession()
 | 
			
		||||
	defer sess.Close()
 | 
			
		||||
 | 
			
		||||
	if err := opts.setupSession(sess); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	sortIssuesSession(sess, opts.SortType)
 | 
			
		||||
 | 
			
		||||
	issues := make([]*Issue, 0, setting.UI.IssuePagingNum)
 | 
			
		||||
	if err := sess.Find(&issues); err != nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -133,7 +133,6 @@ func populateIssueIndexer() error {
 | 
			
		||||
				RepoID:   repo.ID,
 | 
			
		||||
				IsClosed: util.OptionalBoolNone,
 | 
			
		||||
				IsPull:   util.OptionalBoolNone,
 | 
			
		||||
				Page:     -1, // do not page
 | 
			
		||||
			})
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return fmt.Errorf("Issues: %v", err)
 | 
			
		||||
 
 | 
			
		||||
@@ -8,11 +8,8 @@ import (
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-xorm/core"
 | 
			
		||||
	"github.com/go-xorm/xorm"
 | 
			
		||||
	_ "github.com/mattn/go-sqlite3" // for the test engine
 | 
			
		||||
	"github.com/stretchr/testify/assert"
 | 
			
		||||
	"gopkg.in/testfixtures.v2"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// TestFixturesAreConsistent assert that test fixtures are consistent
 | 
			
		||||
@@ -21,23 +18,8 @@ func TestFixturesAreConsistent(t *testing.T) {
 | 
			
		||||
	CheckConsistencyForAll(t)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateTestEngine create an xorm engine for testing
 | 
			
		||||
func CreateTestEngine() error {
 | 
			
		||||
	var err error
 | 
			
		||||
	x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	x.SetMapper(core.GonicMapper{})
 | 
			
		||||
	if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return InitFixtures(&testfixtures.SQLite{}, "fixtures/")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestMain(m *testing.M) {
 | 
			
		||||
	if err := CreateTestEngine(); err != nil {
 | 
			
		||||
	if err := CreateTestEngine("fixtures/"); err != nil {
 | 
			
		||||
		fmt.Printf("Error creating test engine: %v\n", err)
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -15,6 +15,11 @@ import (
 | 
			
		||||
// RepositoryList contains a list of repositories
 | 
			
		||||
type RepositoryList []*Repository
 | 
			
		||||
 | 
			
		||||
// RepositoryListOfMap make list from values of map
 | 
			
		||||
func RepositoryListOfMap(repoMap map[int64]*Repository) RepositoryList {
 | 
			
		||||
	return RepositoryList(valuesRepository(repoMap))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (repos RepositoryList) loadAttributes(e Engine) error {
 | 
			
		||||
	if len(repos) == 0 {
 | 
			
		||||
		return nil
 | 
			
		||||
 
 | 
			
		||||
@@ -7,13 +7,31 @@ package models
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-xorm/core"
 | 
			
		||||
	"github.com/go-xorm/xorm"
 | 
			
		||||
	"github.com/stretchr/testify/assert"
 | 
			
		||||
	"gopkg.in/testfixtures.v2"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// NonexistentID an ID that will never exist
 | 
			
		||||
const NonexistentID = 9223372036854775807
 | 
			
		||||
 | 
			
		||||
// CreateTestEngine create in-memory sqlite database for unit tests
 | 
			
		||||
// Any package that calls this must import github.com/mattn/go-sqlite3
 | 
			
		||||
func CreateTestEngine(fixturesDir string) error {
 | 
			
		||||
	var err error
 | 
			
		||||
	x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	x.SetMapper(core.GonicMapper{})
 | 
			
		||||
	if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return InitFixtures(&testfixtures.SQLite{}, fixturesDir)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PrepareTestDatabase load test fixtures into test database
 | 
			
		||||
func PrepareTestDatabase() error {
 | 
			
		||||
	return LoadFixtures()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user