Browse Source

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
release/v1.9
Lanre Adelowo 4 years ago
committed by Lunny Xiao
parent
commit
de6539fc8c
  1. 8
      models/fixtures/milestone.yml
  2. 5
      models/fixtures/repository.yml
  3. 22
      models/issue_milestone.go
  4. 39
      models/issue_milestone_test.go
  5. 2
      modules/structs/issue.go
  6. 8
      routers/api/v1/repo/milestone.go
  7. 3
      routers/repo/issue.go
  8. 6
      templates/swagger/v1_json.tmpl

8
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

5
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
is_mirror: false

22
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.

39
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)
}

2
modules/structs/issue.go

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

8
routers/api/v1/repo/milestone.go

@ -14,7 +14,7 @@ import (
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) {
// swagger:operation GET /repos/{owner}/{repo}/milestones issue issueGetMilestonesList
// ---
@ -32,10 +32,14 @@ func ListMilestones(ctx *context.APIContext) {
// description: name of the repo
// type: string
// required: true
// - name: state
// in: query
// description: Milestone state, Recognised values are open, closed and all. Defaults to "open"
// type: string
// responses:
// "200":
// "$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 {
ctx.Error(500, "GetMilestonesByRepoID", err)
return

3
routers/repo/issue.go

@ -24,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"github.com/Unknwon/com"
@ -305,7 +306,7 @@ func Issues(ctx *context.Context) {
var err error
// 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 {
ctx.ServerError("GetAllRepoMilestones", err)
return

6
templates/swagger/v1_json.tmpl

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

Loading…
Cancel
Save