Commit 0728588c authored by Robert Schilling's avatar Robert Schilling

API: Ability to filter milestones by state

parent 77e178b5
...@@ -25,6 +25,7 @@ v 8.7.0 (unreleased) ...@@ -25,6 +25,7 @@ v 8.7.0 (unreleased)
- Build status notifications - Build status notifications
- API: Ability to retrieve a specific tag (Robert Schilling) - API: Ability to retrieve a specific tag (Robert Schilling)
- API: Expose user location (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) v 8.6.5 (unreleased)
- Check permissions when user attempts to import members from another project - Check permissions when user attempts to import members from another project
......
...@@ -7,6 +7,8 @@ Returns a list of project milestones. ...@@ -7,6 +7,8 @@ Returns a list of project milestones.
``` ```
GET /projects/:id/milestones GET /projects/:id/milestones
GET /projects/:id/milestones?iid=42 GET /projects/:id/milestones?iid=42
GET /projects/:id/milestones?state=active
GET /projects/:id/milestones?state=closed
``` ```
```json ```json
...@@ -29,6 +31,7 @@ Parameters: ...@@ -29,6 +31,7 @@ Parameters:
- `id` (required) - The ID of a project - `id` (required) - The ID of a project
- `iid` (optional) - Return the milestone having the given `iid` - `iid` (optional) - Return the milestone having the given `iid`
- `state` (optional) - Return "active" or "closed" milestones
## Get single milestone ## Get single milestone
......
...@@ -3,17 +3,33 @@ module API ...@@ -3,17 +3,33 @@ module API
class Milestones < Grape::API class Milestones < Grape::API
before { authenticate! } 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 resource :projects do
# Get a list of project milestones # Get a list of project milestones
# #
# Parameters: # Parameters:
# id (required) - The ID of a project # id (required) - The ID of a project
# state (optional) - Return "active" or "closed" milestones
# Example Request: # Example Request:
# GET /projects/:id/milestones # GET /projects/:id/milestones
# GET /projects/:id/milestones?state=active
# GET /projects/:id/milestones?state=closed
get ":id/milestones" do get ":id/milestones" do
authorize! :read_milestone, user_project 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 end
# Get a single project milestone # Get a single project milestone
......
...@@ -4,6 +4,7 @@ describe API::API, api: true do ...@@ -4,6 +4,7 @@ describe API::API, api: true do
include ApiHelpers include ApiHelpers
let(:user) { create(:user) } let(:user) { create(:user) }
let!(:project) { create(:project, namespace: user.namespace ) } let!(:project) { create(:project, namespace: user.namespace ) }
let!(:closed_milestone) { create(:closed_milestone, project: project, state: :closed) }
let!(:milestone) { create(:milestone, project: project) } let!(:milestone) { create(:milestone, project: project) }
before { project.team << [user, :developer] } before { project.team << [user, :developer] }
...@@ -20,6 +21,22 @@ describe API::API, api: true do ...@@ -20,6 +21,22 @@ describe API::API, api: true do
get api("/projects/#{project.id}/milestones") get api("/projects/#{project.id}/milestones")
expect(response.status).to eq(401) expect(response.status).to eq(401)
end 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 end
describe 'GET /projects/:id/milestones/:milestone_id' do describe 'GET /projects/:id/milestones/:milestone_id' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment