issues.rb 14.8 KB
Newer Older
1 2
# frozen_string_literal: true

3
module API
N
Nihad Abbasov 已提交
4
  class Issues < Grape::API
R
Robert Schilling 已提交
5 6
    include PaginationParams

7
    before { authenticate_non_get! }
N
Nihad Abbasov 已提交
8

9 10
    helpers ::Gitlab::IssuableMetadata

11 12 13 14 15 16 17 18 19
    # EE::API::Issues would override the following helpers
    helpers do
      params :issues_params_ee do
      end

      params :issue_params_ee do
      end
    end

J
jubianchi 已提交
20
    helpers do
21
      # rubocop: disable CodeReuse/ActiveRecord
22
      def find_issues(args = {})
23
        args = declared_params.merge(args)
24 25 26

        args.delete(:id)
        args[:milestone_title] = args.delete(:milestone)
27
        args[:label_name] = args.delete(:labels)
28
        args[:scope] = args[:scope].underscore if args[:scope]
29

30
        issues = IssuesFinder.new(current_user, args).execute
31
          .preload(:assignees, :labels, :notes, :timelogs, :project, :author, :closed_by)
32
        issues.reorder(order_options_with_tie_breaker)
J
jubianchi 已提交
33
      end
34
      # rubocop: enable CodeReuse/ActiveRecord
J
jubianchi 已提交
35

R
Robert Schilling 已提交
36 37
      params :issues_params do
        optional :labels, type: String, desc: 'Comma-separated list of label names'
38
        optional :milestone, type: String, desc: 'Milestone title'
R
Robert Schilling 已提交
39 40 41 42
        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.'
43
        optional :milestone, type: String, desc: 'Return issues for a specific milestone'
44
        optional :iids, type: Array[Integer], desc: 'The IID array of issues'
H
Hiroyuki Sato 已提交
45 46
        optional :search, type: String, desc: 'Search issues for text present in the title, description, or any combination of these'
        optional :in, type: String, desc: '`title`, `description`, or a string joining them with comma'
47 48
        optional :created_after, type: DateTime, desc: 'Return issues created after the specified time'
        optional :created_before, type: DateTime, desc: 'Return issues created before the specified time'
49 50
        optional :updated_after, type: DateTime, desc: 'Return issues updated after the specified time'
        optional :updated_before, type: DateTime, desc: 'Return issues updated before the specified time'
51
        optional :author_id, type: Integer, desc: 'Return issues which are authored by the user with the given ID'
52
        optional :assignee_id, types: [Integer, String], integer_none_any: true,
53
                               desc: 'Return issues which are assigned to the user with the given ID'
54 55
        optional :scope, type: String, values: %w[created-by-me assigned-to-me created_by_me assigned_to_me all],
                         desc: 'Return issues for the given scope: `created_by_me`, `assigned_to_me` or `all`'
56
        optional :my_reaction_emoji, type: String, desc: 'Return issues reacted by the authenticated user by the given emoji'
57
        optional :confidential, type: Boolean, desc: 'Filter confidential or public issues'
R
Robert Schilling 已提交
58
        use :pagination
59 60

        use :issues_params_ee
R
Robert Schilling 已提交
61
      end
62

63
      params :issue_params do
R
Robert Schilling 已提交
64
        optional :description, type: String, desc: 'The description of an issue'
65 66
        optional :assignee_ids, type: Array[Integer], desc: 'The array of user IDs to assign issue'
        optional :assignee_id,  type: Integer, desc: '[Deprecated] The ID of a user to assign issue'
R
Robert Schilling 已提交
67 68
        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'
69
        optional :due_date, type: String, desc: 'Date string in the format YEAR-MONTH-DAY'
R
Robert Schilling 已提交
70
        optional :confidential, type: Boolean, desc: 'Boolean parameter if the issue should be confidential'
71
        optional :discussion_locked, type: Boolean, desc: " Boolean parameter indicating if the issue's discussion is locked"
72

73
        use :issue_params_ee
74
      end
J
jubianchi 已提交
75 76
    end

