diff options
author | Brad Albright <32200834+bhalbright@users.noreply.github.com> | 2019-12-15 08:20:08 -0600 |
---|---|---|
committer | zeripath <art27@cantab.net> | 2019-12-15 14:20:08 +0000 |
commit | f6b29012e09d5f7770a0b1ea8659da5172e155b3 (patch) | |
tree | cf78ded22aee572c91e93e2a2a3db8a7f9fd903c /models | |
parent | 7cc16740a56072465b3938cbb9cd326d40bd7ba9 (diff) | |
download | gitea-f6b29012e09d5f7770a0b1ea8659da5172e155b3.tar.gz gitea-f6b29012e09d5f7770a0b1ea8659da5172e155b3.zip |
Add /milestones endpoint (#8733)
Create a /milestones endpoint which basically serves as a dashboard view for milestones, very similar to the /issues or /pulls page.
Closes #8232
Diffstat (limited to 'models')
-rw-r--r-- | models/issue_milestone.go | 107 | ||||
-rw-r--r-- | models/issue_milestone_test.go | 85 |
2 files changed, 190 insertions, 2 deletions
diff --git a/models/issue_milestone.go b/models/issue_milestone.go index 0b854a8671..5fd1e1b8cc 100644 --- a/models/issue_milestone.go +++ b/models/issue_milestone.go @@ -17,8 +17,9 @@ import ( // Milestone represents a milestone of repository. type Milestone struct { - ID int64 `xorm:"pk autoincr"` - RepoID int64 `xorm:"INDEX"` + ID int64 `xorm:"pk autoincr"` + RepoID int64 `xorm:"INDEX"` + Repo *Repository `xorm:"-"` Name string Content string `xorm:"TEXT"` RenderedContent string `xorm:"-"` @@ -177,11 +178,38 @@ func (milestones MilestoneList) loadTotalTrackedTimes(e Engine) error { return nil } +func (m *Milestone) loadTotalTrackedTime(e Engine) error { + type totalTimesByMilestone struct { + MilestoneID int64 + Time int64 + } + totalTime := &totalTimesByMilestone{MilestoneID: m.ID} + has, err := e.Table("issue"). + Join("INNER", "milestone", "issue.milestone_id = milestone.id"). + Join("LEFT", "tracked_time", "tracked_time.issue_id = issue.id"). + Select("milestone_id, sum(time) as time"). + Where("milestone_id = ?", m.ID). + GroupBy("milestone_id"). + Get(totalTime) + if err != nil { + return err + } else if !has { + return nil + } + m.TotalTrackedTime = totalTime.Time + return nil +} + // LoadTotalTrackedTimes loads for every milestone in the list the TotalTrackedTime by a batch request func (milestones MilestoneList) LoadTotalTrackedTimes() error { return milestones.loadTotalTrackedTimes(x) } +// LoadTotalTrackedTime loads the tracked time for the milestone +func (m *Milestone) LoadTotalTrackedTime() error { + return m.loadTotalTrackedTime(x) +} + func (milestones MilestoneList) getMilestoneIDs() []int64 { var ids = make([]int64, 0, len(milestones)) for _, ms := range milestones { @@ -465,3 +493,78 @@ func DeleteMilestoneByRepoID(repoID, id int64) error { } return sess.Commit() } + +// CountMilestonesByRepoIDs map from repoIDs to number of milestones matching the options` +func CountMilestonesByRepoIDs(repoIDs []int64, isClosed bool) (map[int64]int64, error) { + sess := x.Where("is_closed = ?", isClosed) + sess.In("repo_id", repoIDs) + + countsSlice := make([]*struct { + RepoID int64 + Count int64 + }, 0, 10) + if err := sess.GroupBy("repo_id"). + Select("repo_id AS repo_id, COUNT(*) AS count"). + Table("milestone"). + 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 +} + +// GetMilestonesByRepoIDs returns a list of milestones of given repositories and status. +func GetMilestonesByRepoIDs(repoIDs []int64, page int, isClosed bool, sortType string) (MilestoneList, error) { + miles := make([]*Milestone, 0, setting.UI.IssuePagingNum) + sess := x.Where("is_closed = ?", isClosed) + sess.In("repo_id", repoIDs) + if page > 0 { + sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum) + } + + switch sortType { + case "furthestduedate": + sess.Desc("deadline_unix") + case "leastcomplete": + sess.Asc("completeness") + case "mostcomplete": + sess.Desc("completeness") + case "leastissues": + sess.Asc("num_issues") + case "mostissues": + sess.Desc("num_issues") + default: + sess.Asc("deadline_unix") + } + return miles, sess.Find(&miles) +} + +// MilestonesStats represents milestone statistic information. +type MilestonesStats struct { + OpenCount, ClosedCount int64 +} + +// GetMilestonesStats returns milestone statistic information for dashboard by given conditions. +func GetMilestonesStats(userRepoIDs []int64) (*MilestonesStats, error) { + var err error + stats := &MilestonesStats{} + + stats.OpenCount, err = x.Where("is_closed = ?", false). + And(builder.In("repo_id", userRepoIDs)). + Count(new(Milestone)) + if err != nil { + return nil, err + } + stats.ClosedCount, err = x.Where("is_closed = ?", true). + And(builder.In("repo_id", userRepoIDs)). + Count(new(Milestone)) + if err != nil { + return nil, err + } + + return stats, nil +} diff --git a/models/issue_milestone_test.go b/models/issue_milestone_test.go index 6f8548ec67..787b849cce 100644 --- a/models/issue_milestone_test.go +++ b/models/issue_milestone_test.go @@ -289,3 +289,88 @@ func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) { assert.Equal(t, miles[0].TotalTrackedTime, int64(3662)) } + +func TestCountMilestonesByRepoIDs(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + milestonesCount := func(repoID int64) (int, int) { + repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) + return repo.NumOpenMilestones, repo.NumClosedMilestones + } + repo1OpenCount, repo1ClosedCount := milestonesCount(1) + repo2OpenCount, repo2ClosedCount := milestonesCount(2) + + openCounts, err := CountMilestonesByRepoIDs([]int64{1, 2}, false) + assert.NoError(t, err) + assert.EqualValues(t, repo1OpenCount, openCounts[1]) + assert.EqualValues(t, repo2OpenCount, openCounts[2]) + + closedCounts, err := CountMilestonesByRepoIDs([]int64{1, 2}, true) + assert.NoError(t, err) + assert.EqualValues(t, repo1ClosedCount, closedCounts[1]) + assert.EqualValues(t, repo2ClosedCount, closedCounts[2]) +} + +func TestGetMilestonesByRepoIDs(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + test := func(sortType string, sortCond func(*Milestone) int) { + for _, page := range []int{0, 1} { + openMilestones, err := GetMilestonesByRepoIDs([]int64{repo1.ID, repo2.ID}, page, false, sortType) + assert.NoError(t, err) + assert.Len(t, openMilestones, repo1.NumOpenMilestones+repo2.NumOpenMilestones) + values := make([]int, len(openMilestones)) + for i, milestone := range openMilestones { + values[i] = sortCond(milestone) + } + assert.True(t, sort.IntsAreSorted(values)) + + closedMilestones, err := GetMilestonesByRepoIDs([]int64{repo1.ID, repo2.ID}, page, true, sortType) + assert.NoError(t, err) + assert.Len(t, closedMilestones, repo1.NumClosedMilestones+repo2.NumClosedMilestones) + values = make([]int, len(closedMilestones)) + for i, milestone := range closedMilestones { + values[i] = sortCond(milestone) + } + assert.True(t, sort.IntsAreSorted(values)) + } + } + test("furthestduedate", func(milestone *Milestone) int { + return -int(milestone.DeadlineUnix) + }) + test("leastcomplete", func(milestone *Milestone) int { + return milestone.Completeness + }) + test("mostcomplete", func(milestone *Milestone) int { + return -milestone.Completeness + }) + test("leastissues", func(milestone *Milestone) int { + return milestone.NumIssues + }) + test("mostissues", func(milestone *Milestone) int { + return -milestone.NumIssues + }) + test("soonestduedate", func(milestone *Milestone) int { + return int(milestone.DeadlineUnix) + }) +} + +func TestLoadTotalTrackedTime(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone) + + assert.NoError(t, milestone.LoadTotalTrackedTime()) + + assert.Equal(t, milestone.TotalTrackedTime, int64(3662)) +} + +func TestGetMilestonesStats(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository) + + milestoneStats, err := GetMilestonesStats([]int64{repo1.ID, repo2.ID}) + assert.NoError(t, err) + assert.EqualValues(t, repo1.NumOpenMilestones+repo2.NumOpenMilestones, milestoneStats.OpenCount) + assert.EqualValues(t, repo1.NumClosedMilestones+repo2.NumClosedMilestones, milestoneStats.ClosedCount) +} |