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

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

7 8
    helpers ::Gitlab::IssuableMetadata

J
jubianchi 已提交
9
    helpers do
10
      def find_issues(args = {})
11
        args = declared_params.merge(args)
12 13 14

        args.delete(:id)
        args[:milestone_title] = args.delete(:milestone)
15
        args[:label_name] = args.delete(:labels)
16
        args[:scope] = args[:scope].underscore if args[:scope]
17

18
        issues = IssuesFinder.new(current_user, args).execute
19
          .preload(:assignees, :labels, :notes, :timelogs, :project, :author)
20 21

        issues.reorder(args[:order_by] => args[:sort])
J
jubianchi 已提交
22 23
      end

R
Robert Schilling 已提交
24 25
      params :issues_params do
        optional :labels, type: String, desc: 'Comma-separated list of label names'
26
        optional :milestone, type: String, desc: 'Milestone title'
R
Robert Schilling 已提交
27 28 29 30
        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.'
31
        optional :milestone, type: String, desc: 'Return issues for a specific milestone'
32
        optional :iids, type: Array[Integer], desc: 'The IID array of issues'
33
        optional :search, type: String, desc: 'Search issues for text present in the title or description'
34 35
        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'
36 37
        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'
38 39
        optional :author_id, type: Integer, desc: 'Return issues which are authored by the user with the given ID'
        optional :assignee_id, type: Integer, desc: 'Return issues which are assigned to the user with the given ID'
