merge_requests_controller.rb 16.7 KB
Newer Older
1
class Projects::MergeRequestsController < Projects::ApplicationController
2
  include ToggleSubscriptionAction
S
Sean McGivern 已提交
3
  include DiffForPath
J
Jacob Vosmaer 已提交
4
  include DiffHelper
5
  include IssuableActions
6
  include NotesHelper
7
  include ToggleAwardEmoji
8
  include IssuableCollections
J
Jacob Vosmaer 已提交
9

10
  before_action :module_enabled
11
  before_action :merge_request, only: [
D
Douwe Maan 已提交
12
    :edit, :update, :show, :diffs, :commits, :conflicts, :builds, :pipelines, :merge, :merge_check,
S
Sean McGivern 已提交
13
    :ci_status, :toggle_subscription, :cancel_merge_when_build_succeeds, :remove_wip, :resolve_conflicts
14
  ]
15
  before_action :validates_merge_request, only: [:show, :diffs, :commits, :builds, :pipelines]
D
Douwe Maan 已提交
16
  before_action :define_show_vars, only: [:show, :diffs, :commits, :conflicts, :builds, :pipelines]
17
  before_action :define_widget_vars, only: [:merge, :cancel_merge_when_build_succeeds, :merge_check]
S
Sean McGivern 已提交
18 19
  before_action :define_commit_vars, only: [:diffs]
  before_action :define_diff_comment_vars, only: [:diffs]
D
Douwe Maan 已提交
20
  before_action :ensure_ref_fetched, only: [:show, :diffs, :commits, :builds, :conflicts, :pipelines]
K
Katarzyna Kobierska 已提交
21
  before_action :close_merge_request_without_source_project, only: [:show, :diffs, :commits, :builds, :pipelines]
22 23
  before_action :apply_diff_view_cookie!, only: [:new_diffs]
  before_action :build_merge_request, only: [:new, :new_diffs]
D
Dmitriy Zaporozhets 已提交
24 25

  # Allow read any merge_request
26
  before_action :authorize_read_merge_request!
D
Dmitriy Zaporozhets 已提交
27 28

  # Allow write(create) merge_request
D
Dmitriy Zaporozhets 已提交
29
  before_action :authorize_create_merge_request!, only: [:new, :create]
D
Dmitriy Zaporozhets 已提交
30 31

  # Allow modify merge_request
32
  before_action :authorize_update_merge_request!, only: [:close, :edit, :update, :remove_wip, :sort]
D
Dmitriy Zaporozhets 已提交
33

34 35
  before_action :authorize_can_resolve_conflicts!, only: [:conflicts, :resolve_conflicts]

D
Dmitriy Zaporozhets 已提交
36
  def index
37
    @merge_requests = merge_requests_collection
38
    @merge_requests = @merge_requests.page(params[:page])
39
    @merge_requests = @merge_requests.preload(:target_project)
40

41
    @labels = @project.labels.where(title: params[:label_name])
T
Tap 已提交
42

43 44 45 46
    respond_to do |format|
      format.html
      format.json do
        render json: {
47
          html: view_to_html_string("projects/merge_requests/_merge_requests"),
P
Phil Hughes 已提交
48
          labels: @labels.as_json(methods: :text_color)
49 50 51
        }
      end
    end
D
Dmitriy Zaporozhets 已提交
52 53 54
  end

  def show
55
    respond_to do |format|
56
      format.html { define_discussion_vars }
S
Sean McGivern 已提交
57

D
Douwe Maan 已提交
58 59 60 61
      format.json do
        render json: @merge_request
      end

Z
Z.J. van de Weg 已提交
62
      format.patch  do
D
Douwe Maan 已提交
63 64 65
        return render_404 unless @merge_request.diff_refs

        send_git_patch @project.repository, @merge_request.diff_refs
Z
Z.J. van de Weg 已提交
66
      end
D
Douwe Maan 已提交
67

68
      format.diff do
D
Douwe Maan 已提交
69
        return render_404 unless @merge_request.diff_refs
70

D
Douwe Maan 已提交
71
        send_git_diff @project.repository, @merge_request.diff_refs
72
      end
73
    end
R
randx 已提交
74 75
  end

76
  def diffs
77 78
    apply_diff_view_cookie!

79 80 81 82 83 84
    @merge_request_diff =
      if params[:diff_id]
        @merge_request.merge_request_diffs.find(params[:diff_id])
      else
        @merge_request.merge_request_diff
      end
