2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-11 16:45:16 -04:00
|
|
|
module MergeRequests
|
|
|
|
class ReloadDiffsService
|
|
|
|
def initialize(merge_request, current_user)
|
|
|
|
@merge_request = merge_request
|
|
|
|
@current_user = current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
old_diff_refs = merge_request.diff_refs
|
|
|
|
new_diff = merge_request.create_merge_request_diff
|
|
|
|
|
|
|
|
clear_cache(new_diff)
|
|
|
|
update_diff_discussion_positions(old_diff_refs)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :merge_request, :current_user
|
|
|
|
|
|
|
|
def update_diff_discussion_positions(old_diff_refs)
|
|
|
|
new_diff_refs = merge_request.diff_refs
|
|
|
|
|
|
|
|
merge_request.update_diff_discussion_positions(old_diff_refs: old_diff_refs,
|
|
|
|
new_diff_refs: new_diff_refs,
|
|
|
|
current_user: current_user)
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-06-11 16:45:16 -04:00
|
|
|
def clear_cache(new_diff)
|
|
|
|
# Remove cache for all diffs on this MR. Do not use the association on the
|
|
|
|
# model, as that will interfere with other actions happening when
|
|
|
|
# reloading the diff.
|
2018-10-31 15:51:11 -04:00
|
|
|
MergeRequestDiff
|
|
|
|
.where(merge_request: merge_request)
|
2018-11-01 10:50:39 -04:00
|
|
|
.preload(merge_request: :target_project)
|
2018-10-31 15:51:11 -04:00
|
|
|
.find_each do |merge_request_diff|
|
2018-06-11 16:45:16 -04:00
|
|
|
next if merge_request_diff == new_diff
|
|
|
|
|
2018-09-19 08:26:28 -04:00
|
|
|
cacheable_collection(merge_request_diff).clear_cache
|
2018-06-11 16:45:16 -04:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-09-19 08:26:28 -04:00
|
|
|
|
|
|
|
def cacheable_collection(diff)
|
|
|
|
# There are scenarios where we don't need to request Diff Stats.
|
|
|
|
# Mainly when clearing / writing diff caches.
|
|
|
|
diff.diffs(include_stats: false)
|
|
|
|
end
|
2018-06-11 16:45:16 -04:00
|
|
|
end
|
|
|
|
end
|