diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-04-08 17:11:15 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-08 17:11:15 +0800 |
commit | 1dfa26e00e8e1ce6f6125335871da1d402a63466 (patch) | |
tree | d39414fb085ae52d6633ae9d064aabab2e7f75f8 /models/issues | |
parent | 84ceaa98bd731431c7d3a7f65e59e7ad076a540f (diff) | |
download | gitea-1dfa26e00e8e1ce6f6125335871da1d402a63466.tar.gz gitea-1dfa26e00e8e1ce6f6125335871da1d402a63466.zip |
Move milestone to models/issues/ (#19278)
* Move milestone to models/issues/
* Fix lint
* Fix test
* Fix lint
* Fix lint
Diffstat (limited to 'models/issues')
-rw-r--r-- | models/issues/main_test.go | 1 | ||||
-rw-r--r-- | models/issues/milestone.go | 664 | ||||
-rw-r--r-- | models/issues/milestone_test.go | 273 |
3 files changed, 938 insertions, 0 deletions
diff --git a/models/issues/main_test.go b/models/issues/main_test.go index 1c786d005f..bec6176abd 100644 --- a/models/issues/main_test.go +++ b/models/issues/main_test.go @@ -22,5 +22,6 @@ func TestMain(m *testing.M) { "reaction.yml", "user.yml", "repository.yml", + "milestone.yml", ) } diff --git a/models/issues/milestone.go b/models/issues/milestone.go new file mode 100644 index 0000000000..07c38754d4 --- /dev/null +++ b/models/issues/milestone.go @@ -0,0 +1,664 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package issues + +import ( + "context" + "fmt" + "strings" + "time" + + "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/modules/setting" + api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/timeutil" + + "xorm.io/builder" +) + +// ErrMilestoneNotExist represents a "MilestoneNotExist" kind of error. +type ErrMilestoneNotExist struct { + ID int64 + RepoID int64 + Name string +} + +// IsErrMilestoneNotExist checks if an error is a ErrMilestoneNotExist. +func IsErrMilestoneNotExist(err error) bool { + _, ok := err.(ErrMilestoneNotExist) + return ok +} + +func (err ErrMilestoneNotExist) Error() string { + if len(err.Name) > 0 { + return fmt.Sprintf("milestone does not exist [name: %s, repo_id: %d]", err.Name, err.RepoID) + } + return fmt.Sprintf("milestone does not exist [id: %d, repo_id: %d]", err.ID, err.RepoID) +} + +// Milestone represents a milestone of repository. +type Milestone struct { + ID int64 `xorm:"pk autoincr"` + RepoID int64 `xorm:"INDEX"` + Repo *repo_model.Repository `xorm:"-"` + Name string + Content string `xorm:"TEXT"` + RenderedContent string `xorm:"-"` + IsClosed bool + NumIssues int + NumClosedIssues int + NumOpenIssues int `xorm:"-"` + Completeness int // Percentage(1-100). + IsOverdue bool `xorm:"-"` + + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` + UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` + DeadlineUnix timeutil.TimeStamp + ClosedDateUnix timeutil.TimeStamp + DeadlineString string `xorm:"-"` + + TotalTrackedTime int64 `xorm:"-"` + TimeSinceUpdate int64 `xorm:"-"` +} + +func init() { + db.RegisterModel(new(Milestone)) +} + +// BeforeUpdate is invoked from XORM before updating this object. +func (m *Milestone) BeforeUpdate() { + if m.NumIssues > 0 { + m.Completeness = m.NumClosedIssues * 100 / m.NumIssues + } else { + m.Completeness = 0 + } +} + +// AfterLoad is invoked from XORM after setting the value of a field of +// this object. +func (m *Milestone) AfterLoad() { + if !m.UpdatedUnix.IsZero() { + m.TimeSinceUpdate = time.Now().Unix() - m.UpdatedUnix.AsTime().Unix() + } + m.NumOpenIssues = m.NumIssues - m.NumClosedIssues + if m.DeadlineUnix.Year() == 9999 { + return + } + + m.DeadlineString = m.DeadlineUnix.Format("2006-01-02") + if m.IsClosed { + m.IsOverdue = m.ClosedDateUnix >= m.DeadlineUnix + } else { + m.IsOverdue = timeutil.TimeStampNow() >= m.DeadlineUnix + } +} + +// State returns string representation of milestone status. +func (m *Milestone) State() api.StateType { + if m.IsClosed { + return api.StateClosed + } + return api.StateOpen +} + +// NewMilestone creates new milestone of repository. +func NewMilestone(m *Milestone) (err error) { + ctx, committer, err := db.TxContext() + if err != nil { + return err + } + defer committer.Close() + + m.Name = strings.TrimSpace(m.Name) + + if err = db.Insert(ctx, m); err != nil { + return err + } + + if _, err = db.Exec(ctx, "UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?", m.RepoID); err != nil { + return err + } + return committer.Commit() +} + +// GetMilestoneByRepoID returns the milestone in a repository. +func GetMilestoneByRepoID(ctx context.Context, repoID, id int64) (*Milestone, error) { + m := new(Milestone) + has, err := db.GetEngine(ctx).ID(id).Where("repo_id=?", repoID).Get(m) + if err != nil { + return nil, err + } else if !has { + return nil, ErrMilestoneNotExist{ID: id, RepoID: repoID} + } + return m, nil +} + +// GetMilestoneByRepoIDANDName return a milestone if one exist by name and repo +func GetMilestoneByRepoIDANDName(repoID int64, name string) (*Milestone, error) { + var mile Milestone + has, err := db.GetEngine(db.DefaultContext).Where("repo_id=? AND name=?", repoID, name).Get(&mile) + if err != nil { + return nil, err + } + if !has { + return nil, ErrMilestoneNotExist{Name: name, RepoID: repoID} + } + return &mile, nil +} + +// UpdateMilestone updates information of given milestone. +func UpdateMilestone(m *Milestone, oldIsClosed bool) error { + ctx, committer, err := db.TxContext() + if err != nil { + return err + } + defer committer.Close() + + if m.IsClosed && !oldIsClosed { + m.ClosedDateUnix = timeutil.TimeStampNow() + } + + if err := updateMilestone(ctx, m); err != nil { + return err + } + + // if IsClosed changed, update milestone numbers of repository + if oldIsClosed != m.IsClosed { + if err := updateRepoMilestoneNum(ctx, m.RepoID); err != nil { + return err + } + } + + return committer.Commit() +} + +func updateMilestone(ctx context.Context, m *Milestone) error { + m.Name = strings.TrimSpace(m.Name) + _, err := db.GetEngine(ctx).ID(m.ID).AllCols().Update(m) + if err != nil { + return err + } + return UpdateMilestoneCounters(ctx, m.ID) +} + +// UpdateMilestoneCounters calculates NumIssues, NumClosesIssues and Completeness +func UpdateMilestoneCounters(ctx context.Context, id int64) error { + e := db.GetEngine(ctx) + _, err := e.ID(id). + SetExpr("num_issues", builder.Select("count(*)").From("issue").Where( + builder.Eq{"milestone_id": id}, + )). + SetExpr("num_closed_issues", builder.Select("count(*)").From("issue").Where( + builder.Eq{ + "milestone_id": id, + "is_closed": true, + }, + )). + Update(&Milestone{}) + if err != nil { + return err + } + _, err = e.Exec("UPDATE `milestone` SET completeness=100*num_closed_issues/(CASE WHEN num_issues > 0 THEN num_issues ELSE 1 END) WHERE id=?", + id, + ) + return err +} + +// ChangeMilestoneStatusByRepoIDAndID changes a milestone open/closed status if the milestone ID is in the repo. +func ChangeMilestoneStatusByRepoIDAndID(repoID, milestoneID int64, isClosed bool) error { + ctx, committer, err := db.TxContext() + if err != nil { + return err + } + defer committer.Close() + + m := &Milestone{ + ID: milestoneID, + RepoID: repoID, + } + + has, err := db.GetEngine(ctx).ID(milestoneID).Where("repo_id = ?", repoID).Get(m) + if err != nil { + return err + } else if !has { + return ErrMilestoneNotExist{ID: milestoneID, RepoID: repoID} + } + + if err := changeMilestoneStatus(ctx, m, isClosed); err != nil { + return err + } + + return committer.Commit() +} + +// ChangeMilestoneStatus changes the milestone open/closed status. +func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) { + ctx, committer, err := db.TxContext() + if err != nil { + return err + } + defer committer.Close() + + if err := changeMilestoneStatus(ctx, m, isClosed); err != nil { + return err + } + + return committer.Commit() +} + +func changeMilestoneStatus(ctx context.Context, m *Milestone, isClosed bool) error { + m.IsClosed = isClosed + if isClosed { + m.ClosedDateUnix = timeutil.TimeStampNow() + } + + count, err := db.GetEngine(ctx).ID(m.ID).Where("repo_id = ? AND is_closed = ?", m.RepoID, !isClosed).Cols("is_closed", "closed_date_unix").Update(m) + if err != nil { + return err + } + if count < 1 { + return nil + } + return updateRepoMilestoneNum(ctx, m.RepoID) +} + +// DeleteMilestoneByRepoID deletes a milestone from a repository. +func DeleteMilestoneByRepoID(repoID, id int64) error { + m, err := GetMilestoneByRepoID(db.DefaultContext, repoID, id) + if err != nil { + if IsErrMilestoneNotExist(err) { + return nil + } + return err + } + + repo, err := repo_model.GetRepositoryByID(m.RepoID) + if err != nil { + return err + } + + ctx, committer, err := db.TxContext() + if err != nil { + return err + } + defer committer.Close() + + sess := db.GetEngine(ctx) + + if _, err = sess.ID(m.ID).Delete(new(Milestone)); err != nil { + return err + } + + numMilestones, err := countRepoMilestones(sess, repo.ID) + if err != nil { + return err + } + numClosedMilestones, err := countRepoClosedMilestones(sess, repo.ID) + if err != nil { + return err + } + repo.NumMilestones = int(numMilestones) + repo.NumClosedMilestones = int(numClosedMilestones) + + if _, err = sess.ID(repo.ID).Cols("num_milestones, num_closed_milestones").Update(repo); err != nil { + return err + } + + if _, err = db.Exec(ctx, "UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil { + return err + } + return committer.Commit() +} + +// MilestoneList is a list of milestones offering additional functionality +type MilestoneList []*Milestone + +func (milestones MilestoneList) getMilestoneIDs() []int64 { + ids := make([]int64, 0, len(milestones)) + for _, ms := range milestones { + ids = append(ids, ms.ID) + } + return ids +} + +// GetMilestonesOption contain options to get milestones +type GetMilestonesOption struct { + db.ListOptions + RepoID int64 + State api.StateType + Name string + SortType string +} + +func (opts GetMilestonesOption) toCond() builder.Cond { + cond := builder.NewCond() + if opts.RepoID != 0 { + cond = cond.And(builder.Eq{"repo_id": opts.RepoID}) + } + + switch opts.State { + case api.StateClosed: + cond = cond.And(builder.Eq{"is_closed": true}) + case api.StateAll: + break + // api.StateOpen: + default: + cond = cond.And(builder.Eq{"is_closed": false}) + } + + if len(opts.Name) != 0 { + cond = cond.And(builder.Like{"name", opts.Name}) + } + + return cond +} + +// GetMilestones returns milestones filtered by GetMilestonesOption's +func GetMilestones(opts GetMilestonesOption) (MilestoneList, int64, error) { + sess := db.GetEngine(db.DefaultContext).Where(opts.toCond()) + + if opts.Page != 0 { + sess = db.SetSessionPagination(sess, &opts) + } + + switch opts.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") + case "id": + sess.Asc("id") + default: + sess.Asc("deadline_unix").Asc("id") + } + + miles := make([]*Milestone, 0, opts.PageSize) + total, err := sess.FindAndCount(&miles) + return miles, total, err +} + +// SearchMilestones search milestones +func SearchMilestones(repoCond builder.Cond, page int, isClosed bool, sortType, keyword string) (MilestoneList, error) { + miles := make([]*Milestone, 0, setting.UI.IssuePagingNum) + sess := db.GetEngine(db.DefaultContext).Where("is_closed = ?", isClosed) + if len(keyword) > 0 { + sess = sess.And(builder.Like{"UPPER(name)", strings.ToUpper(keyword)}) + } + if repoCond.IsValid() { + sess.In("repo_id", builder.Select("id").From("repository").Where(repoCond)) + } + 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) +} + +// GetMilestonesByRepoIDs returns a list of milestones of given repositories and status. +func GetMilestonesByRepoIDs(repoIDs []int64, page int, isClosed bool, sortType string) (MilestoneList, error) { + return SearchMilestones( + builder.In("repo_id", repoIDs), + page, + isClosed, + sortType, + "", + ) +} + +// ____ _ _ +// / ___|| |_ __ _| |_ ___ +// \___ \| __/ _` | __/ __| +// ___) | || (_| | |_\__ \ +// |____/ \__\__,_|\__|___/ +// + +// MilestonesStats represents milestone statistic information. +type MilestonesStats struct { + OpenCount, ClosedCount int64 +} + +// Total returns the total counts of milestones +func (m MilestonesStats) Total() int64 { + return m.OpenCount + m.ClosedCount +} + +// GetMilestonesStatsByRepoCond returns milestone statistic information for dashboard by given conditions. +func GetMilestonesStatsByRepoCond(repoCond builder.Cond) (*MilestonesStats, error) { + var err error + stats := &MilestonesStats{} + + sess := db.GetEngine(db.DefaultContext).Where("is_closed = ?", false) + if repoCond.IsValid() { + sess.And(builder.In("repo_id", builder.Select("id").From("repository").Where(repoCond))) + } + stats.OpenCount, err = sess.Count(new(Milestone)) + if err != nil { + return nil, err + } + + sess = db.GetEngine(db.DefaultContext).Where("is_closed = ?", true) + if repoCond.IsValid() { + sess.And(builder.In("repo_id", builder.Select("id").From("repository").Where(repoCond))) + } + stats.ClosedCount, err = sess.Count(new(Milestone)) + if err != nil { + return nil, err + } + + return stats, nil +} + +// GetMilestonesStatsByRepoCondAndKw returns milestone statistic information for dashboard by given repo conditions and name keyword. +func GetMilestonesStatsByRepoCondAndKw(repoCond builder.Cond, keyword string) (*MilestonesStats, error) { + var err error + stats := &MilestonesStats{} + + sess := db.GetEngine(db.DefaultContext).Where("is_closed = ?", false) + if len(keyword) > 0 { + sess = sess.And(builder.Like{"UPPER(name)", strings.ToUpper(keyword)}) + } + if repoCond.IsValid() { + sess.And(builder.In("repo_id", builder.Select("id").From("repository").Where(repoCond))) + } + stats.OpenCount, err = sess.Count(new(Milestone)) + if err != nil { + return nil, err + } + + sess = db.GetEngine(db.DefaultContext).Where("is_closed = ?", true) + if len(keyword) > 0 { + sess = sess.And(builder.Like{"UPPER(name)", strings.ToUpper(keyword)}) + } + if repoCond.IsValid() { + sess.And(builder.In("repo_id", builder.Select("id").From("repository").Where(repoCond))) + } + stats.ClosedCount, err = sess.Count(new(Milestone)) + if err != nil { + return nil, err + } + + return stats, nil +} + +func countRepoMilestones(e db.Engine, repoID int64) (int64, error) { + return e. + Where("repo_id=?", repoID). + Count(new(Milestone)) +} + +func countRepoClosedMilestones(e db.Engine, repoID int64) (int64, error) { + return e. + Where("repo_id=? AND is_closed=?", repoID, true). + Count(new(Milestone)) +} + +// CountRepoClosedMilestones returns number of closed milestones in given repository. +func CountRepoClosedMilestones(repoID int64) (int64, error) { + return countRepoClosedMilestones(db.GetEngine(db.DefaultContext), repoID) +} + +// CountMilestonesByRepoCond map from repo conditions to number of milestones matching the options` +func CountMilestonesByRepoCond(repoCond builder.Cond, isClosed bool) (map[int64]int64, error) { + sess := db.GetEngine(db.DefaultContext).Where("is_closed = ?", isClosed) + if repoCond.IsValid() { + sess.In("repo_id", builder.Select("id").From("repository").Where(repoCond)) + } + + 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 +} + +// CountMilestonesByRepoCondAndKw map from repo conditions and the keyword of milestones' name to number of milestones matching the options` +func CountMilestonesByRepoCondAndKw(repoCond builder.Cond, keyword string, isClosed bool) (map[int64]int64, error) { + sess := db.GetEngine(db.DefaultContext).Where("is_closed = ?", isClosed) + if len(keyword) > 0 { + sess = sess.And(builder.Like{"UPPER(name)", strings.ToUpper(keyword)}) + } + if repoCond.IsValid() { + sess.In("repo_id", builder.Select("id").From("repository").Where(repoCond)) + } + + 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 +} + +func updateRepoMilestoneNum(ctx context.Context, repoID int64) error { + _, err := db.GetEngine(ctx).Exec("UPDATE `repository` SET num_milestones=(SELECT count(*) FROM milestone WHERE repo_id=?),num_closed_milestones=(SELECT count(*) FROM milestone WHERE repo_id=? AND is_closed=?) WHERE id=?", + repoID, + repoID, + true, + repoID, + ) + return err +} + +// _____ _ _ _____ _ +// |_ _| __ __ _ ___| | _____ __| |_ _(_)_ __ ___ ___ ___ +// | || '__/ _` |/ __| |/ / _ \/ _` | | | | | '_ ` _ \ / _ \/ __| +// | || | | (_| | (__| < __/ (_| | | | | | | | | | | __/\__ \ +// |_||_| \__,_|\___|_|\_\___|\__,_| |_| |_|_| |_| |_|\___||___/ +// + +func (milestones MilestoneList) loadTotalTrackedTimes(e db.Engine) error { + type totalTimesByMilestone struct { + MilestoneID int64 + Time int64 + } + if len(milestones) == 0 { + return nil + } + trackedTimes := make(map[int64]int64, len(milestones)) + + // Get total tracked time by milestone_id + rows, err := e.Table("issue"). + Join("INNER", "milestone", "issue.milestone_id = milestone.id"). + Join("LEFT", "tracked_time", "tracked_time.issue_id = issue.id"). + Where("tracked_time.deleted = ?", false). + Select("milestone_id, sum(time) as time"). + In("milestone_id", milestones.getMilestoneIDs()). + GroupBy("milestone_id"). + Rows(new(totalTimesByMilestone)) + if err != nil { + return err + } + + defer rows.Close() + + for rows.Next() { + var totalTime totalTimesByMilestone + err = rows.Scan(&totalTime) + if err != nil { + return err + } + trackedTimes[totalTime.MilestoneID] = totalTime.Time + } + + for _, milestone := range milestones { + milestone.TotalTrackedTime = trackedTimes[milestone.ID] + } + return nil +} + +func (m *Milestone) loadTotalTrackedTime(e db.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"). + Where("tracked_time.deleted = ?", false). + 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(db.GetEngine(db.DefaultContext)) +} + +// LoadTotalTrackedTime loads the tracked time for the milestone +func (m *Milestone) LoadTotalTrackedTime() error { + return m.loadTotalTrackedTime(db.GetEngine(db.DefaultContext)) +} diff --git a/models/issues/milestone_test.go b/models/issues/milestone_test.go new file mode 100644 index 0000000000..09f51de45c --- /dev/null +++ b/models/issues/milestone_test.go @@ -0,0 +1,273 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package issues + +import ( + "sort" + "testing" + + "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/models/unittest" + "code.gitea.io/gitea/modules/setting" + api "code.gitea.io/gitea/modules/structs" + + "github.com/stretchr/testify/assert" + "xorm.io/builder" +) + +func TestMilestone_State(t *testing.T) { + assert.Equal(t, api.StateOpen, (&Milestone{IsClosed: false}).State()) + assert.Equal(t, api.StateClosed, (&Milestone{IsClosed: true}).State()) +} + +func TestGetMilestoneByRepoID(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + milestone, err := GetMilestoneByRepoID(db.DefaultContext, 1, 1) + assert.NoError(t, err) + assert.EqualValues(t, 1, milestone.ID) + assert.EqualValues(t, 1, milestone.RepoID) + + _, err = GetMilestoneByRepoID(db.DefaultContext, unittest.NonexistentID, unittest.NonexistentID) + assert.True(t, IsErrMilestoneNotExist(err)) +} + +func TestGetMilestonesByRepoID(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + test := func(repoID int64, state api.StateType) { + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID}).(*repo_model.Repository) + milestones, _, err := GetMilestones(GetMilestonesOption{ + RepoID: repo.ID, + State: state, + }) + assert.NoError(t, err) + + var n int + + switch state { + case api.StateClosed: + n = repo.NumClosedMilestones + + case api.StateAll: + n = repo.NumMilestones + + case api.StateOpen: + fallthrough + + default: + n = repo.NumOpenMilestones + } + + assert.Len(t, milestones, n) + for _, milestone := range milestones { + assert.EqualValues(t, repoID, milestone.RepoID) + } + } + test(1, api.StateOpen) + test(1, api.StateAll) + test(1, api.StateClosed) + test(2, api.StateOpen) + test(2, api.StateAll) + test(2, api.StateClosed) + test(3, api.StateOpen) + test(3, api.StateClosed) + test(3, api.StateAll) + + milestones, _, err := GetMilestones(GetMilestonesOption{ + RepoID: unittest.NonexistentID, + State: api.StateOpen, + }) + assert.NoError(t, err) + assert.Len(t, milestones, 0) +} + +func TestGetMilestones(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) + test := func(sortType string, sortCond func(*Milestone) int) { + for _, page := range []int{0, 1} { + milestones, _, err := GetMilestones(GetMilestonesOption{ + ListOptions: db.ListOptions{ + Page: page, + PageSize: setting.UI.IssuePagingNum, + }, + RepoID: repo.ID, + State: api.StateOpen, + SortType: sortType, + }) + assert.NoError(t, err) + assert.Len(t, milestones, repo.NumMilestones-repo.NumClosedMilestones) + values := make([]int, len(milestones)) + for i, milestone := range milestones { + values[i] = sortCond(milestone) + } + assert.True(t, sort.IntsAreSorted(values)) + + milestones, _, err = GetMilestones(GetMilestonesOption{ + ListOptions: db.ListOptions{ + Page: page, + PageSize: setting.UI.IssuePagingNum, + }, + RepoID: repo.ID, + State: api.StateClosed, + Name: "", + SortType: sortType, + }) + assert.NoError(t, err) + assert.Len(t, milestones, repo.NumClosedMilestones) + values = make([]int, len(milestones)) + for i, milestone := range milestones { + 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 TestCountRepoMilestones(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + test := func(repoID int64) { + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID}).(*repo_model.Repository) + count, err := countRepoMilestones(db.GetEngine(db.DefaultContext), repoID) + assert.NoError(t, err) + assert.EqualValues(t, repo.NumMilestones, count) + } + test(1) + test(2) + test(3) + + count, err := countRepoMilestones(db.GetEngine(db.DefaultContext), unittest.NonexistentID) + assert.NoError(t, err) + assert.EqualValues(t, 0, count) +} + +func TestCountRepoClosedMilestones(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + test := func(repoID int64) { + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID}).(*repo_model.Repository) + count, err := CountRepoClosedMilestones(repoID) + assert.NoError(t, err) + assert.EqualValues(t, repo.NumClosedMilestones, count) + } + test(1) + test(2) + test(3) + + count, err := CountRepoClosedMilestones(unittest.NonexistentID) + assert.NoError(t, err) + assert.EqualValues(t, 0, count) +} + +func TestCountMilestonesByRepoIDs(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + milestonesCount := func(repoID int64) (int, int) { + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID}).(*repo_model.Repository) + return repo.NumOpenMilestones, repo.NumClosedMilestones + } + repo1OpenCount, repo1ClosedCount := milestonesCount(1) + repo2OpenCount, repo2ClosedCount := milestonesCount(2) + + openCounts, err := CountMilestonesByRepoCond(builder.In("repo_id", []int64{1, 2}), false) + assert.NoError(t, err) + assert.EqualValues(t, repo1OpenCount, openCounts[1]) + assert.EqualValues(t, repo2OpenCount, openCounts[2]) + + closedCounts, err := CountMilestonesByRepoCond(builder.In("repo_id", []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, unittest.PrepareTestDatabase()) + repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.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 TestGetMilestonesStats(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + test := func(repoID int64) { + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID}).(*repo_model.Repository) + stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": repoID})) + assert.NoError(t, err) + assert.EqualValues(t, repo.NumMilestones-repo.NumClosedMilestones, stats.OpenCount) + assert.EqualValues(t, repo.NumClosedMilestones, stats.ClosedCount) + } + test(1) + test(2) + test(3) + + stats, err := GetMilestonesStatsByRepoCond(builder.And(builder.Eq{"repo_id": unittest.NonexistentID})) + assert.NoError(t, err) + assert.EqualValues(t, 0, stats.OpenCount) + assert.EqualValues(t, 0, stats.ClosedCount) + + repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}).(*repo_model.Repository) + repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}).(*repo_model.Repository) + + milestoneStats, err := GetMilestonesStatsByRepoCond(builder.In("repo_id", []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) +} |