N
Nihad Abbasov 已提交
77
    resource :issues do
R
Robert Schilling 已提交
78
      desc "Get currently authenticated user's issues" do
79
        success Entities::IssueBasic
R
Robert Schilling 已提交
80 81 82 83 84
      end
      params do
        optional :state, type: String, values: %w[opened closed all], default: 'all',
                         desc: 'Return opened, closed, or all issues'
        use :issues_params
85 86
        optional :scope, type: String, values: %w[created-by-me assigned-to-me created_by_me assigned_to_me all], default: 'created_by_me',
                         desc: 'Return issues for the given scope: `created_by_me`, `assigned_to_me` or `all`'
R
Robert Schilling 已提交
87
      end
N
Nihad Abbasov 已提交
88
      get do
89
        authenticate! unless params[:scope] == 'all'
90
        issues = paginate(find_issues)
S
Sean McGivern 已提交
91

92 93 94 95 96
        options = {
          with: Entities::IssueBasic,
          current_user: current_user,
          issuable_metadata: issuable_meta_data(issues, 'Issue')
        }
97

98
        present issues, options
N
Nihad Abbasov 已提交
99 100 101
      end
    end

R
Robert Schilling 已提交
102 103 104
    params do
      requires :id, type: String, desc: 'The ID of a group'
    end
105
    resource :groups, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
R
Robert Schilling 已提交
106
      desc 'Get a list of group issues' do
107
        success Entities::IssueBasic
R
Robert Schilling 已提交
108 109
      end
      params do
110
        optional :state, type: String, values: %w[opened closed all], default: 'all',
R
Robert Schilling 已提交
111 112 113
                         desc: 'Return opened, closed, or all issues'
        use :issues_params
      end
114
      get ":id/issues" do
115
        group = find_group!(params[:id])
116

117
        issues = paginate(find_issues(group_id: group.id, include_subgroups: true))
S
Sean McGivern 已提交
118

119 120 121 122 123
        options = {
          with: Entities::IssueBasic,
          current_user: current_user,
          issuable_metadata: issuable_meta_data(issues, 'Issue')
        }
124

125
        present issues, options
126 127 128
      end
    end

129 130 131
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
132
    resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
133 134
      include TimeTrackingEndpoints

R
Robert Schilling 已提交
135
      desc 'Get a list of project issues' do
136
        success Entities::IssueBasic
R
Robert Schilling 已提交
137 138 139 140 141 142
      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 已提交
143
      get ":id/issues" do
144
        project = find_project!(params[:id])
145

146
        issues = paginate(find_issues(project_id: project.id))
R
Robert Schilling 已提交
147

148 149 150 151 152 153
        options = {
          with: Entities::IssueBasic,
          current_user: current_user,
          project: user_project,
          issuable_metadata: issuable_meta_data(issues, 'Issue')
        }
154

155
        present issues, options
N
Nihad Abbasov 已提交
156 157
      end

R
Robert Schilling 已提交
158 159 160 161
      desc 'Get a single project issue' do
        success Entities::Issue
      end
      params do
162
        requires :issue_iid, type: Integer, desc: 'The internal ID of a project issue'
R
Robert Schilling 已提交
163
      end
164
      get ":id/issues/:issue_iid", as: :api_v4_project_issue do
165
        issue = find_project_issue(params[:issue_iid])
R
Robert Schilling 已提交
166
        present issue, with: Entities::Issue, current_user: current_user, project: user_project
N
Nihad Abbasov 已提交
167 168
      end

R
Robert Schilling 已提交
169 170 171 172 173 174 175
      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.'
B
Bob Van Landuyt 已提交
176
        optional :merge_request_to_resolve_discussions_of, type: Integer,
R
Robert Schilling 已提交
177
                                                           desc: 'The IID of a merge request for which to resolve discussions'
178
        optional :discussion_to_resolve, type: String,
B
Bob Van Landuyt 已提交
179
                                         desc: 'The ID of a discussion to resolve, also pass `merge_request_to_resolve_discussions_of`'
