diff options
author | 6543 <6543@obermui.de> | 2020-07-28 13:30:40 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-28 12:30:40 +0100 |
commit | 8bdc9795d8a5aa51bc1295dd1cf57006a5873343 (patch) | |
tree | 81ebd17da66318ca7909284021319be10a552e5c /models | |
parent | 78cbd0ca72619a25849edc3460ec0c492b838a59 (diff) | |
download | gitea-8bdc9795d8a5aa51bc1295dd1cf57006a5873343.tar.gz gitea-8bdc9795d8a5aa51bc1295dd1cf57006a5873343.zip |
Add name filter to API for GetMilestoneList (#12336)
Adds a name filter to the API for GetMilestoneList
Includes a small refactor: merge GetMilestones and GetMilestonesByRepoID
Close #12260
Needed for https://gitea.com/gitea/go-sdk/issues/383 and https://gitea.com/gitea/tea/pulls/149
Diffstat (limited to 'models')
-rw-r--r-- | models/issue_milestone.go | 47 | ||||
-rw-r--r-- | models/issue_milestone_test.go | 32 |
2 files changed, 52 insertions, 27 deletions
diff --git a/models/issue_milestone.go b/models/issue_milestone.go index 4648274459..824b939a56 100644 --- a/models/issue_milestone.go +++ b/models/issue_milestone.go @@ -330,41 +330,38 @@ func (milestones MilestoneList) getMilestoneIDs() []int64 { return ids } -// GetMilestonesByRepoID returns all opened milestones of a repository. -func GetMilestonesByRepoID(repoID int64, state api.StateType, listOptions ListOptions) (MilestoneList, error) { - sess := x.Where("repo_id = ?", repoID) +// GetMilestonesOption contain options to get milestones +type GetMilestonesOption struct { + ListOptions + RepoID int64 + State api.StateType + Name string + SortType string +} + +// GetMilestones returns milestones filtered by GetMilestonesOption's +func GetMilestones(opts GetMilestonesOption) (MilestoneList, error) { + sess := x.Where("repo_id = ?", opts.RepoID) - switch state { + switch opts.State { case api.StateClosed: sess = sess.And("is_closed = ?", true) - case api.StateAll: break - - case api.StateOpen: - fallthrough - + // api.StateOpen: default: sess = sess.And("is_closed = ?", false) } - if listOptions.Page != 0 { - sess = listOptions.setSessionPagination(sess) + if len(opts.Name) != 0 { + sess = sess.And(builder.Like{"name", opts.Name}) } - miles := make([]*Milestone, 0, listOptions.PageSize) - return miles, sess.Asc("deadline_unix").Asc("id").Find(&miles) -} - -// GetMilestones returns a list of milestones of given repository and status. -func GetMilestones(repoID int64, page int, isClosed bool, sortType string) (MilestoneList, error) { - miles := make([]*Milestone, 0, setting.UI.IssuePagingNum) - sess := x.Where("repo_id = ? AND is_closed = ?", repoID, isClosed) - if page > 0 { - sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum) + if opts.Page != 0 { + sess = opts.setSessionPagination(sess) } - switch sortType { + switch opts.SortType { case "furthestduedate": sess.Desc("deadline_unix") case "leastcomplete": @@ -375,9 +372,13 @@ func GetMilestones(repoID int64, page int, isClosed bool, sortType string) (Mile sess.Asc("num_issues") case "mostissues": sess.Desc("num_issues") + case "id": + sess.Asc("id") default: - sess.Asc("deadline_unix") + sess.Asc("deadline_unix").Asc("id") } + + miles := make([]*Milestone, 0, opts.PageSize) return miles, sess.Find(&miles) } diff --git a/models/issue_milestone_test.go b/models/issue_milestone_test.go index 07dd8d57c5..af264aa274 100644 --- a/models/issue_milestone_test.go +++ b/models/issue_milestone_test.go @@ -8,6 +8,7 @@ import ( "sort" "testing" + "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" @@ -49,7 +50,10 @@ func TestGetMilestonesByRepoID(t *testing.T) { assert.NoError(t, PrepareTestDatabase()) test := func(repoID int64, state api.StateType) { repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) - milestones, err := GetMilestonesByRepoID(repo.ID, state, ListOptions{}) + milestones, err := GetMilestones(GetMilestonesOption{ + RepoID: repo.ID, + State: state, + }) assert.NoError(t, err) var n int @@ -83,7 +87,10 @@ func TestGetMilestonesByRepoID(t *testing.T) { test(3, api.StateClosed) test(3, api.StateAll) - milestones, err := GetMilestonesByRepoID(NonexistentID, api.StateOpen, ListOptions{}) + milestones, err := GetMilestones(GetMilestonesOption{ + RepoID: NonexistentID, + State: api.StateOpen, + }) assert.NoError(t, err) assert.Len(t, milestones, 0) } @@ -93,7 +100,15 @@ func TestGetMilestones(t *testing.T) { repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) test := func(sortType string, sortCond func(*Milestone) int) { for _, page := range []int{0, 1} { - milestones, err := GetMilestones(repo.ID, page, false, sortType) + milestones, err := GetMilestones(GetMilestonesOption{ + ListOptions: 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)) @@ -102,7 +117,16 @@ func TestGetMilestones(t *testing.T) { } assert.True(t, sort.IntsAreSorted(values)) - milestones, err = GetMilestones(repo.ID, page, true, sortType) + milestones, err = GetMilestones(GetMilestonesOption{ + ListOptions: 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)) |