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 13 14 15 16 17 18 19 20 21 22 23 24 25
      def find_issues(args = {})
        args = params.merge(args)

        args.delete(:id)
        args[:milestone_title] = args.delete(:milestone)

        match_all_labels = args.delete(:match_all_labels)
        labels = args.delete(:labels)
        args[:label_name] = labels if match_all_labels

        issues = IssuesFinder.new(current_user, args).execute.inc_notes_with_associations

        # TODO: Remove in 9.0  pass `label_name: args.delete(:labels)` to IssuesFinder
        if !match_all_labels && labels.present?
          issues = issues.includes(:labels).where('labels.title' => labels.split(','))
        end

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

R
Robert Schilling 已提交
28 29
      params :issues_params do
        optional :labels, type: String, desc: 'Comma-separated list of label names'
30
        optional :milestone, type: String, desc: 'Milestone title'
R
Robert Schilling 已提交
31 32 33 34
        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.'
35
        optional :milestone, type: String, desc: 'Return issues for a specific milestone'
R
Robert Schilling 已提交
36 37
        use :pagination
      end
38

R
Robert Schilling 已提交
39 40 41 42 43 44 45
      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'
46
      end
J
jubianchi 已提交
47 48
    end

N
Nihad Abbasov 已提交
49
    resource :issues do
R
Robert Schilling 已提交
50 51 52 53 54 55 56 57
      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 已提交
58
      get do
59
        issues = find_issues(scope: 'authored')
S
Sean McGivern 已提交
60

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

R
Robert Schilling 已提交
65 66 67
    params do
      requires :id, type: String, desc: 'The ID of a group'
    end
68
    resource :groups do
R
Robert Schilling 已提交
69 70 71 72 73 74 75 76
      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
77
      get ":id/issues" do
78
        group = find_group!(params[:id])
79

80
        issues = find_issues(group_id: group.id, state: params[:state] || 'opened', match_all_labels: true)
S
Sean McGivern 已提交
81

82 83 84 85
        present paginate(issues), with: Entities::Issue, current_user: current_user
      end
    end

86 87 88
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
N
Nihad Abbasov 已提交
89
    resource :projects do
90 91
      include TimeTrackingEndpoints

R
Robert Schilling 已提交
92 93 94 95 96 97 98 99
      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'
        use :issues_params
      end
N
Nihad Abbasov 已提交
100
      get ":id/issues" do
101 102
        project = find_project(params[:id])

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

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

R
Robert Schilling 已提交
108 109 110 111 112 113
      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 已提交
114
      get ":id/issues/:issue_id" do
R
Robert Schilling 已提交
115 116
        issue = find_project_issue(params[:issue_id])
        present issue, with: Entities::Issue, current_user: current_user, project: user_project
N
Nihad Abbasov 已提交
117 118
      end

R
Robert Schilling 已提交
119 120 121 122 123 124 125 126 127 128 129
      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
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 138

        if merge_request_iid = params[:merge_request_for_resolving_discussions]
R
Robert Schilling 已提交
139
          issue_params[:merge_request_for_resolving_discussions] = MergeRequestsFinder.new(current_user, project_id: user_project.id).
140 141 142
            execute.
            find_by(iid: merge_request_iid)
        end
143

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

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

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

R
Robert Schilling 已提交
175 176 177 178
        # 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
179

R
Robert Schilling 已提交
180 181 182
        issue = ::Issues::UpdateService.new(user_project,
                                            current_user,
                                            declared_params(include_missing: false)).execute(issue)
183

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

R
Robert Schilling 已提交
191 192 193 194 195 196 197
      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
198
      post ':id/issues/:issue_id/move' do
R
Robert Schilling 已提交
199 200
        issue = user_project.issues.find_by(id: params[:issue_id])
        not_found!('Issue') unless issue
R
Robert Schilling 已提交
201

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

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

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

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