issues_controller.rb 8.3 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
H
Heinrich Lee Yu 已提交
14
    %i[index calendar new create bulk_update import_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]
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

D
Dmitriy Zaporozhets 已提交
45
  respond_to :html
G
gitlabhq 已提交
46

47 48
  alias_method :designs, :show

G
gitlabhq 已提交
49
  def index
J
Jarka Kadlecova 已提交
50
    @issues = @issuables
51

G
gitlabhq 已提交
52
    respond_to do |format|
D
Dmitriy Zaporozhets 已提交
53
      format.html
54
      format.atom { render layout: 'xml.atom' }
D
Dmitriy Zaporozhets 已提交
55 56
      format.json do
        render json: {
57
          html: view_to_html_string("projects/issues/_issues"),
P
Phil Hughes 已提交
58
          labels: @labels.as_json(methods: :text_color)
D
Dmitriy Zaporozhets 已提交
59 60
        }
      end
G
gitlabhq 已提交
61 62 63
    end
  end

64
  def calendar
65
    render_issues_calendar(@issuables)
66 67
  end

G
gitlabhq 已提交
68
  def new
69
    params[:issue] ||= ActionController::Parameters.new(
70
      assignee_ids: ""
71
    )
72
    build_params = issue_params.merge(
B
Bob Van Landuyt 已提交
73
      merge_request_to_resolve_discussions_of: params[:merge_request_to_resolve_discussions_of],
74
      discussion_to_resolve: params[:discussion_to_resolve]
75
    )
76
    service = Issues::BuildService.new(project, current_user, build_params)
77

78
    @issue = @noteable = service.execute
B
Bob Van Landuyt 已提交
79
    @merge_request_to_resolve_discussions_of = service.merge_request_to_resolve_discussions_of
80
    @discussion_to_resolve = service.discussions_to_resolve.first if params[:discussion_to_resolve]
81

G
gitlabhq 已提交
82 83 84
    respond_with(@issue)
  end

85 86 87 88
  def edit
    respond_with(@issue)
  end

G
gitlabhq 已提交
89
  def create
90
    create_params = issue_params.merge(spammable_params).merge(
B
Bob Van Landuyt 已提交
91
      merge_request_to_resolve_discussions_of: params[:merge_request_to_resolve_discussions_of],
92 93 94
      discussion_to_resolve: params[:discussion_to_resolve]
    )

95 96 97
    service = Issues::CreateService.new(project, current_user, create_params)
    @issue = service.execute

98
    if service.discussions_to_resolve.count(&:resolved?) > 0
99
      flash[:notice] = if service.discussion_to_resolve_id
100
                         _("Resolved 1 discussion.")
101
                       else
102
                         _("Resolved all discussions.")
103
                       end
104
    end
G
gitlabhq 已提交
105

106
    respond_to do |format|
107
      format.html do
108
        recaptcha_check_with_fallback { render :new }
109
      end
110
      format.js do
111 112
        @link = @issue.attachment.url.to_js
      end
113
    end
G
gitlabhq 已提交
114 115
  end

116 117 118
  def move
    params.require(:move_to_project_id)

119 120
    if params[:move_to_project_id].to_i > 0
      new_project = Project.find(params[:move_to_project_id])
121 122
      return render_404 unless issue.can_move?(current_user, new_project)

123
      @issue = Issues::UpdateService.new(project, current_user, target_project: new_project).execute(issue)
124
    end
G
gitlabhq 已提交
125 126

    respond_to do |format|
127
      format.json do
128
        render_issue_json
129
      end
G
gitlabhq 已提交
130
    end
131 132

  rescue ActiveRecord::StaleObjectError
133
    render_conflict_response
G
gitlabhq 已提交
134 135
  end

136 137 138 139 140 141 142 143 144 145
  def reorder
    service = Issues::ReorderService.new(project, current_user, reorder_params)

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

146
  def related_branches
147
    @related_branches = Issues::RelatedBranchesService.new(project, current_user).execute(issue)
148 149 150 151 152 153 154 155 156 157

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

