2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-30 23:06:09 -04:00
|
|
|
# A note on merge request or commit diffs
|
2017-04-06 11:05:57 -04:00
|
|
|
#
|
|
|
|
# A note of this type can be resolvable.
|
2016-06-20 13:20:39 -04:00
|
|
|
class DiffNote < Note
|
|
|
|
include NoteOnDiff
|
2018-10-04 17:45:00 -04:00
|
|
|
include DiffPositionableNote
|
2018-05-16 11:46:18 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2016-06-20 13:20:39 -04:00
|
|
|
|
2018-10-26 09:23:58 -04:00
|
|
|
def self.noteable_types
|
2020-05-04 02:10:10 -04:00
|
|
|
%w(MergeRequest Commit DesignManagement::Design)
|
2018-10-26 09:23:58 -04:00
|
|
|
end
|
2017-03-17 15:25:52 -04:00
|
|
|
|
2016-06-20 13:20:39 -04:00
|
|
|
validates :original_position, presence: true
|
|
|
|
validates :position, presence: true
|
2017-10-07 00:25:17 -04:00
|
|
|
validates :line_code, presence: true, line_code: true, if: :on_text?
|
2019-06-03 10:41:05 -04:00
|
|
|
# We need to evaluate the `noteable` types when running the validation since
|
|
|
|
# EE might have added a type when the module was prepended
|
|
|
|
validates :noteable_type, inclusion: { in: -> (_note) { noteable_types } }
|
2016-06-20 13:20:39 -04:00
|
|
|
validate :positions_complete
|
2020-11-23 16:09:19 -05:00
|
|
|
validate :verify_supported, unless: :importing?
|
2016-06-20 13:20:39 -04:00
|
|
|
|
2019-12-04 10:11:23 -05:00
|
|
|
before_validation :set_line_code, if: :on_text?, unless: :importing?
|
2021-08-20 02:11:03 -04:00
|
|
|
after_save :keep_around_commits, unless: -> { importing? || skip_keep_around_commits }
|
2019-12-18 07:07:48 -05:00
|
|
|
|
|
|
|
NoteDiffFileCreationError = Class.new(StandardError)
|
|
|
|
|
|
|
|
DIFF_LINE_NOT_FOUND_MESSAGE = "Failed to find diff line for: %{file_path}, old_line: %{old_line}, new_line: %{new_line}"
|
|
|
|
DIFF_FILE_NOT_FOUND_MESSAGE = "Failed to find diff file"
|
|
|
|
|
2018-05-16 11:46:18 -04:00
|
|
|
after_commit :create_diff_file, on: :create
|
2016-06-20 13:20:39 -04:00
|
|
|
|
2017-03-09 20:29:11 -05:00
|
|
|
def discussion_class(*)
|
|
|
|
DiffDiscussion
|
|
|
|
end
|
|
|
|
|
2018-05-16 11:46:18 -04:00
|
|
|
def create_diff_file
|
|
|
|
return unless should_create_diff_file?
|
|
|
|
|
|
|
|
diff_file = fetch_diff_file
|
2019-12-18 07:07:48 -05:00
|
|
|
raise NoteDiffFileCreationError, DIFF_FILE_NOT_FOUND_MESSAGE unless diff_file
|
|
|
|
|
2018-05-16 11:46:18 -04:00
|
|
|
diff_line = diff_file.line_for_position(self.original_position)
|
2019-12-18 07:07:48 -05:00
|
|
|
unless diff_line
|
|
|
|
raise NoteDiffFileCreationError, DIFF_LINE_NOT_FOUND_MESSAGE % {
|
|
|
|
file_path: diff_file.file_path,
|
|
|
|
old_line: original_position.old_line,
|
|
|
|
new_line: original_position.new_line
|
|
|
|
}
|
|
|
|
end
|
2018-05-16 11:46:18 -04:00
|
|
|
|
|
|
|
creation_params = diff_file.diff.to_hash
|
|
|
|
.except(:too_large)
|
|
|
|
.merge(diff: diff_file.diff_hunk(diff_line))
|
|
|
|
|
|
|
|
create_note_diff_file(creation_params)
|
|
|
|
end
|
|
|
|
|
2019-02-20 16:20:08 -05:00
|
|
|
# Returns the diff file from `position`
|
|
|
|
def latest_diff_file
|
|
|
|
strong_memoize(:latest_diff_file) do
|
2020-05-04 02:10:10 -04:00
|
|
|
next if for_design?
|
|
|
|
|
2019-06-03 10:42:49 -04:00
|
|
|
position.diff_file(repository)
|
2019-02-20 16:20:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the diff file from `original_position`
|
2016-06-20 13:20:39 -04:00
|
|
|
def diff_file
|
2018-05-16 11:46:18 -04:00
|
|
|
strong_memoize(:diff_file) do
|
2020-05-04 02:10:10 -04:00
|
|
|
next if for_design?
|
|
|
|
|
2018-05-16 11:46:18 -04:00
|
|
|
enqueue_diff_file_creation_job if should_create_diff_file?
|
|
|
|
|
|
|
|
fetch_diff_file
|
|
|
|
end
|
2016-06-20 13:20:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def diff_line
|
2017-05-21 16:38:33 -04:00
|
|
|
@diff_line ||= diff_file&.line_for_position(self.original_position)
|
2016-06-20 13:20:39 -04:00
|
|
|
end
|
|
|
|
|
2016-08-19 13:38:40 -04:00
|
|
|
def original_line_code
|
2017-10-07 00:25:17 -04:00
|
|
|
return unless on_text?
|
|
|
|
|
2016-08-19 13:38:40 -04:00
|
|
|
self.diff_file.line_code(self.diff_line)
|
|
|
|
end
|
|
|
|
|
2017-04-30 16:32:09 -04:00
|
|
|
def created_at_diff?(diff_refs)
|
|
|
|
return false unless supported?
|
|
|
|
return true if for_commit?
|
2017-03-31 19:39:14 -04:00
|
|
|
|
2017-04-30 16:32:09 -04:00
|
|
|
self.original_position.diff_refs == diff_refs
|
2017-03-31 19:39:14 -04:00
|
|
|
end
|
|
|
|
|
2019-09-23 14:06:14 -04:00
|
|
|
# Checks if the current `position` line in the diff
|
|
|
|
# exists and is suggestible (not a deletion).
|
|
|
|
#
|
|
|
|
# Avoid using in iterations as it requests Gitaly.
|
2018-12-13 14:17:19 -05:00
|
|
|
def supports_suggestion?
|
2019-02-19 14:29:10 -05:00
|
|
|
return false unless noteable&.supports_suggestion? && on_text?
|
2018-12-13 14:17:19 -05:00
|
|
|
# We don't want to trigger side-effects of `diff_file` call.
|
2019-03-15 13:35:34 -04:00
|
|
|
return false unless file = latest_diff_file
|
|
|
|
return false unless line = file.line_for_position(self.position)
|
2018-12-13 14:17:19 -05:00
|
|
|
|
|
|
|
line&.suggestible?
|
|
|
|
end
|
|
|
|
|
|
|
|
def banzai_render_context(field)
|
2019-06-02 13:23:53 -04:00
|
|
|
super.merge(suggestions_filter_enabled: true)
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
2021-01-14 16:10:37 -05:00
|
|
|
def multiline?
|
|
|
|
position&.multiline?
|
|
|
|
end
|
|
|
|
|
2021-08-20 02:11:03 -04:00
|
|
|
def shas
|
|
|
|
[
|
|
|
|
self.original_position.base_sha,
|
|
|
|
self.original_position.start_sha,
|
|
|
|
self.original_position.head_sha
|
|
|
|
].tap do |a|
|
|
|
|
if self.position != self.original_position
|
|
|
|
a << self.position.base_sha
|
|
|
|
a << self.position.start_sha
|
|
|
|
a << self.position.head_sha
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-07 16:13:55 -04:00
|
|
|
private
|
|
|
|
|
2018-05-16 11:46:18 -04:00
|
|
|
def enqueue_diff_file_creation_job
|
|
|
|
# Avoid enqueuing multiple file creation jobs at once for a note (i.e.
|
|
|
|
# parallel calls to `DiffNote#diff_file`).
|
|
|
|
lease = Gitlab::ExclusiveLease.new("note_diff_file_creation:#{id}", timeout: 1.hour.to_i)
|
|
|
|
return unless lease.try_obtain
|
|
|
|
|
|
|
|
CreateNoteDiffFileWorker.perform_async(id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_create_diff_file?
|
2019-11-21 04:06:16 -05:00
|
|
|
on_text? && note_diff_file.nil? && start_of_discussion?
|
2018-05-16 11:46:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_diff_file
|
2022-02-22 10:18:06 -05:00
|
|
|
return note_diff_file.raw_diff_file if note_diff_file && !note_diff_file.raw_diff_file.has_renderable?
|
2019-09-16 11:06:26 -04:00
|
|
|
|
2019-12-18 07:07:48 -05:00
|
|
|
if created_at_diff?(noteable.diff_refs)
|
|
|
|
# We're able to use the already persisted diffs (Postgres) if we're
|
|
|
|
# presenting a "current version" of the MR discussion diff.
|
|
|
|
# So no need to make an extra Gitaly diff request for it.
|
|
|
|
# As an extra benefit, the returned `diff_file` already
|
|
|
|
# has `highlighted_diff_lines` data set from Redis on
|
|
|
|
# `Diff::FileCollection::MergeRequestDiff`.
|
2020-06-05 17:08:27 -04:00
|
|
|
file = original_position.find_diff_file_from(noteable)
|
2019-12-18 07:07:48 -05:00
|
|
|
# if line is not found in persisted diffs, fallback and retrieve file from repository using gitaly
|
|
|
|
# This is required because of https://gitlab.com/gitlab-org/gitlab/issues/42676
|
|
|
|
file = nil if file&.line_for_position(original_position).nil? && importing?
|
|
|
|
end
|
2018-10-16 12:21:16 -04:00
|
|
|
|
2019-12-18 07:07:48 -05:00
|
|
|
file ||= original_position.diff_file(repository)
|
2019-09-16 11:06:26 -04:00
|
|
|
file&.unfold_diff_lines(position)
|
2018-10-16 12:21:16 -04:00
|
|
|
|
|
|
|
file
|
2018-05-16 11:46:18 -04:00
|
|
|
end
|
|
|
|
|
2016-07-07 16:13:55 -04:00
|
|
|
def supported?
|
2020-11-23 04:09:13 -05:00
|
|
|
for_commit? || for_design? || self.noteable&.has_complete_diff_refs?
|
2016-07-07 16:13:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_line_code
|
2019-06-03 10:42:49 -04:00
|
|
|
self.line_code = self.position.line_code(repository)
|
2016-07-07 16:13:55 -04:00
|
|
|
end
|
|
|
|
|
2016-06-20 13:20:39 -04:00
|
|
|
def verify_supported
|
|
|
|
return if supported?
|
|
|
|
|
|
|
|
errors.add(:noteable, "doesn't support new-style diff notes")
|
|
|
|
end
|
|
|
|
|
|
|
|
def positions_complete
|
|
|
|
return if self.original_position.complete? && self.position.complete?
|
|
|
|
|
2020-02-17 13:09:00 -05:00
|
|
|
errors.add(:position, "is incomplete")
|
2016-06-20 13:20:39 -04:00
|
|
|
end
|
2016-07-04 12:26:28 -04:00
|
|
|
|
|
|
|
def keep_around_commits
|
2019-06-03 10:42:49 -04:00
|
|
|
repository.keep_around(*shas)
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository
|
|
|
|
noteable.respond_to?(:repository) ? noteable.repository : project.repository
|
2016-07-04 12:26:28 -04:00
|
|
|
end
|
2016-06-20 13:20:39 -04:00
|
|
|
end
|