BigW Consortium Gitlab

milestones.rb 5.51 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
26
    resource :projects, requirements: { id: %r{[^/]+} } 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 :iids, type: Array[Integer], desc: 'The IIDs of the milestones'
34
        optional :search, type: String, desc: 'The search criteria for the title or description of the milestone'
35
        use :pagination
36
      end
Robert Speicher committed
37
      get ":id/milestones" do
38 39
        authorize! :read_milestone, user_project

40
        milestones = user_project.milestones
41
        milestones = filter_milestones_state(milestones, params[:state])
42
        milestones = filter_by_iid(milestones, params[:iids]) if params[:iids].present?
43
        milestones = filter_by_search(milestones, params[:search]) if params[:search]
44 45

        present paginate(milestones), with: Entities::Milestone
Robert Speicher committed
46 47
      end

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

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

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

71
        milestone = ::Milestones::CreateService.new(user_project, current_user, declared_params).execute
72

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

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

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

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

105
      desc 'Get all issues for a single project milestone' do
106
        success Entities::IssueBasic
107 108 109
      end
      params do
        requires :milestone_id, type: Integer, desc: 'The ID of a project milestone'
110
        use :pagination
111
      end
112 113 114
      get ":id/milestones/:milestone_id/issues" do
        authorize! :read_milestone, user_project

115
        milestone = user_project.milestones.find(params[:milestone_id])
116 117 118

        finder_params = {
          project_id: user_project.id,
119 120
          milestone_title: milestone.title,
          sort: 'position_asc'
121 122 123
        }

        issues = IssuesFinder.new(current_user, finder_params).execute
124
        present paginate(issues), with: Entities::IssueBasic, current_user: current_user, project: user_project
125
      end
126 127 128

      desc 'Get all merge requests for a single project milestone' do
        detail 'This feature was introduced in GitLab 9.'
129
        success Entities::MergeRequestBasic
130 131 132 133 134 135 136 137 138 139 140 141
      end
      params do
        requires :milestone_id, type: Integer, desc: 'The ID of a project milestone'
        use :pagination
      end
      get ':id/milestones/:milestone_id/merge_requests' do
        authorize! :read_milestone, user_project

        milestone = user_project.milestones.find(params[:milestone_id])

        finder_params = {
          project_id: user_project.id,
142
          milestone_title: milestone.title,
143
          sort: 'position_asc'
144 145 146
        }

        merge_requests = MergeRequestsFinder.new(current_user, finder_params).execute
147 148 149 150
        present paginate(merge_requests),
          with: Entities::MergeRequestBasic,
          current_user: current_user,
          project: user_project
151
      end
Robert Speicher committed
152 153 154
    end
  end
end