issues.rb 8.7 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
R
Robert Schilling 已提交
8
      # TODO: Remove in 9.0 and switch to IssueFinder-based label filtering
J
jubianchi 已提交
9
      def filter_issues_labels(issues, labels)
J
jubianchi 已提交
10 11 12
        issues.includes(:labels).where('labels.title' => labels.split(','))
      end

R
Robert Schilling 已提交
13 14 15 16 17 18 19 20
      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
21

R
Robert Schilling 已提交
22 23 24 25 26 27 28
      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'
29
      end
J
jubianchi 已提交
30 31
    end

N
Nihad Abbasov 已提交
32
    resource :issues do
R
Robert Schilling 已提交
33 34 35 36 37 38 39 40
      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 已提交
41
      get do
R
Robert Schilling 已提交
42
        issues = IssuesFinder.new(current_user, scope: 'all', author_id: current_user.id, state: params[:state]).execute.inc_notes_with_associations
J
jubianchi 已提交
43
        issues = filter_issues_labels(issues, params[:labels]) unless params[:labels].nil?
R
Robert Schilling 已提交
44
        issues = issues.reorder(params[:order_by] => params[:sort])
S
Sean McGivern 已提交
45

46
        present paginate(issues), with: Entities::Issue, current_user: current_user
N
Nihad Abbasov 已提交
47 48 49
      end
    end

R
Robert Schilling 已提交
50 51 52
    params do
      requires :id, type: String, desc: 'The ID of a group'
    end
53
    resource :groups do
R
Robert Schilling 已提交
54 55 56 57 58 59 60 61
      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
62
      get ":id/issues" do
R
Robert Schilling 已提交
63
        group = find_group!(params.delete(:id))
64 65 66 67

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

69 70
        issues = IssuesFinder.new(current_user, params).execute

R
Robert Schilling 已提交
71
        issues = issues.reorder(params[:order_by] => params[:sort])
72 73 74 75
        present paginate(issues), with: Entities::Issue, current_user: current_user
      end
    end

76 77 78
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
N
Nihad Abbasov 已提交
79
    resource :projects do
R
Robert Schilling 已提交
80 81 82 83 84 85 86 87 88
      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 已提交
89
      get ":id/issues" do
R
Robert Schilling 已提交
90 91 92 93
        issues = IssuesFinder.new(current_user,
                                  project_id: user_project.id,
                                  state: params[:state],
                                  milestone_title: params[:milestone]).execute.inc_notes_with_associations
J
jubianchi 已提交
94
        issues = filter_issues_labels(issues, params[:labels]) unless params[:labels].nil?
95
        issues = filter_by_iid(issues, params[:iid]) unless params[:iid].nil?
R
Robert Schilling 已提交
96
        issues = issues.reorder(params[:order_by] => params[:sort])
R
Robert Schilling 已提交
97

98
        present paginate(issues), with: Entities::Issue, current_user: current_user, project: user_project
N
Nihad Abbasov 已提交
99 100
      end

R
Robert Schilling 已提交
101 102 103 104 105 106
      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 已提交
107
      get ":id/issues/:issue_id" do
R
Robert Schilling 已提交
108 109
        issue = find_project_issue(params[:issue_id])
        present issue, with: Entities::Issue, current_user: current_user, project: user_project
N
Nihad Abbasov 已提交
110 111
      end

R
Robert Schilling 已提交
112 113 114 115 116 117 118 119 120 121 122
      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
123
      post ':id/issues' do
R
Robert Schilling 已提交
124 125 126 127
        # 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
128

R
Robert Schilling 已提交
129
        issue_params = declared_params(include_missing: false)
130 131

        if merge_request_iid = params[:merge_request_for_resolving_discussions]
R
Robert Schilling 已提交
132
          issue_params[:merge_request_for_resolving_discussions] = MergeRequestsFinder.new(current_user, project_id: user_project.id).
133 134 135
            execute.
            find_by(iid: merge_request_iid)
        end
136

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

144
        if issue.valid?
145
          present issue, with: Entities::Issue, current_user: current_user, project: user_project
146
        else
J
jubianchi 已提交
147
          render_validation_error!(issue)
N
Nihad Abbasov 已提交
148 149 150
        end
      end

151 152 153 154
      desc 'Update an existing issue' do
        success Entities::Issue
      end
      params do
R
Robert Schilling 已提交
155 156 157 158
        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.'
159
        optional :state_event, type: String, values: %w[reopen close], desc: 'State of the issue'
R
Robert Schilling 已提交
160 161 162
        use :issue_params
        at_least_one_of :title, :description, :assignee_id, :milestone_id,
                        :labels, :created_at, :due_date, :confidential, :state_event
163
      end
164
      put ':id/issues/:issue_id' do
R
Robert Schilling 已提交
165
        issue = user_project.issues.find(params.delete(:issue_id))
166
        authorize! :update_issue, issue
167

R
Robert Schilling 已提交
168 169 170 171
        # 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
172

R
Robert Schilling 已提交
173 174 175
        issue = ::Issues::UpdateService.new(user_project,
                                            current_user,
                                            declared_params(include_missing: false)).execute(issue)
176

177
        if issue.valid?
178
          present issue, with: Entities::Issue, current_user: current_user, project: user_project
179
        else
J
jubianchi 已提交
180
          render_validation_error!(issue)
N
Nihad Abbasov 已提交
181 182 183
        end
      end

R
Robert Schilling 已提交
184 185 186 187 188 189 190
      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
191
      post ':id/issues/:issue_id/move' do
R
Robert Schilling 已提交
192 193
        issue = user_project.issues.find_by(id: params[:issue_id])
        not_found!('Issue') unless issue
R
Robert Schilling 已提交
194

R
Robert Schilling 已提交
195 196
        new_project = Project.find_by(id: params[:to_project_id])
        not_found!('Project') unless new_project
R
Robert Schilling 已提交
197 198 199

        begin
          issue = ::Issues::MoveService.new(user_project, current_user).execute(issue, new_project)
200
          present issue, with: Entities::Issue, current_user: current_user, project: user_project
R
Robert Schilling 已提交
201 202 203 204 205
        rescue ::Issues::MoveService::MoveError => error
          render_api_error!(error.message, 400)
        end
      end

R
Robert Schilling 已提交
206 207 208 209
      desc 'Delete a project issue'
      params do
        requires :issue_id, type: Integer, desc: 'The ID of a project issue'
      end
N
Nihad Abbasov 已提交
210
      delete ":id/issues/:issue_id" do
211
        issue = user_project.issues.find_by(id: params[:issue_id])
R
Robert Schilling 已提交
212
        not_found!('Issue') unless issue
Z
Zeger-Jan van de Weg 已提交
213

214
        authorize!(:destroy_issue, issue)
Z
Zeger-Jan van de Weg 已提交
215
        issue.destroy
N
Nihad Abbasov 已提交
216 217 218 219
      end
    end
  end
end