issues.rb 9.0 KB
Newer Older
1
module API
N
Nihad Abbasov 已提交
2
  class Issues < Grape::API
R
Robert Schilling 已提交
3 4
    include PaginationParams

N
Nihad Abbasov 已提交
5 6
    before { authenticate! }

J
jubianchi 已提交
7
    helpers do
J
jubianchi 已提交
8
      def filter_issues_state(issues, state)
J
jubianchi 已提交
9
        case state
10 11
        when 'opened' then issues.opened
        when 'closed' then issues.closed
12
        else issues
J
jubianchi 已提交
13 14
        end
      end
J
jubianchi 已提交
15 16

      def filter_issues_labels(issues, labels)
J
jubianchi 已提交
17 18 19 20 21
        issues.includes(:labels).where('labels.title' => labels.split(','))
      end

      def filter_issues_milestone(issues, milestone)
        issues.includes(:milestone).where('milestones.title' => milestone)
J
jubianchi 已提交
22
      end
23

R
Robert Schilling 已提交
24 25 26 27 28 29 30 31
      params :issues_params do
        optional :labels, type: String, desc: 'Comma-separated list of label names'
        optional :order_by, type: String, values: %w[created_at updated_at], default: 'created_at',
                            desc: 'Return issues ordered by `created_at` or `updated_at` fields.'
        optional :sort, type: String, values: %w[asc desc], default: 'desc',
                        desc: 'Return issues sorted in `asc` or `desc` order.'
        use :pagination
      end
32

R
Robert Schilling 已提交
33 34 35 36 37 38 39
      params :issue_params do
        optional :description, type: String, desc: 'The description of an issue'
        optional :assignee_id, type: Integer, desc: 'The ID of a user to assign issue'
        optional :milestone_id, type: Integer, desc: 'The ID of a milestone to assign issue'
        optional :labels, type: String, desc: 'Comma-separated list of label names'
        optional :due_date, type: String, desc: 'Date time string in the format YEAR-MONTH-DAY'
        optional :confidential, type: Boolean, desc: 'Boolean parameter if the issue should be confidential'
40
      end
J
jubianchi 已提交
41 42
    end

N
Nihad Abbasov 已提交
43
    resource :issues do
R
Robert Schilling 已提交
44 45 46 47 48 49 50 51
      desc "Get currently authenticated user's issues" do
        success Entities::Issue
      end
      params do
        optional :state, type: String, values: %w[opened closed all], default: 'all',
                         desc: 'Return opened, closed, or all issues'
        use :issues_params
      end
N
Nihad Abbasov 已提交
52
      get do
53
        issues = current_user.issues.inc_notes_with_associations
R
Robert Schilling 已提交
54
        issues = filter_issues_state(issues, params[:state])
J
jubianchi 已提交
55
        issues = filter_issues_labels(issues, params[:labels]) unless params[:labels].nil?
R
Robert Schilling 已提交
56
        issues = issues.reorder(params[:order_by] => params[:sort])
S
Sean McGivern 已提交
57

58
        present paginate(issues), with: Entities::Issue, current_user: current_user
N
Nihad Abbasov 已提交
59 60 61
      end
    end

R
Robert Schilling 已提交
62 63 64
    params do
      requires :id, type: String, desc: 'The ID of a group'
    end
65
    resource :groups do
R
Robert Schilling 已提交
66 67 68 69 70 71 72 73
      desc 'Get a list of group issues' do
        success Entities::Issue
      end
      params do
        optional :state, type: String, values: %w[opened closed all], default: 'opened',
                         desc: 'Return opened, closed, or all issues'
        use :issues_params
      end
74
      get ":id/issues" do
R
Robert Schilling 已提交
75
        group = find_group!(params.delete(:id))
76 77 78 79

        params[:group_id] = group.id
        params[:milestone_title] = params.delete(:milestone)
        params[:label_name] = params.delete(:labels)
S
Sean McGivern 已提交
80

81 82
        issues = IssuesFinder.new(current_user, params).execute

R
Robert Schilling 已提交
83
        issues = issues.reorder(params[:order_by] => params[:sort])
84 85 86 87
        present paginate(issues), with: Entities::Issue, current_user: current_user
      end
    end

88 89 90
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
N
Nihad Abbasov 已提交
91
    resource :projects do
R
Robert Schilling 已提交
92 93 94 95 96 97 98 99 100
      desc 'Get a list of project issues' do
        success Entities::Issue
      end
      params do
        optional :state, type: String, values: %w[opened closed all], default: 'all',
                         desc: 'Return opened, closed, or all issues'
        optional :iid, type: Integer, desc: 'The IID of the issue'
        use :issues_params
      end
N
Nihad Abbasov 已提交
101
      get ":id/issues" do
102
        issues = IssuesFinder.new(current_user, project_id: user_project.id).execute.inc_notes_with_associations
R
Robert Schilling 已提交
103
        issues = filter_issues_state(issues, params[:state])
J
jubianchi 已提交
104
        issues = filter_issues_labels(issues, params[:labels]) unless params[:labels].nil?
105
        issues = filter_by_iid(issues, params[:iid]) unless params[:iid].nil?
106

J
jubianchi 已提交
107 108 109
        unless params[:milestone].nil?
          issues = filter_issues_milestone(issues, params[:milestone])
        end
J
jubianchi 已提交
110

R
Robert Schilling 已提交
111
        issues = issues.reorder(params[:order_by] => params[:sort])
112
        present paginate(issues), with: Entities::Issue, current_user: current_user, project: user_project
N
Nihad Abbasov 已提交
113 114
      end

