issues_controller.rb 9.9 KB
Newer Older
1 2
# frozen_string_literal: true

3
class Projects::IssuesController < Projects::ApplicationController
D
Douwe Maan 已提交
4
  include RendersNotes
5
  include ToggleSubscriptionAction
6
  include IssuableActions
7
  include ToggleAwardEmoji
8
  include IssuableCollections
9
  include IssuesCalendar
10
  include SpammableActions
11
  include RecordUserLastActivity
12

13
  def issue_except_actions
14
    %i[index calendar new create bulk_update import_csv export_csv]
15 16
  end

17
  def set_issuables_index_only_actions
18 19 20
    %i[index calendar]
  end

21 22
  prepend_before_action(only: [:index]) { authenticate_sessionless_user!(:rss) }
  prepend_before_action(only: [:calendar]) { authenticate_sessionless_user!(:ics) }
23
  prepend_before_action :authenticate_user!, only: [:new, :export_csv]
24 25
  # designs is only applicable to EE, but defining a prepend_before_action in EE code would overwrite this
  prepend_before_action :store_uri, only: [:new, :show, :designs]
26

27
  before_action :whitelist_query_limiting, only: [:create, :create_merge_request, :move, :bulk_update]
28
  before_action :check_issues_available!
29
  before_action :issue, unless: ->(c) { c.issue_except_actions.include?(c.action_name.to_sym) }
30

31
  before_action :set_issuables_index, if: ->(c) { c.set_issuables_index_only_actions.include?(c.action_name.to_sym) }
D
Dmitriy Zaporozhets 已提交
32 33

  # Allow write(create) issue
D
Dmitriy Zaporozhets 已提交
34
  before_action :authorize_create_issue!, only: [:new, :create]
D
Dmitriy Zaporozhets 已提交
35 36

  # Allow modify issue
37
  before_action :authorize_update_issuable!, only: [:edit, :update, :move, :reorder]
D
Dmitriy Zaporozhets 已提交
38

39
  # Allow create a new branch and empty WIP merge request from current issue
40
  before_action :authorize_create_merge_request_from!, only: [:create_merge_request]
41

42
  before_action :authorize_import_issues!, only: [:import_csv]
43
  before_action :authorize_download_code!, only: [:related_branches]
44

45 46 47
  # Limit the amount of issues created per minute
  before_action :create_rate_limit, only: [:create]

48 49
  before_action do
    push_frontend_feature_flag(:vue_issuable_sidebar, project.group)
50
    push_frontend_feature_flag(:save_issuable_health_status, project.group, default_enabled: true)
51 52
  end

53 54
  before_action only: :show do
    push_frontend_feature_flag(:real_time_issue_sidebar, @project)
55
    push_frontend_feature_flag(:confidential_notes, @project)
56 57
  end

58 59
  around_action :allow_gitaly_ref_name_caching, only: [:discussions]

D
Dmitriy Zaporozhets 已提交
60
  respond_to :html
G
gitlabhq 已提交
61

62 63
  alias_method :designs, :show

G
gitlabhq 已提交
64
  def index
J
Jarka Kadlecova 已提交
65
    @issues = @issuables
66

G
gitlabhq 已提交
67
    respond_to do |format|
D
Dmitriy Zaporozhets 已提交
68
      format.html
69
      format.atom { render layout: 'xml.atom' }
D
Dmitriy Zaporozhets 已提交
70 71
      format.json do
        render json: {
72
          html: view_to_html_string("projects/issues/_issues"),
P
Phil Hughes 已提交
73
          labels: @labels.as_json(methods: :text_color)
D
Dmitriy Zaporozhets 已提交
74 75
        }
      end
G
gitlabhq 已提交
76 77 78
    end
  end

79
  def calendar
80
    render_issues_calendar(@issuables)
81 82
  end

G
gitlabhq 已提交
83
  def new
84
    params[:issue] ||= ActionController::Parameters.new(
85
      assignee_ids: ""
86
    )
87
    build_params = issue_params.merge(
B
Bob Van Landuyt 已提交
88
      merge_request_to_resolve_discussions_of: params[:merge_request_to_resolve_discussions_of],
89
      discussion_to_resolve: params[:discussion_to_resolve]
90
    )
91
    service = Issues::BuildService.new(project, current_user, build_params)
92

93
    @issue = @noteable = service.execute
