2013-05-14 08:33:31 -04:00
|
|
|
module API
|
2012-08-16 14:51:31 -04:00
|
|
|
# Milestones API
|
|
|
|
class Milestones < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
|
2016-04-06 07:03:07 -04:00
|
|
|
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
|
|
|
|
|
2012-08-16 14:51:31 -04:00
|
|
|
resource :projects do
|
|
|
|
# Get a list of project milestones
|
|
|
|
#
|
|
|
|
# Parameters:
|
2016-04-06 07:03:07 -04:00
|
|
|
# id (required) - The ID of a project
|
|
|
|
# state (optional) - Return "active" or "closed" milestones
|
2012-08-16 14:51:31 -04:00
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/milestones
|
2016-04-07 04:59:54 -04:00
|
|
|
# GET /projects/:id/milestones?iid=42
|
2016-04-06 07:03:07 -04:00
|
|
|
# GET /projects/:id/milestones?state=active
|
|
|
|
# GET /projects/:id/milestones?state=closed
|
2012-08-16 14:51:31 -04:00
|
|
|
get ":id/milestones" do
|
2012-09-21 06:22:30 -04:00
|
|
|
authorize! :read_milestone, user_project
|
|
|
|
|
2016-04-06 07:03:07 -04:00
|
|
|
milestones = user_project.milestones
|
2016-04-06 14:53:17 -04:00
|
|
|
milestones = filter_milestones_state(milestones, params[:state])
|
2016-04-07 04:59:54 -04:00
|
|
|
milestones = filter_by_iid(milestones, params[:iid]) if params[:iid].present?
|
2016-04-06 07:03:07 -04:00
|
|
|
|
|
|
|
present paginate(milestones), with: Entities::Milestone
|
2012-08-16 14:51:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Get a single project milestone
|
|
|
|
#
|
|
|
|
# Parameters:
|
2012-12-21 12:47:04 -05:00
|
|
|
# id (required) - The ID of a project
|
2012-08-16 14:51:31 -04:00
|
|
|
# milestone_id (required) - The ID of a project milestone
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/milestones/:milestone_id
|
|
|
|
get ":id/milestones/:milestone_id" do
|
2012-09-21 06:22:30 -04:00
|
|
|
authorize! :read_milestone, user_project
|
|
|
|
|
2012-08-16 14:51:31 -04:00
|
|
|
@milestone = user_project.milestones.find(params[:milestone_id])
|
|
|
|
present @milestone, with: Entities::Milestone
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a new project milestone
|
|
|
|
#
|
|
|
|
# Parameters:
|
2012-12-21 12:47:04 -05:00
|
|
|
# id (required) - The ID of the project
|
2012-08-16 14:51:31 -04:00
|
|
|
# title (required) - The title of the milestone
|
|
|
|
# description (optional) - The description of the milestone
|
|
|
|
# due_date (optional) - The due date of the milestone
|
|
|
|
# Example Request:
|
|
|
|
# POST /projects/:id/milestones
|
|
|
|
post ":id/milestones" do
|
2014-06-10 11:07:52 -04:00
|
|
|
authorize! :admin_milestone, user_project
|
|
|
|
required_attributes! [:title]
|
|
|
|
attrs = attributes_for_keys [:title, :description, :due_date]
|
|
|
|
milestone = ::Milestones::CreateService.new(user_project, current_user, attrs).execute
|
2013-02-20 16:51:59 -05:00
|
|
|
|
2014-06-10 11:07:52 -04:00
|
|
|
if milestone.valid?
|
|
|
|
present milestone, with: Entities::Milestone
|
|
|
|
else
|
2015-01-07 04:46:00 -05:00
|
|
|
render_api_error!("Failed to create milestone #{milestone.errors.messages}", 400)
|
2012-08-16 14:51:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update an existing project milestone
|
|
|
|
#
|
|
|
|
# Parameters:
|
2012-12-21 12:47:04 -05:00
|
|
|
# id (required) - The ID of a project
|
2012-08-29 08:45:00 -04:00
|
|
|
# milestone_id (required) - The ID of a project milestone
|
2012-08-16 14:51:31 -04:00
|
|
|
# title (optional) - The title of a milestone
|
|
|
|
# description (optional) - The description of a milestone
|
|
|
|
# due_date (optional) - The due date of a milestone
|
2013-06-06 05:32:05 -04:00
|
|
|
# state_event (optional) - The state event of the milestone (close|activate)
|
2012-08-16 14:51:31 -04:00
|
|
|
# Example Request:
|
|
|
|
# PUT /projects/:id/milestones/:milestone_id
|
|
|
|
put ":id/milestones/:milestone_id" do
|
2014-06-10 11:07:52 -04:00
|
|
|
authorize! :admin_milestone, user_project
|
|
|
|
attrs = attributes_for_keys [:title, :description, :due_date, :state_event]
|
|
|
|
milestone = user_project.milestones.find(params[:milestone_id])
|
|
|
|
milestone = ::Milestones::UpdateService.new(user_project, current_user, attrs).execute(milestone)
|
2012-09-10 02:06:11 -04:00
|
|
|
|
2014-06-10 11:07:52 -04:00
|
|
|
if milestone.valid?
|
|
|
|
present milestone, with: Entities::Milestone
|
|
|
|
else
|
2015-01-07 04:46:00 -05:00
|
|
|
render_api_error!("Failed to update milestone #{milestone.errors.messages}", 400)
|
2012-08-16 14:51:31 -04:00
|
|
|
end
|
|
|
|
end
|
2014-06-02 19:10:38 -04:00
|
|
|
|
2015-01-21 17:59:30 -05:00
|
|
|
# Get all issues for a single project milestone
|
2014-06-02 19:10:38 -04:00
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# milestone_id (required) - The ID of a project milestone
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/milestones/:milestone_id/issues
|
|
|
|
get ":id/milestones/:milestone_id/issues" do
|
|
|
|
authorize! :read_milestone, user_project
|
|
|
|
|
|
|
|
@milestone = user_project.milestones.find(params[:milestone_id])
|
2016-04-24 23:07:59 -04:00
|
|
|
|
|
|
|
finder_params = {
|
|
|
|
project_id: user_project.id,
|
|
|
|
milestone_title: @milestone.title,
|
|
|
|
state: 'all'
|
|
|
|
}
|
|
|
|
|
|
|
|
issues = IssuesFinder.new(current_user, finder_params).execute
|
|
|
|
present paginate(issues), with: Entities::Issue, current_user: current_user
|
2014-06-02 19:10:38 -04:00
|
|
|
end
|
|
|
|
|
2012-08-16 14:51:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|