85

86 87 88 89 90
    @merge_request_diffs = @merge_request.merge_request_diffs.select_without_diff
    @comparable_diffs = @merge_request_diffs.select { |diff| diff.id < @merge_request_diff.id }

    if params[:start_sha].present?
      @start_sha = params[:start_sha]
91 92 93 94 95
      @start_version = @comparable_diffs.find { |diff| diff.head_commit_sha == @start_sha }

      unless @start_version
        render_404
      end
96 97
    end

98
    respond_to do |format|
99
      format.html { define_discussion_vars }
100
      format.json do
101 102
        if @start_sha
          compared_diff_version
103
        else
104
          original_diff_version
105
        end
106 107 108

        render json: { html: view_to_html_string("projects/merge_requests/show/_diffs") }
      end
109
    end
D
Dmitriy Zaporozhets 已提交
110 111
  end

S
Sean McGivern 已提交
112 113 114 115 116 117 118 119 120 121
  # With an ID param, loads the MR at that ID. Otherwise, accepts the same params as #new
  # and uses that (unsaved) MR.
  #
  def diff_for_path
    if params[:id]
      merge_request
      define_diff_comment_vars
    else
      build_merge_request
      @diff_notes_disabled = true
122
      @grouped_diff_discussions = {}
S
Sean McGivern 已提交
123 124 125 126
    end

    define_commit_vars

127
    render_diff_for_path(@merge_request.diffs(diff_options))
S
Sean McGivern 已提交
128 129
  end

130
  def commits
131
    respond_to do |format|
132 133 134 135 136
      format.html do
        define_discussion_vars

        render 'show'
      end
137 138 139 140 141 142 143 144 145
      format.json do
        # Get commits from repository
        # or from cache if already merged
        @commits = @merge_request.commits
        @note_counts = Note.where(commit_id: @commits.map(&:id)).
          group(:commit_id).count

        render json: { html: view_to_html_string('projects/merge_requests/show/_commits') }
      end
146
    end
147 148
  end

149 150
  def conflicts
    respond_to do |format|
151
      format.html { define_discussion_vars }
S
Sean McGivern 已提交
152

153
      format.json do
154
        if @merge_request.conflicts_can_be_resolved_in_ui?
155
          render json: @merge_request.conflicts
156 157 158 159 160
        elsif @merge_request.can_be_merged?
          render json: {
            message: 'The merge conflicts for this merge request have already been resolved. Please return to the merge request.',
            type: 'error'
          }
161
        else
162
          render json: {
163
            message: 'The merge conflicts for this merge request cannot be resolved through GitLab. Please try to resolve them locally.',
164 165
            type: 'error'
          }
166 167
        end
      end
168 169 170
    end
  end

171
  def resolve_conflicts
172 173 174 175 176 177 178
    return render_404 unless @merge_request.conflicts_can_be_resolved_in_ui?

    if @merge_request.can_be_merged?
      render status: :bad_request, json: { message: 'The merge conflicts for this merge request have already been resolved.' }
      return
    end

179
    begin
180
      MergeRequests::ResolveService.new(@merge_request.source_project, current_user, params).execute(@merge_request)
181

182
      flash[:notice] = 'All merge conflicts were resolved. The merge request can now be merged.'
183

184
      render json: { redirect_to: namespace_project_merge_request_url(@project.namespace, @project, @merge_request, resolved_conflicts: true) }
185 186 187
    rescue Gitlab::Conflict::File::MissingResolution => e
      render status: :bad_request, json: { message: e.message }
    end
188 189
  end

D
Douwe Maan 已提交
190 191
  def builds
    respond_to do |format|
192 193 194 195 196
      format.html do
        define_discussion_vars

        render 'show'
      end
D
Douwe Maan 已提交
197 198 199 200
      format.json { render json: { html: view_to_html_string('projects/merge_requests/show/_builds') } }
    end
  end

201
  def pipelines
202
    @pipelines = @merge_request.all_pipelines
203

204 205 206 207 208 209 210 211 212 213
    respond_to do |format|
      format.html do
        define_discussion_vars

        render 'show'
      end
      format.json { render json: { html: view_to_html_string('projects/merge_requests/show/_pipelines') } }
    end
  end

D
Dmitriy Zaporozhets 已提交
214
  def new
215 216
    define_new_vars
  end