40 41
        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`'
42
        optional :my_reaction_emoji, type: String, desc: 'Return issues reacted by the authenticated user by the given emoji'
R
Robert Schilling 已提交
43 44
        use :pagination
      end
45

46
      params :issue_params_ce do
R
Robert Schilling 已提交
47
        optional :description, type: String, desc: 'The description of an issue'
48 49
        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 已提交
50 51
        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'
52
        optional :due_date, type: String, desc: 'Date string in the format YEAR-MONTH-DAY'
R
Robert Schilling 已提交
53
        optional :confidential, type: Boolean, desc: 'Boolean parameter if the issue should be confidential'
54
        optional :discussion_locked, type: Boolean, desc: " Boolean parameter indicating if the issue's discussion is locked"
55
      end
56 57 58 59

      params :issue_params do
        use :issue_params_ce
      end
J
jubianchi 已提交
60 61
    end

N
Nihad Abbasov 已提交
62
    resource :issues do
R
Robert Schilling 已提交
63
      desc "Get currently authenticated user's issues" do
64
        success Entities::IssueBasic
R
Robert Schilling 已提交
65 66 67 68 69
      end
      params do
        optional :state, type: String, values: %w[opened closed all], default: 'all',
                         desc: 'Return opened, closed, or all issues'
        use :issues_params
70 71
        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 已提交
72
      end
N
Nihad Abbasov 已提交
73
      get do
74
        authenticate! unless params[:scope] == 'all'
75
        issues = paginate(find_issues)
S
Sean McGivern 已提交
76

77 78 79 80 81
        options = {
          with: Entities::IssueBasic,
          current_user: current_user,
          issuable_metadata: issuable_meta_data(issues, 'Issue')
        }
82

83
        present issues, options
N
Nihad Abbasov 已提交
84 85 86
      end
    end

R
Robert Schilling 已提交
87 88 89
    params do
      requires :id, type: String, desc: 'The ID of a group'
    end
90
    resource :groups, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
R
Robert Schilling 已提交
91
      desc 'Get a list of group issues' do
92
        success Entities::IssueBasic
R
Robert Schilling 已提交
93 94
      end
      params do
95
        optional :state, type: String, values: %w[opened closed all], default: 'all',
R
Robert Schilling 已提交
96 97 98
                         desc: 'Return opened, closed, or all issues'
        use :issues_params
      end
99
      get ":id/issues" do
100
        group = find_group!(params[:id])
101

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

104 105 106 107 108
        options = {
          with: Entities::IssueBasic,
          current_user: current_user,
          issuable_metadata: issuable_meta_data(issues, 'Issue')
        }
109

110
        present issues, options
111 112 113
      end
    end

114 115 116
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
117
    resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
118 119
      include TimeTrackingEndpoints

R
Robert Schilling 已提交
120
      desc 'Get a list of project issues' do
121
        success Entities::IssueBasic
R
Robert Schilling 已提交
122 123 124 125 126 127
      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 已提交
128
      get ":id/issues" do
129
        project = find_project!(params[:id])
130

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

133 134 135 136 137 138
        options = {
          with: Entities::IssueBasic,
          current_user: current_user,
          project: user_project,
          issuable_metadata: issuable_meta_data(issues, 'Issue')
        }
139

140
        present issues, options
N
Nihad Abbasov 已提交
141 142
      end

R
Robert Schilling 已提交
143 144 145 146
      desc 'Get a single project issue' do
        success Entities::Issue
      end
      params do
147
        requires :issue_iid, type: Integer, desc: 'The internal ID of a project issue'
R
Robert Schilling 已提交
148
      end
149
      get ":id/issues/:issue_iid", as: :api_v4_project_issue do
150
        issue = find_project_issue(params[:issue_iid])
R
Robert Schilling 已提交
151
        present issue, with: Entities::Issue, current_user: current_user, project: user_project
N
Nihad Abbasov 已提交
152 153
      end

R
Robert Schilling 已提交
154 155 156 157 158 159 160
      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 已提交
161
        optional :merge_request_to_resolve_discussions_of, type: Integer,
R
Robert Schilling 已提交
162
                                                           desc: 'The IID of a merge request for which to resolve discussions'
163
        optional :discussion_to_resolve, type: String,
B
Bob Van Landuyt 已提交
164
                                         desc: 'The ID of a discussion to resolve, also pass `merge_request_to_resolve_discussions_of`'
165 166 167
        optional :iid, type: Integer,
                       desc: 'The internal ID of a project issue. Available only for admins and project owners.'

R
Robert Schilling 已提交
168 169
        use :issue_params
      end
170
      post ':id/issues' do
171 172
        Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42320')

173 174
        authorize! :create_issue, user_project

175
        # Setting created_at time or iid only allowed for admins and project owners
176
        unless current_user.admin? || user_project.owner == current_user || current_user.owned_groups.include?(user_project.owner)
R
Robert Schilling 已提交
177
          params.delete(:created_at)
178
          params.delete(:iid)
R
Robert Schilling 已提交
179
        end
180

R
Robert Schilling 已提交
181
        issue_params = declared_params(include_missing: false)
182

183 184
        issue_params = convert_parameters_from_legacy_format(issue_params)

R
Robert Schilling 已提交
185 186 187
        issue = ::Issues::CreateService.new(user_project,
                                            current_user,
                                            issue_params.merge(request: request, api: true)).execute
188

189
        if issue.spam?
190 191
          render_api_error!({ error: 'Spam detected' }, 400)
        end
192

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

200 201 202 203
      desc 'Update an existing issue' do
        success Entities::Issue
      end
      params do
204
        requires :issue_iid, type: Integer, desc: 'The internal ID of a project issue'
R
Robert Schilling 已提交
205 206 207
        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.'
208
        optional :state_event, type: String, values: %w[reopen close], desc: 'State of the issue'
R
Robert Schilling 已提交
209
        use :issue_params
210
        at_least_one_of :title, :description, :assignee_ids, :assignee_id, :milestone_id, :discussion_locked,
R
Robert Schilling 已提交
211
                        :labels, :created_at, :due_date, :confidential, :state_event
212
      end
213
      put ':id/issues/:issue_iid' do
214 215
        Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42322')

216
        issue = user_project.issues.find_by!(iid: params.delete(:issue_iid))
217
        authorize! :update_issue, issue
218

219 220
        # 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 已提交
221 222
          params.delete(:updated_at)
        end
223

224 225
        update_params = declared_params(include_missing: false).merge(request: request, api: true)

226 227
        update_params = convert_parameters_from_legacy_format(update_params)

R
Robert Schilling 已提交
228 229
        issue = ::Issues::UpdateService.new(user_project,
                                            current_user,
230 231 232
                                            update_params).execute(issue)

        render_spam_error! if issue.spam?
233

234
        if issue.valid?
235
          present issue, with: Entities::Issue, current_user: current_user, project: user_project
236
        else
J
jubianchi 已提交
237
          render_validation_error!(issue)
N
Nihad Abbasov 已提交
238 239 240
        end
      end

R
Robert Schilling 已提交
241 242 243 244
      desc 'Move an existing issue' do
        success Entities::Issue
      end
      params do
245
        requires :issue_iid, type: Integer, desc: 'The internal ID of a project issue'
R
Robert Schilling 已提交
246 247
        requires :to_project_id, type: Integer, desc: 'The ID of the new project'
      end
248
      post ':id/issues/:issue_iid/move' do
249 250
        Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42323')

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

R
Robert Schilling 已提交
254 255
        new_project = Project.find_by(id: params[:to_project_id])
        not_found!('Project') unless new_project
R
Robert Schilling 已提交
256 257 258

        begin
          issue = ::Issues::MoveService.new(user_project, current_user).execute(issue, new_project)
259
          present issue, with: Entities::Issue, current_user: current_user, project: user_project
R
Robert Schilling 已提交
260 261 262 263 264
        rescue ::Issues::MoveService::MoveError => error
          render_api_error!(error.message, 400)
        end
      end

R
Robert Schilling 已提交
265 266
      desc 'Delete a project issue'
      params do
267
        requires :issue_iid, type: Integer, desc: 'The internal ID of a project issue'
R
Robert Schilling 已提交
268
      end
269 270
      delete ":id/issues/:issue_iid" do
        issue = user_project.issues.find_by(iid: params[:issue_iid])
R
Robert Schilling 已提交
271
        not_found!('Issue') unless issue
Z
Zeger-Jan van de Weg 已提交
272

273
        authorize!(:destroy_issue, issue)
274

275 276 277
        destroy_conditionally!(issue) do |issue|
          Issuable::DestroyService.new(user_project, current_user).execute(issue)
        end
N
Nihad Abbasov 已提交
278
      end
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

      desc 'List merge requests closing issue'  do
        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/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
294

295 296 297 298 299 300 301 302 303 304 305 306 307
      desc 'List participants for an issue'  do
        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

308 309 310 311 312 313 314 315 316 317 318
      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])

319
        break not_found!('UserAgentDetail') unless issue.user_agent_detail
320

J
James Lopez 已提交
321
        present issue.user_agent_detail, with: Entities::UserAgentDetail
322
      end
N
Nihad Abbasov 已提交
323 324 325
    end
  end
end