issues.rb 9.5 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 11 12
      def find_issues(args = {})
        args = params.merge(args)

        args.delete(:id)
        args[:milestone_title] = args.delete(:milestone)
13
        args[:label_name] = args.delete(:labels)
14

15
        issues = IssuesFinder.new(current_user, args).execute
16 17

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

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

33
      params :issue_params_ce do
R
Robert Schilling 已提交
34
        optional :description, type: String, desc: 'The description of an issue'
35 36
        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 已提交
37 38
        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'
39
        optional :due_date, type: String, desc: 'Date string in the format YEAR-MONTH-DAY'
R
Robert Schilling 已提交
40
        optional :confidential, type: Boolean, desc: 'Boolean parameter if the issue should be confidential'
41
      end
42 43 44 45

      params :issue_params do
        use :issue_params_ce
      end
J
jubianchi 已提交
46 47
    end

N
Nihad Abbasov 已提交
48
    resource :issues do
R
Robert Schilling 已提交
49
      desc "Get currently authenticated user's issues" do
50
        success Entities::IssueBasic
R
Robert Schilling 已提交
51 52 53 54 55 56
      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 已提交
57
      get do
58
        issues = find_issues(scope: 'authored')
S
Sean McGivern 已提交
59

60
        present paginate(issues), with: Entities::IssueBasic, current_user: current_user
N
Nihad Abbasov 已提交
61 62 63
      end
    end

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

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

81
        present paginate(issues), with: Entities::IssueBasic, current_user: current_user
82 83 84
      end
    end

85 86 87
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
88
    resource :projects, requirements: { id: %r{[^/]+} } do
89 90
      include TimeTrackingEndpoints

R
Robert Schilling 已提交
91
      desc 'Get a list of project issues' do
92
        success Entities::IssueBasic
R
Robert Schilling 已提交
93 94 95 96 97 98
      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 已提交
99
      get ":id/issues" do
100
        project = find_project!(params[:id])
101

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

104
        present paginate(issues), with: Entities::IssueBasic, current_user: current_user, project: user_project
N
Nihad Abbasov 已提交
105 106
      end

R
Robert Schilling 已提交
107 108 109 110
      desc 'Get a single project issue' do
        success Entities::Issue
      end
      params do
111
        requires :issue_iid, type: Integer, desc: 'The internal ID of a project issue'
R
Robert Schilling 已提交
112
      end
113 114
      get ":id/issues/:issue_iid" do
        issue = find_project_issue(params[:issue_iid])
R
Robert Schilling 已提交
115
        present issue, with: Entities::Issue, current_user: current_user, project: user_project
N
Nihad Abbasov 已提交
116 117
      end

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

R
Robert Schilling 已提交
137
        issue_params = declared_params(include_missing: false)
138

139 140
        issue_params = convert_parameters_from_legacy_format(issue_params)

R
Robert Schilling 已提交
141 142 143
        issue = ::Issues::CreateService.new(user_project,
                                            current_user,
                                            issue_params.merge(request: request, api: true)).execute
144
        if issue.spam?
145 146
          render_api_error!({ error: 'Spam detected' }, 400)
        end
147

148
        if issue.valid?
149
          present issue, with: Entities::Issue, current_user: current_user, project: user_project
150
        else
J
jubianchi 已提交
151
          render_validation_error!(issue)
N
Nihad Abbasov 已提交
152 153 154
        end
      end

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

R
Robert Schilling 已提交
172 173 174 175
        # 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
176

177 178
        update_params = declared_params(include_missing: false).merge(request: request, api: true)

179 180
        update_params = convert_parameters_from_legacy_format(update_params)

R
Robert Schilling 已提交
181 182
        issue = ::Issues::UpdateService.new(user_project,
                                            current_user,
183 184 185
                                            update_params).execute(issue)

        render_spam_error! if issue.spam?
186

187
        if issue.valid?
188
          present issue, with: Entities::Issue, current_user: current_user, project: user_project
189
        else
J
jubianchi 已提交
190
          render_validation_error!(issue)
N
Nihad Abbasov 已提交
191 192 193
        end
      end

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

R
Robert Schilling 已提交
205 206
        new_project = Project.find_by(id: params[:to_project_id])
        not_found!('Project') unless new_project
R
Robert Schilling 已提交
207 208 209

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

R
Robert Schilling 已提交
216 217
      desc 'Delete a project issue'
      params do
218
        requires :issue_iid, type: Integer, desc: 'The internal ID of a project issue'
R
Robert Schilling 已提交
219
      end
220 221
      delete ":id/issues/:issue_iid" do
        issue = user_project.issues.find_by(iid: params[:issue_iid])
R
Robert Schilling 已提交
222
        not_found!('Issue') unless issue
Z
Zeger-Jan van de Weg 已提交
223

224
        authorize!(:destroy_issue, issue)
Z
Zeger-Jan van de Weg 已提交
225
        issue.destroy
N
Nihad Abbasov 已提交
226
      end
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

      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
N
Nihad Abbasov 已提交
242 243 244
    end
  end
end