D
Douwe Maan 已提交
217

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  def new_diffs
    respond_to do |format|
      format.html do
        define_new_vars
        render "new"
      end
      format.json do
        @diffs = if @merge_request.can_be_created
                   @merge_request.diffs(diff_options)
                 else
                   []
                 end
        @diff_notes_disabled = true

        render json: { html: view_to_html_string('projects/merge_requests/_new_diffs', diffs: @diffs) }
      end
    end
D
Dmitriy Zaporozhets 已提交
235 236 237
  end

  def create
I
Izaak Alpert 已提交
238
    @target_branches ||= []
239
    @merge_request = MergeRequests::CreateService.new(project, current_user, merge_request_params).execute
240 241

    if @merge_request.valid?
242
      redirect_to(merge_request_path(@merge_request))
243
    else
I
Izaak Alpert 已提交
244 245 246
      @source_project = @merge_request.source_project
      @target_project = @merge_request.target_project
      render action: "new"
D
Dmitriy Zaporozhets 已提交
247 248 249
    end
  end

D
Douwe Maan 已提交
250 251 252 253 254 255
  def edit
    @source_project = @merge_request.source_project
    @target_project = @merge_request.target_project
    @target_branches = @merge_request.target_project.repository.branch_names
  end

D
Dmitriy Zaporozhets 已提交
256
  def update
257
    @merge_request = MergeRequests::UpdateService.new(project, current_user, merge_request_params).execute(@merge_request)
258

259
    if @merge_request.valid?
260 261
      respond_to do |format|
        format.html do
V
Vinnie Okada 已提交
262
          redirect_to([@merge_request.target_project.namespace.becomes(Namespace),
263
                       @merge_request.target_project, @merge_request])
264
        end
265
        format.json do
266
          render json: @merge_request.to_json(include: { milestone: {}, assignee: { methods: :avatar_url }, labels: { methods: :text_color } })
267
        end
268
      end
269
    else
270
      render "edit"
D
Dmitriy Zaporozhets 已提交
271
    end
272 273 274
  rescue ActiveRecord::StaleObjectError
    @conflict = true
    render :edit
D
Dmitriy Zaporozhets 已提交
275 276
  end

277
  def remove_wip
T
Thomas Balthazar 已提交
278
    MergeRequests::UpdateService.new(project, current_user, wip_event: 'unwip').execute(@merge_request)
279 280 281 282 283

    redirect_to namespace_project_merge_request_path(@project.namespace, @project, @merge_request),
      notice: "The merge request can now be merged."
  end

284
  def merge_check
285
    @merge_request.check_if_can_be_merged
286

D
Dmitriy Zaporozhets 已提交
287
    render partial: "projects/merge_requests/widget/show.html.haml", layout: false
V
Valery Sizov 已提交
288 289
  end

Z
Zeger-Jan van de Weg 已提交
290
  def cancel_merge_when_build_succeeds
291
    return access_denied! unless @merge_request.can_cancel_merge_when_build_succeeds?(current_user)
Z
Zeger-Jan van de Weg 已提交
292

293
    MergeRequests::MergeWhenBuildSucceedsService.new(@project, current_user).cancel(@merge_request)
Z
Zeger-Jan van de Weg 已提交
294 295
  end

296
  def merge
297
    return access_denied! unless @merge_request.can_be_merged_by?(current_user)
298

299 300 301
    # Disable the CI check if merge_when_build_succeeds is enabled since we have
    # to wait until CI completes to know
    unless @merge_request.mergeable?(skip_ci_check: merge_when_build_succeeds_active?)
Z
Zeger-Jan van de Weg 已提交
302 303 304 305
      @status = :failed
      return
    end

306
    if params[:sha] != @merge_request.diff_head_sha
307 308 309 310
      @status = :sha_mismatch
      return
    end

Z
Zeger-Jan van de Weg 已提交
311 312
    @merge_request.update(merge_error: nil)

313 314 315 316 317 318 319
    if params[:merge_when_build_succeeds].present?
      unless @merge_request.pipeline
        @status = :failed
        return
      end

      if @merge_request.pipeline.active?
320 321 322
        MergeRequests::MergeWhenBuildSucceedsService.new(@project, current_user, merge_params)
                                                        .execute(@merge_request)
        @status = :merge_when_build_succeeds
Z
Z.J. van de Weg 已提交
323 324 325
      elsif @merge_request.pipeline.success?
        # This can be triggered when a user clicks the auto merge button while
        # the tests finish at about the same time
