issues.rb 10.6 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
8 9 10
      def find_issues(args = {})
        args = params.merge(args)

11 12 13
        # Do not scope to "authored" when author or assignee id is given
        args.delete(:scope) if args[:author_id] || args[:assignee_id]

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

18
        issues = IssuesFinder.new(current_user, args).execute
19 20

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

R
Robert Schilling 已提交
23 24
      params :issues_params do
        optional :labels, type: String, desc: 'Comma-separated list of label names'
25
        optional :milestone, type: String, desc: 'Milestone title'
R
Robert Schilling 已提交
26 27 28 29
        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.'
30
        optional :milestone, type: String, desc: 'Return issues for a specific milestone'
31
        optional :iids, type: Array[Integer], desc: 'The IID array of issues'
32
        optional :search, type: String, desc: 'Search issues for text present in the title or description'
33 34
        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'
35 36
        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'
R
Robert Schilling 已提交
37 38
        use :pagination
      end
39

40
      params :issue_params_ce do
R
Robert Schilling 已提交
41
        optional :description, type: String, desc: 'The description of an issue'
42 43
        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 已提交
44 45
        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'
46
        optional :due_date, type: String, desc: 'Date string in the format YEAR-MONTH-DAY'
R
Robert Schilling 已提交
47
        optional :confidential, type: Boolean, desc: 'Boolean parameter if the issue should be confidential'
48
      end
49 50 51 52

      params :issue_params do
        use :issue_params_ce
      end
J
jubianchi 已提交
53 54
    end

N
Nihad Abbasov 已提交
55
    resource :issues do
R
Robert Schilling 已提交
56
      desc "Get currently authenticated user's issues" do
57
        success Entities::IssueBasic
R
Robert Schilling 已提交
58 59 60 61 62 63
      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 已提交
64
      get do
65
        issues = find_issues(scope: 'authored')
S
Sean McGivern 已提交
66

67
        present paginate(issues), with: Entities::IssueBasic, current_user: current_user
N
Nihad Abbasov 已提交
68 69 70
      end
    end

R
Robert Schilling 已提交
71 72 73
    params do
      requires :id, type: String, desc: 'The ID of a group'
    end
74
    resource :groups, requirements: { id: %r{[^/]+} } do
R
Robert Schilling 已提交
75
      desc 'Get a list of group issues' do
76
        success Entities::IssueBasic
R
Robert Schilling 已提交
77 78
      end
      params do
79
        optional :state, type: String, values: %w[opened closed all], default: 'all',
R
Robert Schilling 已提交
80 81 82
                         desc: 'Return opened, closed, or all issues'
        use :issues_params
      end
83
      get ":id/issues" do
84
        group = find_group!(params[:id])
85

86
        issues = find_issues(group_id: group.id)
S
Sean McGivern 已提交
87

88
        present paginate(issues), with: Entities::IssueBasic, current_user: current_user
89 90 91
      end
    end

92 93 94
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
95
    resource :projects, requirements: { id: %r{[^/]+} } do
96 97
      include TimeTrackingEndpoints

R
Robert Schilling 已提交
98
      desc 'Get a list of project issues' do
99
        success Entities::IssueBasic
R
Robert Schilling 已提交
100 101 102 103 104 105
      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 已提交
106
      get ":id/issues" do
107
        project = find_project!(params[:id])
108

109
        issues = find_issues(project_id: project.id)
R
Robert Schilling 已提交
110

111
        present paginate(issues), with: Entities::IssueBasic, current_user: current_user, project: user_project
N
Nihad Abbasov 已提交
112 113
      end

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

R
Robert Schilling 已提交
125 126 127 128 129 130 131
      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 已提交
132
        optional :merge_request_to_resolve_discussions_of, type: Integer,
R
Robert Schilling 已提交
133
                                                           desc: 'The IID of a merge request for which to resolve discussions'
134
        optional :discussion_to_resolve, type: String,
B
Bob Van Landuyt 已提交
135
                                         desc: 'The ID of a discussion to resolve, also pass `merge_request_to_resolve_discussions_of`'
R
Robert Schilling 已提交
136 137
        use :issue_params
      end
138
      post ':id/issues' do
R
Robert Schilling 已提交
139 140 141 142
        # 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
143

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

146 147
        issue_params = convert_parameters_from_legacy_format(issue_params)

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

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

162 163 164 165
      desc 'Update an existing issue' do
        success Entities::Issue
      end
      params do
166
        requires :issue_iid, type: Integer, desc: 'The internal ID of a project issue'
R
Robert Schilling 已提交
167 168 169
        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.'
170
        optional :state_event, type: String, values: %w[reopen close], desc: 'State of the issue'
R
Robert Schilling 已提交
171
        use :issue_params
172
        at_least_one_of :title, :description, :assignee_ids, :assignee_id, :milestone_id,
R
Robert Schilling 已提交
173
                        :labels, :created_at, :due_date, :confidential, :state_event
174
      end
175 176
      put ':id/issues/:issue_iid' do
        issue = user_project.issues.find_by!(iid: params.delete(:issue_iid))
177
        authorize! :update_issue, issue
178

R
Robert Schilling 已提交
179 180 181 182
        # 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
183

184 185
        update_params = declared_params(include_missing: false).merge(request: request, api: true)

186 187
        update_params = convert_parameters_from_legacy_format(update_params)

R
Robert Schilling 已提交
188 189
        issue = ::Issues::UpdateService.new(user_project,
                                            current_user,
190 191 192
                                            update_params).execute(issue)

        render_spam_error! if issue.spam?
193

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

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

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

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

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

231
        authorize!(:destroy_issue, issue)
D
Dmitriy Zaporozhets 已提交
232
        status 204
Z
Zeger-Jan van de Weg 已提交
233
        issue.destroy
N
Nihad Abbasov 已提交
234
      end
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

      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
250 251 252 253 254 255 256 257 258 259 260 261 262 263

      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])

        return not_found!('UserAgentDetail') unless issue.user_agent_detail

J
James Lopez 已提交
264
        present issue.user_agent_detail, with: Entities::UserAgentDetail
265
      end
N
Nihad Abbasov 已提交
266 267 268
    end
  end
end