180 181 182
        optional :iid, type: Integer,
                       desc: 'The internal ID of a project issue. Available only for admins and project owners.'

R
Robert Schilling 已提交
183 184
        use :issue_params
      end
185
      post ':id/issues' do
186 187
        Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42320')

188 189
        authorize! :create_issue, user_project

190 191
        params.delete(:created_at) unless current_user.can?(:set_issue_created_at, user_project)
        params.delete(:iid) unless current_user.can?(:set_issue_iid, user_project)
192

R
Robert Schilling 已提交
193
        issue_params = declared_params(include_missing: false)
194

195 196
        issue_params = convert_parameters_from_legacy_format(issue_params)

R
Robert Schilling 已提交
197 198 199
        issue = ::Issues::CreateService.new(user_project,
                                            current_user,
                                            issue_params.merge(request: request, api: true)).execute
200

201
        if issue.spam?
202 203
          render_api_error!({ error: 'Spam detected' }, 400)
        end
204

205
        if issue.valid?
206
          present issue, with: Entities::Issue, current_user: current_user, project: user_project
207
        else
J
jubianchi 已提交
208
          render_validation_error!(issue)
N
Nihad Abbasov 已提交
209 210 211
        end
      end

212 213 214 215
      desc 'Update an existing issue' do
        success Entities::Issue
      end
      params do
216
        requires :issue_iid, type: Integer, desc: 'The internal ID of a project issue'
R
Robert Schilling 已提交
217 218 219
        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.'
220
        optional :state_event, type: String, values: %w[reopen close], desc: 'State of the issue'
R
Robert Schilling 已提交
221
        use :issue_params
222
        at_least_one_of :title, :description, :assignee_ids, :assignee_id, :milestone_id, :discussion_locked,
R
Robert Schilling 已提交
223
                        :labels, :created_at, :due_date, :confidential, :state_event
224
      end
225
      # rubocop: disable CodeReuse/ActiveRecord
226
      put ':id/issues/:issue_iid' do
227 228
        Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42322')

229
        issue = user_project.issues.find_by!(iid: params.delete(:issue_iid))
230
        authorize! :update_issue, issue
231

232 233
        # Setting created_at time only allowed for admins and project/group owners
        unless current_user.admin? || user_project.owner == current_user || current_user.owned_groups.include?(user_project.owner)
R
Robert Schilling 已提交
234 235
          params.delete(:updated_at)
        end
236

237 238
        update_params = declared_params(include_missing: false).merge(request: request, api: true)

239 240
        update_params = convert_parameters_from_legacy_format(update_params)

R
Robert Schilling 已提交
241 242
        issue = ::Issues::UpdateService.new(user_project,
                                            current_user,
243 244 245
                                            update_params).execute(issue)

        render_spam_error! if issue.spam?
246

247
        if issue.valid?
248
          present issue, with: Entities::Issue, current_user: current_user, project: user_project
249
        else
J
jubianchi 已提交
250
          render_validation_error!(issue)
N
Nihad Abbasov 已提交
251 252
        end
      end
253
      # rubocop: enable CodeReuse/ActiveRecord
N
Nihad Abbasov 已提交
254

R
Robert Schilling 已提交
255 256 257 258
      desc 'Move an existing issue' do
        success Entities::Issue
      end
      params do
259
        requires :issue_iid, type: Integer, desc: 'The internal ID of a project issue'
R
Robert Schilling 已提交
260 261
        requires :to_project_id, type: Integer, desc: 'The ID of the new project'
      end
262
      # rubocop: disable CodeReuse/ActiveRecord
263
      post ':id/issues/:issue_iid/move' do
264 265
        Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42323')

266
        issue = user_project.issues.find_by(iid: params[:issue_iid])
R
Robert Schilling 已提交
267
        not_found!('Issue') unless issue
R
Robert Schilling 已提交
268

R
Robert Schilling 已提交
269 270
        new_project = Project.find_by(id: params[:to_project_id])
        not_found!('Project') unless new_project
R
Robert Schilling 已提交
271 272 273

        begin
          issue = ::Issues::MoveService.new(user_project, current_user).execute(issue, new_project)
