aboutsummaryrefslogtreecommitdiffstats
path: root/models/issue_milestone.go
diff options
context:
space:
mode:
authorBrad Albright <32200834+bhalbright@users.noreply.github.com>2019-12-15 08:20:08 -0600
committerzeripath <art27@cantab.net>2019-12-15 14:20:08 +0000
commitf6b29012e09d5f7770a0b1ea8659da5172e155b3 (patch)
treecf78ded22aee572c91e93e2a2a3db8a7f9fd903c /models/issue_milestone.go
parent7cc16740a56072465b3938cbb9cd326d40bd7ba9 (diff)
downloadgitea-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/issue_milestone.go')
-rw-r--r--models/issue_milestone.go107
1 files changed, 105 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
+}