326 327 328 329 330
        MergeWorker.perform_async(@merge_request.id, current_user.id, params)
        @status = :success
      else
        @status = :failed
      end
331
    else
Z
Zeger-Jan van de Weg 已提交
332 333
      MergeWorker.perform_async(@merge_request.id, current_user.id, params)
      @status = :success
334
    end
R
randx 已提交
335 336
  end

337
  def branch_from
F
Felipe Artur 已提交
338
    # This is always source
I
Izaak Alpert 已提交
339
    @source_project = @merge_request.nil? ? @project : @merge_request.source_project
340
    @commit = @repository.commit(params[:ref]) if params[:ref].present?
P
Phil Hughes 已提交
341
    render layout: false
342 343 344
  end

  def branch_to
I
Izaak Alpert 已提交
345
    @target_project = selected_target_project
346
    @commit = @target_project.commit(params[:ref]) if params[:ref].present?
P
Phil Hughes 已提交
347
    render layout: false
348 349
  end

I
Izaak Alpert 已提交
350 351
  def update_branches
    @target_project = selected_target_project
I
Izaak Alpert 已提交
352
    @target_branches = @target_project.repository.branch_names
353

P
Phil Hughes 已提交
354
    render layout: false
I
Izaak Alpert 已提交
355 356
  end

357
  def ci_status
358 359 360 361
    pipeline = @merge_request.pipeline
    if pipeline
      status = pipeline.status
      coverage = pipeline.try(:coverage)
362

C
Connor Shea 已提交
363
      status = "success_with_warnings" if pipeline.success? && pipeline.has_warnings?
364

365
      status ||= "preparing"
366 367
    else
      ci_service = @merge_request.source_project.ci_service
368
      status = ci_service.commit_status(merge_request.diff_head_sha, merge_request.source_branch) if ci_service
369

370
      if ci_service.respond_to?(:commit_coverage)
371
        coverage = ci_service.commit_coverage(merge_request.diff_head_sha, merge_request.source_branch)
372
      end
373 374 375
    end

    response = {
376
      title: merge_request.title,
377
      sha: merge_request.diff_head_commit.short_id,
378 379 380
      status: status,
      coverage: coverage
    }
381 382 383 384

    render json: response
  end

D
Dmitriy Zaporozhets 已提交
385 386
  protected

I
Izaak Alpert 已提交
387
  def selected_target_project
388 389 390 391 392
    if @project.id.to_s == params[:target_project_id] || @project.forked_project_link.nil?
      @project
    else
      @project.forked_project_link.forked_from_project
    end
I
Izaak Alpert 已提交
393 394
  end

D
Dmitriy Zaporozhets 已提交
395
  def merge_request
396
    @issuable = @merge_request ||= @project.merge_requests.find_by!(iid: params[:id])
D
Dmitriy Zaporozhets 已提交
397
  end
398
  alias_method :subscribable_resource, :merge_request
399
  alias_method :issuable, :merge_request
400
  alias_method :awardable, :merge_request
D
Dmitriy Zaporozhets 已提交
401

D
Dmitriy Zaporozhets 已提交
402
  def authorize_update_merge_request!
403
    return render_404 unless can?(current_user, :update_merge_request, @merge_request)
D
Dmitriy Zaporozhets 已提交
404 405 406
  end

  def authorize_admin_merge_request!
407
    return render_404 unless can?(current_user, :admin_merge_request, @merge_request)
D
Dmitriy Zaporozhets 已提交
408
  end
409

410 411 412 413
  def authorize_can_resolve_conflicts!
    return render_404 unless @merge_request.conflicts_can_be_resolved_by?(current_user)
  end

414
  def module_enabled
F
Felipe Artur 已提交
415
    return render_404 unless @project.feature_available?(:merge_requests, current_user)
416
  end
417 418

  def validates_merge_request
419 420 421
    # Show git not found page
    # if there is no saved commits between source & target branch
    if @merge_request.commits.blank?
422 423
      # and if target branch doesn't exist
      return invalid_mr unless @merge_request.target_branch_exists?
424
    end
425 426 427
  end

  def define_show_vars
428 429 430 431
    @noteable = @merge_request
    @commits_count = @merge_request.commits.count

    @pipeline = @merge_request.pipeline
432
    @statuses = @pipeline.statuses.relevant if @pipeline
433 434 435 436 437 438 439

    if @merge_request.locked_long_ago?
      @merge_request.unlock_mr
      @merge_request.close
    end
  end

