aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLanre Adelowo <adelowomailbox@gmail.com>2019-06-06 01:37:45 +0100
committerLunny Xiao <xiaolunwen@gmail.com>2019-06-06 08:37:45 +0800
commitde6539fc8c37485afcd6f30092a9ccbc3cae7de5 (patch)
tree02aec172ffe99ac123b56b301c70774f1f3f1292 /models
parent59e6a7b97f503c12ef2a6096ca7f127f81521b7a (diff)
downloadgitea-de6539fc8c37485afcd6f30092a9ccbc3cae7de5.tar.gz
gitea-de6539fc8c37485afcd6f30092a9ccbc3cae7de5.zip
Add state param to milestone listing API (#7131)
* Support state params * update tests * fix tests * add state=all support * update tests * update swagger * update swagger
Diffstat (limited to 'models')
-rw-r--r--models/fixtures/milestone.yml8
-rw-r--r--models/fixtures/repository.yml5
-rw-r--r--models/issue_milestone.go22
-rw-r--r--models/issue_milestone_test.go39
4 files changed, 61 insertions, 13 deletions
diff --git a/models/fixtures/milestone.yml b/models/fixtures/milestone.yml
index 8192c4fbe1..15f422fc3b 100644
--- a/models/fixtures/milestone.yml
+++ b/models/fixtures/milestone.yml
@@ -13,3 +13,11 @@
content: content2
is_closed: false
num_issues: 0
+
+-
+ id: 3
+ repo_id: 1
+ name: milestone3
+ content: content3
+ is_closed: true
+ num_issues: 0
diff --git a/models/fixtures/repository.yml b/models/fixtures/repository.yml
index 083182e2bc..609f421a90 100644
--- a/models/fixtures/repository.yml
+++ b/models/fixtures/repository.yml
@@ -8,7 +8,8 @@
num_closed_issues: 1
num_pulls: 2
num_closed_pulls: 0
- num_milestones: 2
+ num_milestones: 3
+ num_closed_milestones: 1
num_watches: 3
-
@@ -495,4 +496,4 @@
num_stars: 0
num_forks: 0
num_issues: 0
- is_mirror: false \ No newline at end of file
+ is_mirror: false
diff --git a/models/issue_milestone.go b/models/issue_milestone.go
index 3bde4b558c..f279dda195 100644
--- a/models/issue_milestone.go
+++ b/models/issue_milestone.go
@@ -190,10 +190,26 @@ func (milestones MilestoneList) getMilestoneIDs() []int64 {
}
// GetMilestonesByRepoID returns all opened milestones of a repository.
-func GetMilestonesByRepoID(repoID int64) (MilestoneList, error) {
+func GetMilestonesByRepoID(repoID int64, state api.StateType) (MilestoneList, error) {
+
+ sess := x.Where("repo_id = ?", repoID)
+
+ switch state {
+ case api.StateClosed:
+ sess = sess.And("is_closed = ?", true)
+
+ case api.StateAll:
+ break
+
+ case api.StateOpen:
+ fallthrough
+
+ default:
+ sess = sess.And("is_closed = ?", false)
+ }
+
miles := make([]*Milestone, 0, 10)
- return miles, x.Where("repo_id = ? AND is_closed = ?", repoID, false).
- Asc("deadline_unix").Asc("id").Find(&miles)
+ return miles, sess.Asc("deadline_unix").Asc("id").Find(&miles)
}
// GetMilestones returns a list of milestones of given repository and status.
diff --git a/models/issue_milestone_test.go b/models/issue_milestone_test.go
index fb1ee8b7a5..f9e51aff31 100644
--- a/models/issue_milestone_test.go
+++ b/models/issue_milestone_test.go
@@ -69,20 +69,43 @@ func TestGetMilestoneByRepoID(t *testing.T) {
func TestGetMilestonesByRepoID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
- test := func(repoID int64) {
+ test := func(repoID int64, state api.StateType) {
repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
- milestones, err := GetMilestonesByRepoID(repo.ID)
+ milestones, err := GetMilestonesByRepoID(repo.ID, state)
assert.NoError(t, err)
- assert.Len(t, milestones, repo.NumMilestones)
+
+ 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)
- test(2)
- test(3)
-
- milestones, err := GetMilestonesByRepoID(NonexistentID)
+ 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 := GetMilestonesByRepoID(NonexistentID, api.StateOpen)
assert.NoError(t, err)
assert.Len(t, milestones, 0)
}