BigW Consortium Gitlab

milestones.rb 4.24 KB
Newer Older
1
module API
Robert Speicher committed
2 3 4 5
  # Milestones API
  class Milestones < Grape::API
    before { authenticate! }

6 7 8 9 10 11 12 13 14 15
    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

Robert Speicher committed
16 17 18 19
    resource :projects do
      # Get a list of project milestones
      #
      # Parameters:
20 21
      #   id (required)    - The ID of a project
      #   state (optional) - Return "active" or "closed" milestones
Robert Speicher committed
22 23
      # Example Request:
      #   GET /projects/:id/milestones
24
      #   GET /projects/:id/milestones?iid=42
25 26
      #   GET /projects/:id/milestones?state=active
      #   GET /projects/:id/milestones?state=closed
Robert Speicher committed
27
      get ":id/milestones" do
28 29
        authorize! :read_milestone, user_project

30
        milestones = user_project.milestones
31
        milestones = filter_milestones_state(milestones, params[:state])
32
        milestones = filter_by_iid(milestones, params[:iid]) if params[:iid].present?
33 34

        present paginate(milestones), with: Entities::Milestone
Robert Speicher committed
35 36 37 38 39
      end

      # Get a single project milestone
      #
      # Parameters:
40
      #   id (required) - The ID of a project
Robert Speicher committed
41 42 43 44
      #   milestone_id (required) - The ID of a project milestone
      # Example Request:
      #   GET /projects/:id/milestones/:milestone_id
      get ":id/milestones/:milestone_id" do
45 46
        authorize! :read_milestone, user_project

Robert Speicher committed
47 48 49 50 51 52 53
        @milestone = user_project.milestones.find(params[:milestone_id])
        present @milestone, with: Entities::Milestone
      end

      # Create a new project milestone
      #
      # Parameters:
54
      #   id (required) - The ID of the project
Robert Speicher committed
55 56 57 58 59 60
      #   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
61 62 63 64
        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
65

66 67 68
        if milestone.valid?
          present milestone, with: Entities::Milestone
        else
69
          render_api_error!("Failed to create milestone #{milestone.errors.messages}", 400)
Robert Speicher committed
70 71 72 73 74 75
        end
      end

      # Update an existing project milestone
      #
      # Parameters:
76
      #   id (required) - The ID of a project
77
      #   milestone_id (required) - The ID of a project milestone
Robert Speicher committed
78 79 80
      #   title (optional) - The title of a milestone
      #   description (optional) - The description of a milestone
      #   due_date (optional) - The due date of a milestone
81
      #   state_event (optional) - The state event of the milestone (close|activate)
Robert Speicher committed
82 83 84
      # Example Request:
      #   PUT /projects/:id/milestones/:milestone_id
      put ":id/milestones/:milestone_id" do
85 86 87 88
        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)
randx committed
89

90 91 92
        if milestone.valid?
          present milestone, with: Entities::Milestone
        else
93
          render_api_error!("Failed to update milestone #{milestone.errors.messages}", 400)
Robert Speicher committed
94 95
        end
      end
96

97
      # Get all issues for a single project milestone
98 99 100 101 102 103 104 105 106 107
      #
      # 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])
108 109 110 111 112 113 114 115 116

        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
117 118
      end

Robert Speicher committed
119 120 121
    end
  end
end