API: Ability to filter milestones by state

This commit is contained in:
Robert Schilling 2016-04-06 13:03:07 +02:00
parent 77e178b5e1
commit 0728588c34
4 changed files with 39 additions and 2 deletions

View file

@ -25,6 +25,7 @@ v 8.7.0 (unreleased)
- Build status notifications
- API: Ability to retrieve a specific tag (Robert Schilling)
- API: Expose user location (Robert Schilling)
- API: Ability to milter milestones by state `active` and `closed` (Robert Schilling)
v 8.6.5 (unreleased)
- Check permissions when user attempts to import members from another project

View file

@ -7,6 +7,8 @@ Returns a list of project milestones.
```
GET /projects/:id/milestones
GET /projects/:id/milestones?iid=42
GET /projects/:id/milestones?state=active
GET /projects/:id/milestones?state=closed
```
```json
@ -29,6 +31,7 @@ Parameters:
- `id` (required) - The ID of a project
- `iid` (optional) - Return the milestone having the given `iid`
- `state` (optional) - Return "active" or "closed" milestones
## Get single milestone

View file

@ -3,17 +3,33 @@ module API
class Milestones < Grape::API
before { authenticate! }
helpers do
def filter_milestones_state(milestones, state)
case state
when 'active' then milestones.active
when 'closed' then milestones.closed
else milestones
end
end
end
resource :projects do
# Get a list of project milestones
#
# Parameters:
# id (required) - The ID of a project
# id (required) - The ID of a project
# state (optional) - Return "active" or "closed" milestones
# Example Request:
# GET /projects/:id/milestones
# GET /projects/:id/milestones?state=active
# GET /projects/:id/milestones?state=closed
get ":id/milestones" do
authorize! :read_milestone, user_project
present paginate(user_project.milestones), with: Entities::Milestone
milestones = user_project.milestones
milestones = filter_milestones_state(milestones, params[:state]) unless params[:state].nil?
present paginate(milestones), with: Entities::Milestone
end
# Get a single project milestone

View file

@ -4,6 +4,7 @@ describe API::API, api: true do
include ApiHelpers
let(:user) { create(:user) }
let!(:project) { create(:project, namespace: user.namespace ) }
let!(:closed_milestone) { create(:closed_milestone, project: project, state: :closed) }
let!(:milestone) { create(:milestone, project: project) }
before { project.team << [user, :developer] }
@ -20,6 +21,22 @@ describe API::API, api: true do
get api("/projects/#{project.id}/milestones")
expect(response.status).to eq(401)
end
it 'should return an array of active milestones' do
get api("/projects/#{project.id}/milestones?state=active", user)
expect(response.status).to eq(200)
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
expect(json_response.first['id']).to eq(milestone.id)
end
it 'should return an array of closed milestones' do
get api("/projects/#{project.id}/milestones?state=closed", user)
expect(response.status).to eq(200)
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
expect(json_response.first['id']).to eq(closed_milestone.id)
end
end
describe 'GET /projects/:id/milestones/:milestone_id' do