158 159 160
  def can_create_branch
    can_create = current_user &&
      can?(current_user, :push_code, @project) &&
H
Heinrich Lee Yu 已提交
161
      @issue.can_be_worked_on?
162 163 164

    respond_to do |format|
      format.json do
H
Heinrich Lee Yu 已提交
165
        render json: { can_create_branch: can_create, suggested_branch_name: @issue.suggested_branch_name }
166 167 168 169
      end
    end
  end

170
  def create_merge_request
171
    create_params = params.slice(:branch_name, :ref).merge(issue_iid: issue.iid)
172
    create_params[:target_project_id] = params[:target_project_id] if helpers.create_confidential_merge_request_enabled?
173
    result = ::MergeRequests::CreateFromIssueService.new(project, current_user, create_params).execute
174 175 176 177 178 179 180 181

    if result[:status] == :success
      render json: MergeRequestCreateSerializer.new.represent(result[:merge_request])
    else
      render json: result[:messsage], status: :unprocessable_entity
    end
  end

H
Heinrich Lee Yu 已提交
182
  def import_csv
183 184
    if uploader = UploadService.new(project, params[:file]).execute
      ImportIssuesCsvWorker.perform_async(current_user.id, project.id, uploader.upload.id)
H
Heinrich Lee Yu 已提交
185 186 187 188 189 190 191

      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 已提交
192 193
  end

N
Nihad Abbasov 已提交
194
  protected
G
gitlabhq 已提交
195

G
George Koltsov 已提交
196
  def sorting_field
197 198 199
    Issue::SORTING_PREFERENCE_FIELD
  end

200
  # rubocop: disable CodeReuse/ActiveRecord
G
gitlabhq 已提交
201
  def issue
202
    return @issue if defined?(@issue)
203

204
    # The Sortable default scope causes performance issues when used with find_by
205
    @issuable = @noteable = @issue ||= @project.issues.includes(author: :status).where(iid: params[:id]).reorder(nil).take!
206
    @note = @project.notes.new(noteable: @issuable)
207 208 209 210

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

    @issue
G
gitlabhq 已提交
211
  end
212
  # rubocop: enable CodeReuse/ActiveRecord
213
  alias_method :subscribable_resource, :issue
214
  alias_method :issuable, :issue
215
  alias_method :awardable, :issue
216
  alias_method :spammable, :issue
D
Dmitriy Zaporozhets 已提交
217

218 219 220 221
  def spammable_path
    project_issue_path(@project, @issue)
  end

222
  def authorize_create_merge_request!
223
    render_404 unless can?(current_user, :push_code, @project) && @issue.can_be_worked_on?
224 225
  end

226 227 228 229 230 231 232 233
  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

234
  def issue_params
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    params.require(:issue).permit(*issue_params_attributes)
  end

  def issue_params_attributes
    %i[
      title
      assignee_id
      position
      description
      confidential
      milestone_id
      due_date
      state_event
      task_num
      lock_version
250
      discussion_locked
251
    ] + [{ label_ids: [], assignee_ids: [], update_task: [:index, :checked, :line_number, :line_source] }]
252
  end
253

254 255 256 257
  def reorder_params
    params.permit(:move_before_id, :move_after_id, :group_full_path)
  end

258
  def store_uri
259 260 261
    if request.get? && !request.xhr?
      store_location_for :user, request.fullpath
    end
262
  end
D
Douwe Maan 已提交
263 264 265 266

  def serializer
    IssueSerializer.new(current_user: current_user, project: issue.project)
  end
267 268 269 270 271

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

273 274
  def finder_type
    IssuesFinder
J
Jarka Kadlecova 已提交
275
  end
276 277 278 279 280 281 282 283 284

  def whitelist_query_limiting
    # Also see the following issues:
    #
    # 1. https://gitlab.com/gitlab-org/gitlab-ce/issues/42423
    # 2. https://gitlab.com/gitlab-org/gitlab-ce/issues/42424
    # 3. https://gitlab.com/gitlab-org/gitlab-ce/issues/42426
    Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42422')
  end
G
gitlabhq 已提交
285
end
286 287

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