274
          present issue, with: Entities::Issue, current_user: current_user, project: user_project
R
Robert Schilling 已提交
275 276 277 278
        rescue ::Issues::MoveService::MoveError => error
          render_api_error!(error.message, 400)
        end
      end
279
      # rubocop: enable CodeReuse/ActiveRecord
R
Robert Schilling 已提交
280

R
Robert Schilling 已提交
281 282
      desc 'Delete a project issue'
      params do
283
        requires :issue_iid, type: Integer, desc: 'The internal ID of a project issue'
R
Robert Schilling 已提交
284
      end
285
      # rubocop: disable CodeReuse/ActiveRecord
286 287
      delete ":id/issues/:issue_iid" do
        issue = user_project.issues.find_by(iid: params[:issue_iid])
R
Robert Schilling 已提交
288
        not_found!('Issue') unless issue
Z
Zeger-Jan van de Weg 已提交
289

290
        authorize!(:destroy_issue, issue)
291

292 293 294
        destroy_conditionally!(issue) do |issue|
          Issuable::DestroyService.new(user_project, current_user).execute(issue)
        end
N
Nihad Abbasov 已提交
295
      end
296
      # rubocop: enable CodeReuse/ActiveRecord
297

298
      desc 'List merge requests that are related to the issue' do
299 300 301 302 303 304 305 306
        success Entities::MergeRequestBasic
      end
      params do
        requires :issue_iid, type: Integer, desc: 'The internal ID of a project issue'
      end
      get ':id/issues/:issue_iid/related_merge_requests' do
        issue = find_project_issue(params[:issue_iid])

307
        merge_requests = ::Issues::ReferencedMergeRequestsService.new(user_project, current_user)
308 309 310
          .execute(issue)
          .flatten

311 312 313 314
        present paginate(::Kaminari.paginate_array(merge_requests)),
          with: Entities::MergeRequestBasic,
          current_user: current_user,
          project: user_project
315 316
      end

317
      desc 'List merge requests closing issue' do
318 319 320 321 322
        success Entities::MergeRequestBasic
      end
      params do
        requires :issue_iid, type: Integer, desc: 'The internal ID of a project issue'
      end
323
      # rubocop: disable CodeReuse/ActiveRecord
324 325 326 327 328 329 330 331
      get ':id/issues/:issue_iid/closed_by' do
        issue = find_project_issue(params[:issue_iid])

        merge_request_ids = MergeRequestsClosingIssues.where(issue_id: issue).select(:merge_request_id)
        merge_requests = MergeRequestsFinder.new(current_user, project_id: user_project.id).execute.where(id: merge_request_ids)

        present paginate(merge_requests), with: Entities::MergeRequestBasic, current_user: current_user, project: user_project
      end
332
      # rubocop: enable CodeReuse/ActiveRecord
333

334
      desc 'List participants for an issue' do
335 336 337 338 339 340 341 342 343 344 345 346
        success Entities::UserBasic
      end
      params do
        requires :issue_iid, type: Integer, desc: 'The internal ID of a project issue'
      end
      get ':id/issues/:issue_iid/participants' do
        issue = find_project_issue(params[:issue_iid])
        participants = ::Kaminari.paginate_array(issue.participants)

        present paginate(participants), with: Entities::UserBasic, current_user: current_user, project: user_project
      end

347 348 349 350 351 352 353 354 355 356 357
      desc 'Get the user agent details for an issue' do
        success Entities::UserAgentDetail
      end
      params do
        requires :issue_iid, type: Integer, desc: 'The internal ID of a project issue'
      end
      get ":id/issues/:issue_iid/user_agent_detail" do
        authenticated_as_admin!

        issue = find_project_issue(params[:issue_iid])

358
        break not_found!('UserAgentDetail') unless issue.user_agent_detail
359

J
James Lopez 已提交
360
        present issue.user_agent_detail, with: Entities::UserAgentDetail
361
      end
N
Nihad Abbasov 已提交
362 363 364
    end
  end
end