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

155 lines
5.5 KiB
Ruby
Raw Normal View History

module API
2012-08-16 14:51:31 -04:00
class Milestones < Grape::API
2016-12-04 12:11:19 -05:00
include PaginationParams
2012-08-16 14:51:31 -04:00
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'
2016-11-15 12:48:30 -05:00
optional :due_date, type: String, desc: 'The due date of the milestone. The ISO 8601 date format (%Y-%m-%d)'
optional :start_date, type: String, desc: 'The start date of the milestone. The ISO 8601 date format (%Y-%m-%d)'
2016-11-09 09:14:02 -05:00
end
end
2016-11-09 09:14:02 -05:00
params do
requires :id, type: String, desc: 'The ID of a project'
end
resource :projects, requirements: { id: %r{[^/]+} } 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'
2017-02-09 09:28:19 -05:00
optional :iids, type: Array[Integer], desc: 'The IIDs of the milestones'
optional :search, type: String, desc: 'The search criteria for the title or description of the milestone'
2016-12-04 12:11:19 -05:00
use :pagination
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])
2017-02-09 09:28:19 -05:00
milestones = filter_by_iid(milestones, params[:iids]) if params[:iids].present?
milestones = filter_by_search(milestones, params[:search]) if params[:search]
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::IssueBasic
2016-11-09 09:14:02 -05:00
end
params do
requires :milestone_id, type: Integer, desc: 'The ID of a project milestone'
2016-12-04 12:11:19 -05:00
use :pagination
2016-11-09 09:14:02 -05:00
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,
milestone_title: milestone.title,
2017-06-07 17:14:23 -04:00
sort: 'label_priority'
}
issues = IssuesFinder.new(current_user, finder_params).execute
present paginate(issues), with: Entities::IssueBasic, current_user: current_user, project: user_project
2014-06-02 19:10:38 -04:00
end
desc 'Get all merge requests for a single project milestone' do
detail 'This feature was introduced in GitLab 9.'
success Entities::MergeRequestBasic
end
params do
requires :milestone_id, type: Integer, desc: 'The ID of a project milestone'
use :pagination
end
get ':id/milestones/:milestone_id/merge_requests' do
authorize! :read_milestone, user_project
milestone = user_project.milestones.find(params[:milestone_id])
finder_params = {
project_id: user_project.id,
milestone_title: milestone.title,
2017-06-07 17:14:23 -04:00
sort: 'label_priority'
}
merge_requests = MergeRequestsFinder.new(current_user, finder_params).execute
present paginate(merge_requests),
with: Entities::MergeRequestBasic,
current_user: current_user,
project: user_project
end
2012-08-16 14:51:31 -04:00
end
end
end