gitlab-org--gitlab-foss/lib/api/milestones.rb

122 lines
4.1 KiB
Ruby
Raw Normal View History

module API
2012-08-16 14:51:31 -04:00
# Milestones 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
2016-11-09 09:14:02 -05:00
params :optional_params do
optional :description, type: String, desc: 'The description of the milestone'
optional :due_date, type: String, desc: 'The due date of the milestone'
end
end
2016-11-09 09:14:02 -05:00
params do
requires :id, type: String, desc: 'The ID of a project'
end
2012-08-16 14:51:31 -04:00
resource :projects do
2016-11-09 09:14:02 -05:00
desc 'Get a list of project milestones' do
success Entities::Milestone
end
params do
optional :state, type: String, values: %w[active closed all], default: 'all',
desc: 'Return "active", "closed", or "all" milestones'
optional :iid, type: Array[Integer], desc: 'The IID of the milestone'
2016-11-09 09:14:02 -05:00
end
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
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?
present paginate(milestones), with: Entities::Milestone
2012-08-16 14:51:31 -04:00
end
2016-11-09 09:14:02 -05:00
desc 'Get a single project milestone' do
success Entities::Milestone
end
params do
requires :milestone_id, type: Integer, desc: 'The ID of a project milestone'
end
2012-08-16 14:51:31 -04:00
get ":id/milestones/:milestone_id" do
2012-09-21 06:22:30 -04:00
authorize! :read_milestone, user_project
2016-11-09 09:14:02 -05:00
milestone = user_project.milestones.find(params[:milestone_id])
present milestone, with: Entities::Milestone
2012-08-16 14:51:31 -04:00
end
2016-11-09 09:14:02 -05:00
desc 'Create a new project milestone' do
success Entities::Milestone
end
params do
requires :title, type: String, desc: 'The title of the milestone'
use :optional_params
end
2012-08-16 14:51:31 -04:00
post ":id/milestones" do
authorize! :admin_milestone, user_project
2016-11-09 09:14:02 -05:00
2016-11-10 10:32:59 -05:00
milestone = ::Milestones::CreateService.new(user_project, current_user, declared_params).execute
if milestone.valid?
present milestone, with: Entities::Milestone
else
render_api_error!("Failed to create milestone #{milestone.errors.messages}", 400)
2012-08-16 14:51:31 -04:00
end
end
2016-11-09 09:14:02 -05:00
desc 'Update an existing project milestone' do
success Entities::Milestone
end
params do
requires :milestone_id, type: Integer, desc: 'The ID of a project milestone'
optional :title, type: String, desc: 'The title of the milestone'
optional :state_event, type: String, values: %w[close activate],
desc: 'The state event of the milestone '
use :optional_params
at_least_one_of :title, :description, :due_date, :state_event
end
2012-08-16 14:51:31 -04:00
put ":id/milestones/:milestone_id" do
authorize! :admin_milestone, user_project
2016-11-10 10:32:59 -05:00
milestone = user_project.milestones.find(params.delete(:milestone_id))
2016-11-09 09:14:02 -05:00
2016-11-10 10:32:59 -05:00
milestone_params = declared_params(include_missing: false)
2016-11-09 09:14:02 -05:00
milestone = ::Milestones::UpdateService.new(user_project, current_user, milestone_params).execute(milestone)
2012-09-10 02:06:11 -04:00
if milestone.valid?
present milestone, with: Entities::Milestone
else
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
2016-11-09 09:14:02 -05:00
desc 'Get all issues for a single project milestone' do
success Entities::Issue
end
params do
requires :milestone_id, type: Integer, desc: 'The ID of a project milestone'
end
2014-06-02 19:10:38 -04:00
get ":id/milestones/:milestone_id/issues" do
authorize! :read_milestone, user_project
2016-11-09 09:14:02 -05:00
milestone = user_project.milestones.find(params[:milestone_id])
finder_params = {
project_id: user_project.id,
2016-11-09 09:14:02 -05:00
milestone_title: milestone.title
}
issues = IssuesFinder.new(current_user, finder_params).execute
present paginate(issues), with: Entities::Issue, current_user: current_user, project: user_project
2014-06-02 19:10:38 -04:00
end
2012-08-16 14:51:31 -04:00
end
end
end