440 441 442
  # Discussion tab data is rendered on html responses of actions
  # :show, :diff, :commits, :builds. but not when request the data through AJAX
  def define_discussion_vars
443
    # Build a note object for comment form
444
    @note = @project.notes.new(noteable: @merge_request)
445

446
    @discussions = @merge_request.discussions
447

448 449
    preload_noteable_for_regular_notes(@discussions.flat_map(&:notes))

450
    # This is not executed lazily
451
    @notes = Banzai::NoteRenderer.render(
452
      @discussions.flat_map(&:notes),
453 454 455 456 457 458
      @project,
      current_user,
      @path,
      @project_wiki,
      @ref
    )
459

S
Stan Hu 已提交
460
    preload_max_access_for_authors(@notes, @project)
461 462
  end

463
  def define_widget_vars
464 465
    @pipeline = @merge_request.pipeline
    @pipelines = [@pipeline].compact
466 467
  end

S
Sean McGivern 已提交
468 469 470 471 472 473 474 475 476 477 478
  def define_commit_vars
    @commit = @merge_request.diff_head_commit
    @base_commit = @merge_request.diff_base_commit || @merge_request.likely_diff_base_commit
  end

  def define_diff_comment_vars
    @comments_target = {
      noteable_type: 'MergeRequest',
      noteable_id: @merge_request.id
    }

479
    @use_legacy_diff_notes = !@merge_request.has_complete_diff_refs?
D
Douwe Maan 已提交
480
    @grouped_diff_discussions = @merge_request.notes.inc_relations_for_view.grouped_diff_discussions
S
Sean McGivern 已提交
481 482

    Banzai::NoteRenderer.render(
483
      @grouped_diff_discussions.values.flat_map(&:notes),
S
Sean McGivern 已提交
484 485 486 487 488 489 490 491
      @project,
      current_user,
      @path,
      @project_wiki,
      @ref
    )
  end

492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
  def define_new_vars
    @noteable = @merge_request

    @target_branches = if @merge_request.target_project
                         @merge_request.target_project.repository.branch_names
                       else
                         []
                       end

    @target_project = merge_request.target_project
    @source_project = merge_request.source_project
    @commits = @merge_request.compare_commits.reverse
    @commit = @merge_request.diff_head_commit
    @base_commit = @merge_request.diff_base_commit

    @pipeline = @merge_request.pipeline
    @statuses = @pipeline.statuses.relevant if @pipeline
    @note_counts = Note.where(commit_id: @commits.map(&:id)).
      group(:commit_id).count
  end

513
  def invalid_mr
514
    # Render special view for MR with removed target branch
515 516 517
    render 'invalid'
  end

518
  def merge_request_params
519
    params.require(:merge_request).permit(
520 521
      :title, :assignee_id, :source_project_id, :source_branch,
      :target_project_id, :target_branch, :milestone_id,
522
      :state_event, :description, :task_num, :force_remove_source_branch,
523
      :lock_version, label_ids: []
524 525
    )
  end
526

Z
Zeger-Jan van de Weg 已提交
527 528 529 530
  def merge_params
    params.permit(:should_remove_source_branch, :commit_message)
  end

531 532 533 534 535
  # Make sure merge requests created before 8.0
  # have head file in refs/merge-requests/
  def ensure_ref_fetched
    @merge_request.ensure_ref_fetched
  end
536 537 538 539 540

  def merge_when_build_succeeds_active?
    params[:merge_when_build_succeeds].present? &&
      @merge_request.pipeline && @merge_request.pipeline.active?
  end
S
Sean McGivern 已提交
541 542 543

  def build_merge_request
    params[:merge_request] ||= ActionController::Parameters.new(source_project: @project)
544
    @merge_request = MergeRequests::BuildService.new(project, current_user, merge_request_params.merge(diff_options: diff_options)).execute
S
Sean McGivern 已提交
545
  end
546

547
  def compared_diff_version
548 549
    @diff_notes_disabled = true
    @diffs = @merge_request_diff.compare_with(@start_sha).diffs(diff_options)
550
  end
551 552

  def original_diff_version
553
    @diff_notes_disabled = !@merge_request_diff.latest?
554 555
    @diffs = @merge_request_diff.diffs(diff_options)
  end
556 557 558 559 560 561

  def close_merge_request_without_source_project
    if !@merge_request.source_project && @merge_request.open?
      @merge_request.close
    end
  end
D
Dmitriy Zaporozhets 已提交
562
end