2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# A note on merge request or commit diffs
|
|
|
|
#
|
|
|
|
# A note of this type can be resolvable.
|
2016-08-24 12:49:21 +05:30
|
|
|
class DiffNote < Note
|
|
|
|
include NoteOnDiff
|
2018-12-05 23:21:45 +05:30
|
|
|
include DiffPositionableNote
|
2018-11-08 19:23:39 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
def self.noteable_types
|
|
|
|
%w(MergeRequest Commit)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
validates :original_position, presence: true
|
|
|
|
validates :position, presence: true
|
2018-03-17 18:26:18 +05:30
|
|
|
validates :line_code, presence: true, line_code: true, if: :on_text?
|
2019-09-04 21:01:54 +05:30
|
|
|
# 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-08-24 12:49:21 +05:30
|
|
|
validate :positions_complete
|
|
|
|
validate :verify_supported
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
before_validation :set_line_code, if: :on_text?, unless: :importing?
|
|
|
|
after_save :keep_around_commits, unless: :importing?
|
2020-03-09 13:42:32 +05:30
|
|
|
|
|
|
|
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-11-08 19:23:39 +05:30
|
|
|
after_commit :create_diff_file, on: :create
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def discussion_class(*)
|
|
|
|
DiffDiscussion
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def create_diff_file
|
|
|
|
return unless should_create_diff_file?
|
|
|
|
|
|
|
|
diff_file = fetch_diff_file
|
2020-03-09 13:42:32 +05:30
|
|
|
raise NoteDiffFileCreationError, DIFF_FILE_NOT_FOUND_MESSAGE unless diff_file
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
diff_line = diff_file.line_for_position(self.original_position)
|
2020-03-09 13:42:32 +05:30
|
|
|
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-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
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-07-07 11:18:12 +05:30
|
|
|
# Returns the diff file from `position`
|
|
|
|
def latest_diff_file
|
|
|
|
strong_memoize(:latest_diff_file) do
|
2019-09-04 21:01:54 +05:30
|
|
|
position.diff_file(repository)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the diff file from `original_position`
|
2016-08-24 12:49:21 +05:30
|
|
|
def diff_file
|
2018-11-08 19:23:39 +05:30
|
|
|
strong_memoize(:diff_file) do
|
|
|
|
enqueue_diff_file_creation_job if should_create_diff_file?
|
|
|
|
|
|
|
|
fetch_diff_file
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def diff_line
|
2017-09-10 17:25:29 +05:30
|
|
|
@diff_line ||= diff_file&.line_for_position(self.original_position)
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def original_line_code
|
2018-03-17 18:26:18 +05:30
|
|
|
return unless on_text?
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
self.diff_file.line_code(self.diff_line)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def created_at_diff?(diff_refs)
|
|
|
|
return false unless supported?
|
|
|
|
return true if for_commit?
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
self.original_position.diff_refs == diff_refs
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
# Checks if the current `position` line in the diff
|
|
|
|
# exists and is suggestible (not a deletion).
|
|
|
|
#
|
|
|
|
# Avoid using in iterations as it requests Gitaly.
|
2019-02-15 15:39:39 +05:30
|
|
|
def supports_suggestion?
|
2019-09-04 21:01:54 +05:30
|
|
|
return false unless noteable&.supports_suggestion? && on_text?
|
2019-02-15 15:39:39 +05:30
|
|
|
# We don't want to trigger side-effects of `diff_file` call.
|
2019-07-07 11:18:12 +05:30
|
|
|
return false unless file = latest_diff_file
|
|
|
|
return false unless line = file.line_for_position(self.position)
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
line&.suggestible?
|
|
|
|
end
|
|
|
|
|
|
|
|
def banzai_render_context(field)
|
2019-09-04 21:01:54 +05:30
|
|
|
super.merge(suggestions_filter_enabled: true)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
private
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
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?
|
2020-01-01 13:55:28 +05:30
|
|
|
on_text? && note_diff_file.nil? && start_of_discussion?
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_diff_file
|
2019-12-04 20:38:33 +05:30
|
|
|
return note_diff_file.raw_diff_file if note_diff_file
|
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
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`.
|
|
|
|
file = noteable.diffs(original_position.diff_options).diff_files.first
|
|
|
|
# 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-12-13 13:39:08 +05:30
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
file ||= original_position.diff_file(repository)
|
2019-12-04 20:38:33 +05:30
|
|
|
file&.unfold_diff_lines(position)
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
file
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def supported?
|
2016-09-13 17:45:13 +05:30
|
|
|
for_commit? || self.noteable.has_complete_diff_refs?
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def set_line_code
|
2019-09-04 21:01:54 +05:30
|
|
|
self.line_code = self.position.line_code(repository)
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
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-03-09 13:42:32 +05:30
|
|
|
errors.add(:position, "is incomplete")
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def keep_around_commits
|
2018-11-20 20:47:30 +05:30
|
|
|
shas = [
|
|
|
|
self.original_position.base_sha,
|
|
|
|
self.original_position.start_sha,
|
|
|
|
self.original_position.head_sha
|
|
|
|
]
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
if self.position != self.original_position
|
2018-11-20 20:47:30 +05:30
|
|
|
shas << self.position.base_sha
|
|
|
|
shas << self.position.start_sha
|
|
|
|
shas << self.position.head_sha
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
repository.keep_around(*shas)
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository
|
|
|
|
noteable.respond_to?(:repository) ? noteable.repository : project.repository
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
DiffNote.prepend_if_ee('::EE::DiffNote')
|