BigW Consortium Gitlab

milestones.rb 4.11 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
    helpers do
      def filter_milestones_state(milestones, state)
        case state
        when 'active' then milestones.active
        when 'closed' then milestones.closed
        else milestones
        end
      end
14 15 16 17 18

      params :optional_params do
        optional :description, type: String, desc: 'The description of the milestone'
        optional :due_date, type: String, desc: 'The due date of the milestone'
      end
19 20
    end

21 22 23
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
Robert Speicher committed
24
    resource :projects do
25 26 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'
        optional :iid, type: Integer, desc: 'The IID of the milestone'
      end
Robert Speicher committed
33
      get ":id/milestones" do
34 35
        authorize! :read_milestone, user_project

36
        milestones = user_project.milestones
37
        milestones = filter_milestones_state(milestones, params[:state])
38
        milestones = filter_by_iid(milestones, params[:iid]) if params[:iid].present?
39 40

        present paginate(milestones), with: Entities::Milestone
Robert Speicher committed
41 42
      end

43 44 45 46 47 48
      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
49
      get ":id/milestones/:milestone_id" do
50 51
        authorize! :read_milestone, user_project

52 53
        milestone = user_project.milestones.find(params[:milestone_id])
        present milestone, with: Entities::Milestone
Robert Speicher committed
54 55
      end

56 57 58 59 60 61 62
      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
63
      post ":id/milestones" do
64
        authorize! :admin_milestone, user_project
65

66
        milestone = ::Milestones::CreateService.new(user_project, current_user, declared_params).execute
67

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

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

90
        milestone_params = declared_params(include_missing: false)
91
        milestone = ::Milestones::UpdateService.new(user_project, current_user, milestone_params).execute(milestone)
randx committed
92

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

100 101 102 103 104 105
      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'
      end
106 107 108
      get ":id/milestones/:milestone_id/issues" do
        authorize! :read_milestone, user_project

109
        milestone = user_project.milestones.find(params[:milestone_id])
110 111 112

        finder_params = {
          project_id: user_project.id,
113
          milestone_title: milestone.title
114 115 116 117
        }

        issues = IssuesFinder.new(current_user, finder_params).execute
        present paginate(issues), with: Entities::Issue, current_user: current_user
118
      end
Robert Speicher committed
119 120 121
    end
  end
end