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

R
Rajat Jain 已提交
13 14 15 16
  before_action do
    push_frontend_feature_flag(:manual_sorting)
  end

17
  def issue_except_actions
H
Heinrich Lee Yu 已提交
18
    %i[index calendar new create bulk_update import_csv]
19 20
  end

21
  def set_issuables_index_only_actions
22 23 24
    %i[index calendar]
  end

25 26
  prepend_before_action(only: [:index]) { authenticate_sessionless_user!(:rss) }
  prepend_before_action(only: [:calendar]) { authenticate_sessionless_user!(:ics) }
27
  prepend_before_action :authenticate_user!, only: [:new]
28
  prepend_before_action :store_uri, only: [:new, :show]
29

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

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

  # Allow write(create) issue
D
Dmitriy Zaporozhets 已提交
37
  before_action :authorize_create_issue!, only: [:new, :create]
D
Dmitriy Zaporozhets 已提交
38 39

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

42
  # Allow create a new branch and empty WIP merge request from current issue
43
  before_action :authorize_create_merge_request_from!, only: [:create_merge_request]
44

45
  before_action :authorize_import_issues!, only: [:import_csv]
46
  before_action :authorize_download_code!, only: [:related_branches]
47

P
Phil Hughes 已提交
48 49
  before_action :set_suggested_issues_feature_flags, only: [:new]

D
Dmitriy Zaporozhets 已提交
50
  respond_to :html
G
gitlabhq 已提交
51 52

  def index
J
Jarka Kadlecova 已提交
53
    @issues = @issuables
54

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

67
  def calendar
68
    render_issues_calendar(@issuables)
69 70
  end

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

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

G
gitlabhq 已提交
85 86 87
    respond_with(@issue)
  end

88 89 90 91
  def edit
    respond_with(@issue)
  end

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

98 99 100
    service = Issues::CreateService.new(project, current_user, create_params)
    @issue = service.execute

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

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

119 120 121
  def move
    params.require(:move_to_project_id)

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

126
      @issue = Issues::UpdateService.new(project, current_user, target_project: new_project).execute(issue)
127
    end
G
gitlabhq 已提交
128 129

    respond_to do |format|
130
      format.json do
131
        render_issue_json
132
      end
G
gitlabhq 已提交
133
    end
134 135

  rescue ActiveRecord::StaleObjectError
136
    render_conflict_response
G
gitlabhq 已提交
137 138
  end

139 140 141 142 143 144 145 146 147 148
  def reorder
    service = Issues::ReorderService.new(project, current_user, reorder_params)

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

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

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

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

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

173
  def create_merge_request
174
    create_params = params.slice(:branch_name, :ref).merge(issue_iid: issue.iid)
175
    create_params[:target_project_id] = params[:target_project_id] if Feature.enabled?(:create_confidential_merge_request, @project)
176
    result = ::MergeRequests::CreateFromIssueService.new(project, current_user, create_params).execute
177 178 179 180 181 182 183 184

    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 已提交
185
  def import_csv
186 187
    if uploader = UploadService.new(project, params[:file]).execute
      ImportIssuesCsvWorker.perform_async(current_user.id, project.id, uploader.upload.id)
H
Heinrich Lee Yu 已提交
188 189 190 191 192 193 194

      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 已提交
195 196
  end

N
Nihad Abbasov 已提交
197
  protected
G
gitlabhq 已提交
198

199 200 201 202
  def issuable_sorting_field
    Issue::SORTING_PREFERENCE_FIELD
  end

203
  # rubocop: disable CodeReuse/ActiveRecord
G
gitlabhq 已提交
204
  def issue
205
    return @issue if defined?(@issue)
206

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

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

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

221 222 223 224
  def spammable_path
    project_issue_path(@project, @issue)
  end

225
  def authorize_create_merge_request!
226
    render_404 unless can?(current_user, :push_code, @project) && @issue.can_be_worked_on?
227 228
  end

229 230 231 232 233 234 235 236
  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

237
  def issue_params
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    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
253
      discussion_locked
254
    ] + [{ label_ids: [], assignee_ids: [], update_task: [:index, :checked, :line_number, :line_source] }]
255
  end
256

257 258 259 260
  def reorder_params
    params.permit(:move_before_id, :move_after_id, :group_full_path)
  end

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

  def serializer
    IssueSerializer.new(current_user: current_user, project: issue.project)
  end
270 271 272 273 274

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

276 277
  def finder_type
    IssuesFinder
J
Jarka Kadlecova 已提交
278
  end
279 280 281 282 283 284 285 286 287

  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
P
Phil Hughes 已提交
288 289

  def set_suggested_issues_feature_flags
290
    push_frontend_feature_flag(:graphql, default_enabled: true)
P
Phil Hughes 已提交
291
  end
G
gitlabhq 已提交
292
end