2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
class Projects::MergeRequests::DiffsController < Projects::MergeRequests::ApplicationController
|
|
|
|
include DiffHelper
|
|
|
|
include RendersNotes
|
|
|
|
|
|
|
|
before_action :apply_diff_view_cookie!
|
2018-03-17 18:26:18 +05:30
|
|
|
before_action :commit
|
2017-09-10 17:25:29 +05:30
|
|
|
before_action :define_diff_vars
|
|
|
|
before_action :define_diff_comment_vars
|
|
|
|
|
|
|
|
def show
|
2018-11-08 19:23:39 +05:30
|
|
|
render_diffs
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def diff_for_path
|
2018-11-08 19:23:39 +05:30
|
|
|
render_diffs
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def render_diffs
|
|
|
|
@environment = @merge_request.environments_for(current_user).last
|
2018-12-13 13:39:08 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
note_positions = renderable_notes.map(&:position).compact
|
|
|
|
@diffs.unfold_diff_files(note_positions)
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
@diffs.write_cache
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
request = {
|
|
|
|
current_user: current_user,
|
|
|
|
project: @merge_request.project,
|
|
|
|
render: ->(partial, locals) { view_to_html_string(partial, locals) }
|
|
|
|
}
|
|
|
|
|
|
|
|
render json: DiffsSerializer.new(request).represent(@diffs, additional_attributes)
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def define_diff_vars
|
2018-03-17 18:26:18 +05:30
|
|
|
@merge_request_diffs = @merge_request.merge_request_diffs.viewable.order_id_desc
|
|
|
|
@compare = commit || find_merge_request_diff_compare
|
|
|
|
return render_404 unless @compare
|
|
|
|
|
|
|
|
@diffs = @compare.diffs(diff_options)
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
def commit
|
2019-05-18 00:54:41 +05:30
|
|
|
return unless commit_id = params[:commit_id].presence
|
|
|
|
return unless @merge_request.all_commits.exists?(sha: commit_id)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
@commit ||= @project.commit(commit_id)
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
def find_merge_request_diff_compare
|
2017-09-10 17:25:29 +05:30
|
|
|
@merge_request_diff =
|
2018-03-17 18:26:18 +05:30
|
|
|
if diff_id = params[:diff_id].presence
|
|
|
|
@merge_request.merge_request_diffs.viewable.find_by(id: diff_id)
|
2017-09-10 17:25:29 +05:30
|
|
|
else
|
|
|
|
@merge_request.merge_request_diff
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
return unless @merge_request_diff
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
@comparable_diffs = @merge_request_diffs.select { |diff| diff.id < @merge_request_diff.id }
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if @start_sha = params[:start_sha].presence
|
2017-09-10 17:25:29 +05:30
|
|
|
@start_version = @comparable_diffs.find { |diff| diff.head_commit_sha == @start_sha }
|
|
|
|
|
|
|
|
unless @start_version
|
|
|
|
@start_sha = @merge_request_diff.head_commit_sha
|
|
|
|
@start_version = @merge_request_diff
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if @start_sha
|
|
|
|
@merge_request_diff.compare_with(@start_sha)
|
|
|
|
else
|
|
|
|
@merge_request_diff
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def additional_attributes
|
|
|
|
{
|
|
|
|
environment: @environment,
|
|
|
|
merge_request: @merge_request,
|
|
|
|
merge_request_diff: @merge_request_diff,
|
|
|
|
merge_request_diffs: @merge_request_diffs,
|
|
|
|
start_version: @start_version,
|
|
|
|
start_sha: @start_sha,
|
|
|
|
commit: @commit,
|
|
|
|
latest_diff: @merge_request_diff&.latest?
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def define_diff_comment_vars
|
|
|
|
@new_diff_note_attrs = {
|
|
|
|
noteable_type: 'MergeRequest',
|
2018-03-17 18:26:18 +05:30
|
|
|
noteable_id: @merge_request.id,
|
|
|
|
commit_id: @commit&.id
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
@diff_notes_disabled = false
|
|
|
|
|
|
|
|
@use_legacy_diff_notes = !@merge_request.has_complete_diff_refs?
|
|
|
|
|
|
|
|
@grouped_diff_discussions = @merge_request.grouped_diff_discussions(@compare.diff_refs)
|
2018-03-17 18:26:18 +05:30
|
|
|
@notes = prepare_notes_for_rendering(@grouped_diff_discussions.values.flatten.flat_map(&:notes), @merge_request)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
def renderable_notes
|
|
|
|
define_diff_comment_vars unless @notes
|
|
|
|
|
|
|
|
@notes
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|