gitlabhq/lib/api/milestones.rb

95 lines
3 KiB
Ruby
Raw Normal View History

2012-08-16 20:51:31 +02:00
module Gitlab
# Milestones API
class Milestones < Grape::API
before { authenticate! }
resource :projects do
helpers do
# If an error occurs this helper method handles error codes for a given milestone
#
# Parameters:
# milestone_errors (required) - The erros collection of a milestone
#
def handle_milestone_errors(milestone_errors)
if milestone_errors[:title].any?
bad_request!(:title)
end
end
end
2012-08-16 20:51:31 +02:00
# Get a list of project milestones
#
# Parameters:
# id (required) - The ID of a project
2012-08-16 20:51:31 +02:00
# Example Request:
# GET /projects/:id/milestones
get ":id/milestones" do
2012-09-21 12:22:30 +02:00
authorize! :read_milestone, user_project
2012-09-03 13:46:29 +02:00
present paginate(user_project.milestones), with: Entities::Milestone
2012-08-16 20:51:31 +02:00
end
# Get a single project milestone
#
# Parameters:
# id (required) - The ID of a project
2012-08-16 20:51:31 +02: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 12:22:30 +02:00
authorize! :read_milestone, user_project
2012-08-16 20:51:31 +02:00
@milestone = user_project.milestones.find(params[:milestone_id])
present @milestone, with: Entities::Milestone
end
# Create a new project milestone
#
# Parameters:
# id (required) - The ID of the project
2012-08-16 20:51:31 +02: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
2012-09-21 12:22:30 +02:00
authorize! :admin_milestone, user_project
2012-09-16 19:08:57 +02:00
attrs = attributes_for_keys [:title, :description, :due_date]
2012-09-16 18:51:04 +02:00
@milestone = user_project.milestones.new attrs
2012-08-16 20:51:31 +02:00
if @milestone.save
present @milestone, with: Entities::Milestone
else
handle_milestone_errors(@milestone.errors)
2012-09-10 09:41:46 +02:00
not_found!
2012-08-16 20:51:31 +02:00
end
end
# Update an existing project milestone
#
# Parameters:
# id (required) - The ID of a project
# milestone_id (required) - The ID of a project milestone
2012-08-16 20:51:31 +02:00
# title (optional) - The title of a milestone
# description (optional) - The description of a milestone
# due_date (optional) - The due date of a milestone
# closed (optional) - The status of the milestone
# Example Request:
# PUT /projects/:id/milestones/:milestone_id
put ":id/milestones/:milestone_id" do
2012-09-10 08:06:11 +02:00
authorize! :admin_milestone, user_project
2012-08-16 20:51:31 +02:00
@milestone = user_project.milestones.find(params[:milestone_id])
2012-09-16 19:08:57 +02:00
attrs = attributes_for_keys [:title, :description, :due_date, :closed]
2012-09-16 18:51:04 +02:00
if @milestone.update_attributes attrs
2012-08-16 20:51:31 +02:00
present @milestone, with: Entities::Milestone
else
handle_milestone_errors(@milestone.errors)
2012-09-10 09:41:46 +02:00
not_found!
2012-08-16 20:51:31 +02:00
end
end
end
end
end