issues.rb 8.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 35 36 37 38 39
        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'
40
      end
41 42 43 44

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

174 175
        update_params = declared_params(include_missing: false).merge(request: request, api: true)

R
Robert Schilling 已提交
176 177
        issue = ::Issues::UpdateService.new(user_project,
                                            current_user,
178 179 180
                                            update_params).execute(issue)

        render_spam_error! if issue.spam?
181

182
        if issue.valid?
183
          present issue, with: Entities::Issue, current_user: current_user, project: user_project
184
        else
J
jubianchi 已提交
185
          render_validation_error!(issue)
N
Nihad Abbasov 已提交
186 187 188
        end
      end

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

R
Robert Schilling 已提交
200 201
        new_project = Project.find_by(id: params[:to_project_id])
        not_found!('Project') unless new_project
R
Robert Schilling 已提交
202 203 204

        begin
          issue = ::Issues::MoveService.new(user_project, current_user).execute(issue, new_project)
205
          present issue, with: Entities::Issue, current_user: current_user, project: user_project
R
Robert Schilling 已提交
206 207 208 209 210
        rescue ::Issues::MoveService::MoveError => error
          render_api_error!(error.message, 400)
        end
      end

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

219
        authorize!(:destroy_issue, issue)
Z
Zeger-Jan van de Weg 已提交
220
        issue.destroy
N
Nihad Abbasov 已提交
221 222 223 224
      end
    end
  end
end