debian-mirror-gitlab/app/controllers/projects/merge_requests/diffs_controller.rb

206 lines
6.1 KiB
Ruby
Raw Normal View History

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
2020-01-01 13:55:28 +05:30
before_action :commit
before_action :define_diff_vars
2019-12-21 20:55:43 +05:30
before_action :define_diff_comment_vars, except: [:diffs_batch, :diffs_metadata]
2020-07-28 23:09:34 +05:30
before_action :update_diff_discussion_positions!
2017-09-10 17:25:29 +05:30
2020-05-24 23:13:21 +05:30
around_action :allow_gitaly_ref_name_caching
2021-03-08 18:12:59 +05:30
after_action :track_viewed_diffs_events, only: [:diffs_batch]
2017-09-10 17:25:29 +05:30
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
2019-12-21 20:55:43 +05:30
def diffs_batch
2021-01-29 00:20:46 +05:30
diff_options_hash = diff_options
diff_options_hash[:paths] = params[:paths] if params[:paths]
diffs = @compare.diffs_in_batch(params[:page], params[:per_page], diff_options: diff_options_hash)
2019-12-21 20:55:43 +05:30
positions = @merge_request.note_positions_for_paths(diffs.diff_file_paths, current_user)
2020-11-24 15:15:51 +05:30
environment = @merge_request.environments_for(current_user, latest: true).last
2019-12-21 20:55:43 +05:30
diffs.unfold_diff_files(positions.unfoldable)
2020-01-01 13:55:28 +05:30
diffs.write_cache
2019-12-21 20:55:43 +05:30
options = {
2020-11-24 15:15:51 +05:30
environment: environment,
2019-12-21 20:55:43 +05:30
merge_request: @merge_request,
2019-12-26 22:10:19 +05:30
diff_view: diff_view,
2021-01-29 00:20:46 +05:30
merge_ref_head_diff: render_merge_ref_head_diff?,
2019-12-21 20:55:43 +05:30
pagination_data: diffs.pagination_data
}
render json: PaginatedDiffSerializer.new(current_user: current_user).represent(diffs, options)
end
def diffs_metadata
2020-01-01 13:55:28 +05:30
diffs = @compare.diffs(diff_options)
2020-10-24 23:57:45 +05:30
render json: DiffsMetadataSerializer.new(project: @merge_request.project, current_user: current_user)
2020-01-01 13:55:28 +05:30
.represent(diffs, additional_attributes)
2019-12-21 20:55:43 +05:30
end
2017-09-10 17:25:29 +05:30
private
2019-12-21 20:55:43 +05:30
def preloadable_mr_relations
[{ source_project: :namespace }, { target_project: :namespace }]
end
2020-01-01 13:55:28 +05:30
# Deprecated: https://gitlab.com/gitlab-org/gitlab/issues/37735
2018-11-08 19:23:39 +05:30
def render_diffs
2020-01-01 13:55:28 +05:30
diffs = @compare.diffs(diff_options)
2020-03-13 15:44:24 +05:30
@environment = @merge_request.environments_for(current_user, latest: true).last
2018-12-13 13:39:08 +05:30
2020-01-01 13:55:28 +05:30
diffs.unfold_diff_files(note_positions.unfoldable)
diffs.write_cache
2018-11-20 20:47:30 +05:30
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) }
}
2021-01-29 00:20:46 +05:30
options = additional_attributes.merge(
2021-02-22 17:27:13 +05:30
diff_view: "inline",
2021-01-29 00:20:46 +05:30
merge_ref_head_diff: render_merge_ref_head_diff?
)
2019-12-26 22:10:19 +05:30
2020-03-13 15:44:24 +05:30
if @merge_request.project.context_commits_enabled?
2020-04-22 19:07:51 +05:30
options[:context_commits] = @merge_request.recent_context_commits
2020-03-13 15:44:24 +05:30
end
2020-01-01 13:55:28 +05:30
render json: DiffsSerializer.new(request).represent(diffs, options)
2018-11-08 19:23:39 +05:30
end
2020-01-01 13:55:28 +05:30
# Deprecated: https://gitlab.com/gitlab-org/gitlab/issues/37735
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
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def commit
2019-07-07 11:18:12 +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
2020-01-01 13:55:28 +05:30
#
# Deprecated: https://gitlab.com/gitlab-org/gitlab/issues/37735
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 =
2020-04-22 19:07:51 +05:30
if params[:diff_id].present?
@merge_request.merge_request_diffs.viewable.find_by(id: params[:diff_id])
2017-09-10 17:25:29 +05:30
else
@merge_request.merge_request_diff
end
2020-04-22 19:07:51 +05:30
return unless @merge_request_diff&.id
2018-03-17 18:26:18 +05:30
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
2021-01-29 00:20:46 +05:30
if render_merge_ref_head_diff?
2020-03-13 15:44:24 +05:30
return CompareService.new(@project, @merge_request.merge_ref_head.sha)
.execute(@project, @merge_request.target_branch)
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
2020-01-01 13:55:28 +05:30
# Deprecated: https://gitlab.com/gitlab-org/gitlab/issues/37735
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
2021-01-29 00:20:46 +05:30
def render_merge_ref_head_diff?
2021-03-08 18:12:59 +05:30
Gitlab::Utils.to_boolean(params[:diff_head]) && @merge_request.diffable_merge_ref? && @start_sha.nil?
2021-01-29 00:20:46 +05:30
end
2019-12-21 20:55:43 +05:30
def note_positions
@note_positions ||= Gitlab::Diff::PositionCollection.new(renderable_notes.map(&:position))
end
2018-12-13 13:39:08 +05:30
def renderable_notes
define_diff_comment_vars unless @notes
2020-06-23 00:09:42 +05:30
draft_notes =
if current_user
merge_request.draft_notes.authored_by(current_user)
else
[]
end
@notes.concat(draft_notes)
2018-12-13 13:39:08 +05:30
end
2020-07-28 23:09:34 +05:30
def update_diff_discussion_positions!
return if @merge_request.has_any_diff_note_positions?
Discussions::CaptureDiffNotePositionsService.new(@merge_request).execute
end
2021-03-08 18:12:59 +05:30
def track_viewed_diffs_events
return if request.headers['DNT'] == '1'
Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter
.track_mr_diffs_action(merge_request: @merge_request)
return unless current_user&.view_diffs_file_by_file
Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter
.track_mr_diffs_single_file_action(merge_request: @merge_request, user: current_user)
end
2017-09-10 17:25:29 +05:30
end