BigW Consortium Gitlab

milestones.rb 4.35 KB
Newer Older
1
module API
Robert Speicher committed
2
  class Milestones < Grape::API
3 4
    include PaginationParams

Robert Speicher committed
5 6
    before { authenticate! }

7 8 9 10 11 12 13 14
    helpers do
      def filter_milestones_state(milestones, state)
        case state
        when 'active' then milestones.active
        when 'closed' then milestones.closed
        else milestones
        end
      end
15 16 17

      params :optional_params do
        optional :description, type: String, desc: 'The description of the milestone'
18 19
        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)'
20
      end
21 22
    end

23 24 25
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
Robert Speicher committed
26
    resource :projects do
27 28 29 30 31 32
      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'
33
        optional :iid, type: Array[Integer], desc: 'The IID of the milestone'
34
        use :pagination
35
      end
Robert Speicher committed
36
      get ":id/milestones" do
37 38
        authorize! :read_milestone, user_project

39
        milestones = user_project.milestones
40
        milestones = filter_milestones_state(milestones, params[:state])
41
        milestones = filter_by_iid(milestones, params[:iid]) if params[:iid].present?
42 43

        present paginate(milestones), with: Entities::Milestone
Robert Speicher committed
44 45
      end

46 47 48 49 50 51
      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
Robert Speicher committed
52
      get ":id/milestones/:milestone_id" do
53 54
        authorize! :read_milestone, user_project

55 56
        milestone = user_project.milestones.find(params[:milestone_id])
        present milestone, with: Entities::Milestone
Robert Speicher committed
57 58
      end

59 60 61 62 63 64 65
      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
Robert Speicher committed
66
      post ":id/milestones" do
67
        authorize! :admin_milestone, user_project
68

69
        milestone = ::Milestones::CreateService.new(user_project, current_user, declared_params).execute
70

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

78 79 80 81 82 83 84 85 86 87 88
      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
Robert Speicher committed
89
      put ":id/milestones/:milestone_id" do
90
        authorize! :admin_milestone, user_project
91
        milestone = user_project.milestones.find(params.delete(:milestone_id))
92

93
        milestone_params = declared_params(include_missing: false)
94
        milestone = ::Milestones::UpdateService.new(user_project, current_user, milestone_params).execute(milestone)
randx committed
95

96 97 98
        if milestone.valid?
          present milestone, with: Entities::Milestone
        else
99
          render_api_error!("Failed to update milestone #{milestone.errors.messages}", 400)
Robert Speicher committed
100 101
        end
      end
102

103 104 105 106 107
      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'
108
        use :pagination
109
      end
110 111 112
      get ":id/milestones/:milestone_id/issues" do
        authorize! :read_milestone, user_project

113
        milestone = user_project.milestones.find(params[:milestone_id])
114 115 116

        finder_params = {
          project_id: user_project.id,
117
          milestone_title: milestone.title
118 119 120
        }

        issues = IssuesFinder.new(current_user, finder_params).execute
121
        present paginate(issues), with: Entities::Issue, current_user: current_user, project: user_project
122
      end
Robert Speicher committed
123 124 125
    end
  end
end