milestones.rb 2.58 KB
Newer Older
Robert Speicher's avatar
Robert Speicher committed
1 2 3 4 5 6 7 8 9 10 11 12 13
module Gitlab
  # Milestones API
  class Milestones < Grape::API
    before { authenticate! }

    resource :projects do
      # Get a list of project milestones
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
      # Example Request:
      #   GET /projects/:id/milestones
      get ":id/milestones" do
14 15
        authorize! :read_milestone, user_project

Nihad Abbasov's avatar
Nihad Abbasov committed
16
        present paginate(user_project.milestones), with: Entities::Milestone
Robert Speicher's avatar
Robert Speicher committed
17 18 19 20 21 22 23 24 25 26
      end

      # Get a single project milestone
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
      #   milestone_id (required) - The ID of a project milestone
      # Example Request:
      #   GET /projects/:id/milestones/:milestone_id
      get ":id/milestones/:milestone_id" do
27 28
        authorize! :read_milestone, user_project

Robert Speicher's avatar
Robert Speicher committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42
        @milestone = user_project.milestones.find(params[:milestone_id])
        present @milestone, with: Entities::Milestone
      end

      # Create a new project milestone
      #
      # Parameters:
      #   id (required) - The ID or code name of the project
      #   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
43 44
        authorize! :admin_milestone, user_project

Alex Denisov's avatar
Alex Denisov committed
45
        attrs = attributes_for_keys [:title, :description, :due_date]
Alex Denisov's avatar
Alex Denisov committed
46
        @milestone = user_project.milestones.new attrs
Robert Speicher's avatar
Robert Speicher committed
47 48 49
        if @milestone.save
          present @milestone, with: Entities::Milestone
        else
50
          not_found!
Robert Speicher's avatar
Robert Speicher committed
51 52 53 54 55 56 57
        end
      end

      # Update an existing project milestone
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
58
      #   milestone_id (required) - The ID of a project milestone
Robert Speicher's avatar
Robert Speicher committed
59 60 61 62 63 64 65
      #   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
randx's avatar
randx committed
66 67
        authorize! :admin_milestone, user_project

Robert Speicher's avatar
Robert Speicher committed
68
        @milestone = user_project.milestones.find(params[:milestone_id])
Alex Denisov's avatar
Alex Denisov committed
69
        attrs = attributes_for_keys [:title, :description, :due_date, :closed]
Alex Denisov's avatar
Alex Denisov committed
70
        if @milestone.update_attributes attrs
Robert Speicher's avatar
Robert Speicher committed
71 72
          present @milestone, with: Entities::Milestone
        else
73
          not_found!
Robert Speicher's avatar
Robert Speicher committed
74 75 76 77 78
        end
      end
    end
  end
end