B
Bob Van Landuyt 已提交
94
    @merge_request_to_resolve_discussions_of = service.merge_request_to_resolve_discussions_of
95
    @discussion_to_resolve = service.discussions_to_resolve.first if params[:discussion_to_resolve]
96

G
gitlabhq 已提交
97 98 99
    respond_with(@issue)
  end

100 101 102 103
  def edit
    respond_with(@issue)
  end

G
gitlabhq 已提交
104
  def create
105
    create_params = issue_params.merge(spammable_params).merge(
B
Bob Van Landuyt 已提交
106
      merge_request_to_resolve_discussions_of: params[:merge_request_to_resolve_discussions_of],
107 108 109
      discussion_to_resolve: params[:discussion_to_resolve]
    )

110 111 112
    service = Issues::CreateService.new(project, current_user, create_params)
    @issue = service.execute

113
    if service.discussions_to_resolve.count(&:resolved?) > 0
114
      flash[:notice] = if service.discussion_to_resolve_id
115
                         _("Resolved 1 discussion.")
116
                       else
117
                         _("Resolved all discussions.")
118
                       end
119
    end
G
gitlabhq 已提交
120

121
    respond_to do |format|
122
      format.html do
123
        recaptcha_check_with_fallback { render :new }
124
      end
125
      format.js do
126 127
        @link = @issue.attachment.url.to_js
      end
128
    end
G
gitlabhq 已提交
129 130
  end

131 132 133
  def move
    params.require(:move_to_project_id)

134 135
    if params[:move_to_project_id].to_i > 0
      new_project = Project.find(params[:move_to_project_id])
136 137
      return render_404 unless issue.can_move?(current_user, new_project)

138
      @issue = Issues::UpdateService.new(project, current_user, target_project: new_project).execute(issue)
139
    end
G
gitlabhq 已提交
140 141

    respond_to do |format|
142
      format.json do
143
        render_issue_json
144
      end
G
gitlabhq 已提交
145
    end
146 147

  rescue ActiveRecord::StaleObjectError
148
    render_conflict_response
G
gitlabhq 已提交
149 150
  end

151 152 153 154 155 156 157 158 159 160
  def reorder
    service = Issues::ReorderService.new(project, current_user, reorder_params)

    if service.execute(issue)
      head :ok
    else
      head :unprocessable_entity
    end
  end

161
  def related_branches
162 163 164 165
    @related_branches = Issues::RelatedBranchesService
      .new(project, current_user)
      .execute(issue)
      .map { |branch| branch.merge(link: branch_link(branch)) }
166 167 168 169 170 171 172 173 174 175

    respond_to do |format|
      format.json do
        render json: {
          html: view_to_html_string('projects/issues/_related_branches')
        }
      end
    end
  end

176 177 178
  def can_create_branch
    can_create = current_user &&
      can?(current_user, :push_code, @project) &&
H
Heinrich Lee Yu 已提交
179
      @issue.can_be_worked_on?
180 181 182

    respond_to do |format|
      format.json do
H
Heinrich Lee Yu 已提交
183
        render json: { can_create_branch: can_create, suggested_branch_name: @issue.suggested_branch_name }
184 185 186 187
      end
    end
  end

188
  def create_merge_request
189
    create_params = params.slice(:branch_name, :ref).merge(issue_iid: issue.iid)
190
    create_params[:target_project_id] = params[:target_project_id] if helpers.create_confidential_merge_request_enabled?
191
    result = ::MergeRequests::CreateFromIssueService.new(project, current_user, create_params).execute
192 193 194 195

    if result[:status] == :success
      render json: MergeRequestCreateSerializer.new.represent(result[:merge_request])
    else
196
      render json: result[:message], status: :unprocessable_entity
197 198 199
    end
  end

200 201 202 203
  def export_csv
    ExportCsvWorker.perform_async(current_user.id, project.id, finder_options.to_h) # rubocop:disable CodeReuse/Worker

    index_path = project_issues_path(project)
204 205
    message = _('Your CSV export has started. It will be emailed to %{email} when complete.') % { email: current_user.notification_email }
    redirect_to(index_path, notice: message)
206 207
  end

H
Heinrich Lee Yu 已提交
208
  def import_csv
209
    if uploader = UploadService.new(project, params[:file]).execute
210
      ImportIssuesCsvWorker.perform_async(current_user.id, project.id, uploader.upload.id) # rubocop:disable CodeReuse/Worker
