summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorEthan Koenig <etk39@cornell.edu>2017-08-02 22:09:16 -0700
committerLunny Xiao <xiaolunwen@gmail.com>2017-08-03 13:09:16 +0800
commit7e0654bd9e4f90fc156884afd88cb82ad8df86a8 (patch)
tree03d320ee4a46d003ef5db801c1740b8d7ed9b966 /models
parentf29458bd3a20d2d89638d5031d801c161f456374 (diff)
downloadgitea-7e0654bd9e4f90fc156884afd88cb82ad8df86a8.tar.gz
gitea-7e0654bd9e4f90fc156884afd88cb82ad8df86a8.zip
Fix counts on issues dashboard (#2215)
* Fix counts on issues dashboard * setupSess -> setupSession * Unit test * Load repo owners for issues
Diffstat (limited to 'models')
-rw-r--r--models/issue.go57
-rw-r--r--models/issue_indexer.go1
-rw-r--r--models/main_test.go20
-rw-r--r--models/repo_list.go5
-rw-r--r--models/unit_tests.go18
5 files changed, 69 insertions, 32 deletions
diff --git a/models/issue.go b/models/issue.go
index d40b81eb32..709ebc35f9 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -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 {
diff --git a/models/issue_indexer.go b/models/issue_indexer.go
index c2cb8b429e..05b324f535 100644
--- a/models/issue_indexer.go
+++ b/models/issue_indexer.go
@@ -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)
diff --git a/models/main_test.go b/models/main_test.go
index 57e72a57fc..451b5e4b21 100644
--- a/models/main_test.go
+++ b/models/main_test.go
@@ -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)
}
diff --git a/models/repo_list.go b/models/repo_list.go
index a2dae85c84..2158cfe676 100644
--- a/models/repo_list.go
+++ b/models/repo_list.go
@@ -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
diff --git a/models/unit_tests.go b/models/unit_tests.go
index b4b36ba6b7..315627d8e0 100644
--- a/models/unit_tests.go
+++ b/models/unit_tests.go
@@ -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()