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
This commit is contained in:
Lanre Adelowo 2019-06-06 01:37:45 +01:00 committed by Lunny Xiao
parent 59e6a7b97f
commit de6539fc8c
8 changed files with 76 additions and 15 deletions

View file

@ -13,3 +13,11 @@
content: content2 content: content2
is_closed: false is_closed: false
num_issues: 0 num_issues: 0
-
id: 3
repo_id: 1
name: milestone3
content: content3
is_closed: true
num_issues: 0

View file

@ -8,7 +8,8 @@
num_closed_issues: 1 num_closed_issues: 1
num_pulls: 2 num_pulls: 2
num_closed_pulls: 0 num_closed_pulls: 0
num_milestones: 2 num_milestones: 3
num_closed_milestones: 1
num_watches: 3 num_watches: 3
- -

View file

@ -190,10 +190,26 @@ func (milestones MilestoneList) getMilestoneIDs() []int64 {
} }
// GetMilestonesByRepoID returns all opened milestones of a repository. // 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) miles := make([]*Milestone, 0, 10)
return miles, x.Where("repo_id = ? AND is_closed = ?", repoID, false). return miles, sess.Asc("deadline_unix").Asc("id").Find(&miles)
Asc("deadline_unix").Asc("id").Find(&miles)
} }
// GetMilestones returns a list of milestones of given repository and status. // GetMilestones returns a list of milestones of given repository and status.

View file

@ -69,20 +69,43 @@ func TestGetMilestoneByRepoID(t *testing.T) {
func TestGetMilestonesByRepoID(t *testing.T) { func TestGetMilestonesByRepoID(t *testing.T) {
assert.NoError(t, PrepareTestDatabase()) assert.NoError(t, PrepareTestDatabase())
test := func(repoID int64) { test := func(repoID int64, state api.StateType) {
repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
milestones, err := GetMilestonesByRepoID(repo.ID) milestones, err := GetMilestonesByRepoID(repo.ID, state)
assert.NoError(t, err) 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 { for _, milestone := range milestones {
assert.EqualValues(t, repoID, milestone.RepoID) assert.EqualValues(t, repoID, milestone.RepoID)
} }
} }
test(1) test(1, api.StateOpen)
test(2) test(1, api.StateAll)
test(3) 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) milestones, err := GetMilestonesByRepoID(NonexistentID, api.StateOpen)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, milestones, 0) assert.Len(t, milestones, 0)
} }

View file

@ -16,6 +16,8 @@ const (
StateOpen StateType = "open" StateOpen StateType = "open"
// StateClosed pr is closed // StateClosed pr is closed
StateClosed StateType = "closed" StateClosed StateType = "closed"
// StateAll is all
StateAll StateType = "all"
) )
// PullRequestMeta PR info if an issue is a PR // PullRequestMeta PR info if an issue is a PR

View file

@ -14,7 +14,7 @@ import (
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
) )
// ListMilestones list all the opened milestones for a repository // ListMilestones list milestones for a repository
func ListMilestones(ctx *context.APIContext) { func ListMilestones(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/milestones issue issueGetMilestonesList // swagger:operation GET /repos/{owner}/{repo}/milestones issue issueGetMilestonesList
// --- // ---
@ -32,10 +32,14 @@ func ListMilestones(ctx *context.APIContext) {
// description: name of the repo // description: name of the repo
// type: string // type: string
// required: true // required: true
// - name: state
// in: query
// description: Milestone state, Recognised values are open, closed and all. Defaults to "open"
// type: string
// responses: // responses:
// "200": // "200":
// "$ref": "#/responses/MilestoneList" // "$ref": "#/responses/MilestoneList"
milestones, err := models.GetMilestonesByRepoID(ctx.Repo.Repository.ID) milestones, err := models.GetMilestonesByRepoID(ctx.Repo.Repository.ID, api.StateType(ctx.Query("state")))
if err != nil { if err != nil {
ctx.Error(500, "GetMilestonesByRepoID", err) ctx.Error(500, "GetMilestonesByRepoID", err)
return return

View file

@ -24,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"github.com/Unknwon/com" "github.com/Unknwon/com"
@ -305,7 +306,7 @@ func Issues(ctx *context.Context) {
var err error var err error
// Get milestones. // Get milestones.
ctx.Data["Milestones"], err = models.GetMilestonesByRepoID(ctx.Repo.Repository.ID) ctx.Data["Milestones"], err = models.GetMilestonesByRepoID(ctx.Repo.Repository.ID, api.StateType(ctx.Query("state")))
if err != nil { if err != nil {
ctx.ServerError("GetAllRepoMilestones", err) ctx.ServerError("GetAllRepoMilestones", err)
return return

View file

@ -3892,6 +3892,12 @@
"name": "repo", "name": "repo",
"in": "path", "in": "path",
"required": true "required": true
},
{
"type": "string",
"description": "Milestone state, Recognised values are open, closed and all. Defaults to \"open\"",
"name": "state",
"in": "query"
} }
], ],
"responses": { "responses": {