R
Robert Schilling 已提交
115 116 117 118 119 120
      desc 'Get a single project issue' do
        success Entities::Issue
      end
      params do
        requires :issue_id, type: Integer, desc: 'The ID of a project issue'
      end
N
Nihad Abbasov 已提交
121
      get ":id/issues/:issue_id" do
R
Robert Schilling 已提交
122 123
        issue = find_project_issue(params[:issue_id])
        present issue, with: Entities::Issue, current_user: current_user, project: user_project
N
Nihad Abbasov 已提交
124 125
      end

R
Robert Schilling 已提交
126 127 128 129 130 131 132 133 134 135 136
      desc 'Create a new project issue' do
        success Entities::Issue
      end
      params do
        requires :title, type: String, desc: 'The title of an issue'
        optional :created_at, type: DateTime,
                              desc: 'Date time when the issue was created. Available only for admins and project owners.'
        optional :merge_request_for_resolving_discussions, type: Integer,
                                                           desc: 'The IID of a merge request for which to resolve discussions'
        use :issue_params
      end
137
      post ':id/issues' do
R
Robert Schilling 已提交
138 139 140 141
        # Setting created_at time only allowed for admins and project owners
        unless current_user.admin? || user_project.owner == current_user
          params.delete(:created_at)
        end
142

R
Robert Schilling 已提交
143
        issue_params = declared_params(include_missing: false)
144 145

        if merge_request_iid = params[:merge_request_for_resolving_discussions]
R
Robert Schilling 已提交
146
          issue_params[:merge_request_for_resolving_discussions] = MergeRequestsFinder.new(current_user, project_id: user_project.id).
147 148 149
            execute.
            find_by(iid: merge_request_iid)
        end
150

R
Robert Schilling 已提交
151 152 153
        issue = ::Issues::CreateService.new(user_project,
                                            current_user,
                                            issue_params.merge(request: request, api: true)).execute
154
        if issue.spam?
155 156
          render_api_error!({ error: 'Spam detected' }, 400)
        end
157

158
        if issue.valid?
159
          present issue, with: Entities::Issue, current_user: current_user, project: user_project
160
        else
J
jubianchi 已提交
161
          render_validation_error!(issue)
N
Nihad Abbasov 已提交
162 163 164
        end
      end

165 166 167 168
      desc 'Update an existing issue' do
        success Entities::Issue
      end
      params do
R
Robert Schilling 已提交
169 170 171 172
        requires :issue_id, type: Integer, desc: 'The ID of a project issue'
        optional :title, type: String, desc: 'The title of an issue'
        optional :updated_at, type: DateTime,
                              desc: 'Date time when the issue was updated. Available only for admins and project owners.'
173
        optional :state_event, type: String, values: %w[reopen close], desc: 'State of the issue'
R
Robert Schilling 已提交
174 175 176
        use :issue_params
        at_least_one_of :title, :description, :assignee_id, :milestone_id,
                        :labels, :created_at, :due_date, :confidential, :state_event
177
      end
178
      put ':id/issues/:issue_id' do
R
Robert Schilling 已提交
179
        issue = user_project.issues.find(params.delete(:issue_id))
180
        authorize! :update_issue, issue
181

R
Robert Schilling 已提交
182 183 184 185
        # Setting created_at time only allowed for admins and project owners
        unless current_user.admin? || user_project.owner == current_user
          params.delete(:updated_at)
        end
186

R
Robert Schilling 已提交
187 188 189
        issue = ::Issues::UpdateService.new(user_project,
                                            current_user,
                                            declared_params(include_missing: false)).execute(issue)
190

191
        if issue.valid?
192
          present issue, with: Entities::Issue, current_user: current_user, project: user_project
193
        else
J
jubianchi 已提交
194
          render_validation_error!(issue)
N
Nihad Abbasov 已提交
195 196 197
        end
      end

R
Robert Schilling 已提交
198 199 200 201 202 203 204
      desc 'Move an existing issue' do
        success Entities::Issue
      end
      params do
        requires :issue_id, type: Integer, desc: 'The ID of a project issue'
        requires :to_project_id, type: Integer, desc: 'The ID of the new project'
      end
205
      post ':id/issues/:issue_id/move' do
R
Robert Schilling 已提交
206 207
        issue = user_project.issues.find_by(id: params[:issue_id])
        not_found!('Issue') unless issue
R
Robert Schilling 已提交
208

R
Robert Schilling 已提交
209 210
        new_project = Project.find_by(id: params[:to_project_id])
        not_found!('Project') unless new_project
R
Robert Schilling 已提交
211 212 213

        begin
          issue = ::Issues::MoveService.new(user_project, current_user).execute(issue, new_project)
214
          present issue, with: Entities::Issue, current_user: current_user, project: user_project
R
Robert Schilling 已提交
215 216 217 218 219
        rescue ::Issues::MoveService::MoveError => error
          render_api_error!(error.message, 400)
        end
      end

R
Robert Schilling 已提交
220 221 222 223
      desc 'Delete a project issue'
      params do
        requires :issue_id, type: Integer, desc: 'The ID of a project issue'
      end
N
Nihad Abbasov 已提交
224
      delete ":id/issues/:issue_id" do
225
        issue = user_project.issues.find_by(id: params[:issue_id])
R
Robert Schilling 已提交
226
        not_found!('Issue') unless issue
Z
Zeger-Jan van de Weg 已提交
227

228
        authorize!(:destroy_issue, issue)
Z
Zeger-Jan van de Weg 已提交
229
        issue.destroy
N
Nihad Abbasov 已提交
230 231 232 233
      end
    end
  end
end