H
Heinrich Lee Yu 已提交
211 212 213 214 215 216 217

      flash[:notice] = _("Your issues are being imported. Once finished, you'll get a confirmation email.")
    else
      flash[:alert] = _("File upload error.")
    end

    redirect_to project_issues_path(project)
H
Heinrich Lee Yu 已提交
218 219
  end

N
Nihad Abbasov 已提交
220
  protected
G
gitlabhq 已提交
221

G
George Koltsov 已提交
222
  def sorting_field
223 224 225
    Issue::SORTING_PREFERENCE_FIELD
  end

226
  # rubocop: disable CodeReuse/ActiveRecord
G
gitlabhq 已提交
227
  def issue
228
    return @issue if defined?(@issue)
229

230
    # The Sortable default scope causes performance issues when used with find_by
231
    @issuable = @noteable = @issue ||= @project.issues.includes(author: :status).where(iid: params[:id]).reorder(nil).take!
232
    @note = @project.notes.new(noteable: @issuable)
233 234 235 236

    return render_404 unless can?(current_user, :read_issue, @issue)

    @issue
G
gitlabhq 已提交
237
  end
238
  # rubocop: enable CodeReuse/ActiveRecord
239
  alias_method :subscribable_resource, :issue
240
  alias_method :issuable, :issue
241
  alias_method :awardable, :issue
242
  alias_method :spammable, :issue
D
Dmitriy Zaporozhets 已提交
243

244 245 246 247
  def spammable_path
    project_issue_path(@project, @issue)
  end

248
  def authorize_create_merge_request!
249
    render_404 unless can?(current_user, :push_code, @project) && @issue.can_be_worked_on?
250 251
  end

252 253 254 255 256 257 258 259
  def render_issue_json
    if @issue.valid?
      render json: serializer.represent(@issue)
    else
      render json: { errors: @issue.errors.full_messages }, status: :unprocessable_entity
    end
  end

260
  def issue_params
261 262 263 264
    params.require(:issue).permit(
      *issue_params_attributes,
      sentry_issue_attributes: [:sentry_issue_identifier]
    )
265 266 267 268 269 270 271 272 273 274 275 276 277 278
  end

  def issue_params_attributes
    %i[
      title
      assignee_id
      position
      description
      confidential
      milestone_id
      due_date
      state_event
      task_num
      lock_version
279
      discussion_locked
280
    ] + [{ label_ids: [], assignee_ids: [], update_task: [:index, :checked, :line_number, :line_source] }]
281
  end
282

283 284 285 286
  def reorder_params
    params.permit(:move_before_id, :move_after_id, :group_full_path)
  end

287
  def store_uri
288 289 290
    if request.get? && !request.xhr?
      store_location_for :user, request.fullpath
    end
291
  end
D
Douwe Maan 已提交
292 293 294 295

  def serializer
    IssueSerializer.new(current_user: current_user, project: issue.project)
  end
296 297 298 299 300

  def update_service
    update_params = issue_params.merge(spammable_params)
    Issues::UpdateService.new(project, current_user, update_params)
  end
J
Jarka Kadlecova 已提交
301

302 303
  def finder_type
    IssuesFinder
J
Jarka Kadlecova 已提交
304
  end
305 306 307 308

  def whitelist_query_limiting
    # Also see the following issues:
    #
309 310 311 312
    # 1. https://gitlab.com/gitlab-org/gitlab-foss/issues/42423
    # 2. https://gitlab.com/gitlab-org/gitlab-foss/issues/42424
    # 3. https://gitlab.com/gitlab-org/gitlab-foss/issues/42426
    Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-foss/issues/42422')
313
  end
314 315 316

  private

317 318 319 320
  def branch_link(branch)
    project_compare_path(project, from: project.default_branch, to: branch[:name])
  end

321 322 323 324 325 326 327 328 329 330 331 332 333
  def create_rate_limit
    key = :issues_create

    if rate_limiter.throttled?(key, scope: [@project, @current_user])
      rate_limiter.log_request(request, "#{key}_request_limit".to_sym, current_user)

      render plain: _('This endpoint has been requested too many times. Try again later.'), status: :too_many_requests
    end
  end

  def rate_limiter
    ::Gitlab::ApplicationRateLimiter
  end
G
gitlabhq 已提交
334
end
335 336

Projects::IssuesController.prepend_if_ee('EE::Projects::IssuesController')