2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-04 18:27:23 -04:00
|
|
|
# A note on merge request or commit diffs, using the legacy implementation.
|
2017-04-06 11:05:57 -04:00
|
|
|
#
|
2017-04-04 18:27:23 -04:00
|
|
|
# All new diff notes are of the type `DiffNote`, but any diff notes created
|
|
|
|
# before the introduction of the new implementation still use `LegacyDiffNote`.
|
2017-04-06 11:05:57 -04:00
|
|
|
#
|
|
|
|
# A note of this type is never resolvable.
|
2016-05-10 18:41:46 -04:00
|
|
|
class LegacyDiffNote < Note
|
2016-06-20 13:15:44 -04:00
|
|
|
include NoteOnDiff
|
|
|
|
|
2017-07-03 10:01:41 -04:00
|
|
|
serialize :st_diff # rubocop:disable Cop/ActiveRecordSerialize
|
2016-05-10 18:41:46 -04:00
|
|
|
|
|
|
|
validates :line_code, presence: true, line_code: true
|
|
|
|
|
|
|
|
before_create :set_diff
|
|
|
|
|
2017-03-09 20:29:11 -05:00
|
|
|
def discussion_class(*)
|
|
|
|
LegacyDiffDiscussion
|
2016-05-10 18:41:46 -04:00
|
|
|
end
|
|
|
|
|
2016-07-28 10:51:44 -04:00
|
|
|
def project_repository
|
|
|
|
if RequestStore.active?
|
|
|
|
RequestStore.fetch("project:#{project_id}:repository") { self.project.repository }
|
|
|
|
else
|
|
|
|
self.project.repository
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-13 15:53:31 -04:00
|
|
|
def diff_file_hash
|
|
|
|
line_code.split('_')[0] if line_code
|
2016-05-10 18:41:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def diff
|
|
|
|
@diff ||= Gitlab::Git::Diff.new(st_diff) if st_diff.respond_to?(:map)
|
|
|
|
end
|
|
|
|
|
2016-06-20 13:15:44 -04:00
|
|
|
def diff_file
|
2016-07-28 10:51:44 -04:00
|
|
|
@diff_file ||= Gitlab::Diff::File.new(diff, repository: project_repository) if diff
|
2016-05-13 15:53:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def diff_line
|
2017-12-22 06:49:56 -05:00
|
|
|
@diff_line ||= diff_file&.line_for_line_code(self.line_code)
|
2016-05-10 18:41:46 -04:00
|
|
|
end
|
|
|
|
|
2016-08-19 13:38:40 -04:00
|
|
|
def original_line_code
|
|
|
|
self.line_code
|
|
|
|
end
|
|
|
|
|
2016-05-10 18:41:46 -04:00
|
|
|
# Check if this note is part of an "active" discussion
|
|
|
|
#
|
|
|
|
# This will always return true for anything except MergeRequest noteables,
|
|
|
|
# which have special logic.
|
|
|
|
#
|
|
|
|
# If the note's current diff cannot be matched in the MergeRequest's current
|
|
|
|
# diff, it's considered inactive.
|
2017-03-31 19:39:14 -04:00
|
|
|
def active?(diff_refs = nil)
|
2016-05-13 15:53:31 -04:00
|
|
|
return @active if defined?(@active)
|
2016-05-10 18:41:46 -04:00
|
|
|
return true if for_commit?
|
2016-07-15 18:32:49 -04:00
|
|
|
return true unless diff_line
|
2016-05-10 18:41:46 -04:00
|
|
|
return false unless noteable
|
2017-05-15 14:19:49 -04:00
|
|
|
return false if diff_refs && diff_refs != noteable.diff_refs
|
2016-05-10 18:41:46 -04:00
|
|
|
|
|
|
|
noteable_diff = find_noteable_diff
|
|
|
|
|
|
|
|
if noteable_diff
|
|
|
|
parsed_lines = Gitlab::Diff::Parser.new.parse(noteable_diff.diff.each_line)
|
|
|
|
|
2016-06-20 13:15:44 -04:00
|
|
|
@active = parsed_lines.any? { |line_obj| line_obj.text == diff_line.text }
|
2016-05-10 18:41:46 -04:00
|
|
|
else
|
|
|
|
@active = false
|
|
|
|
end
|
|
|
|
|
|
|
|
@active
|
|
|
|
end
|
|
|
|
|
2016-05-13 15:53:31 -04:00
|
|
|
private
|
2016-05-10 18:41:46 -04:00
|
|
|
|
2016-05-13 15:53:31 -04:00
|
|
|
def find_diff
|
|
|
|
return nil unless noteable
|
|
|
|
return @diff if defined?(@diff)
|
2016-05-10 18:41:46 -04:00
|
|
|
|
2016-07-27 13:00:34 -04:00
|
|
|
@diff = noteable.raw_diffs(Commit.max_diff_options).find do |d|
|
2016-05-13 15:53:31 -04:00
|
|
|
d.new_path && Digest::SHA1.hexdigest(d.new_path) == diff_file_hash
|
2016-05-10 18:41:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-13 15:53:31 -04:00
|
|
|
def set_diff
|
|
|
|
# First lets find notes with same diff
|
|
|
|
# before iterating over all mr diffs
|
|
|
|
diff = diff_for_line_code unless for_merge_request?
|
|
|
|
diff ||= find_diff
|
2016-05-10 18:41:46 -04:00
|
|
|
|
2016-05-13 15:53:31 -04:00
|
|
|
self.st_diff = diff.to_hash if diff
|
2016-05-10 18:41:46 -04:00
|
|
|
end
|
|
|
|
|
2016-05-13 15:53:31 -04:00
|
|
|
def diff_for_line_code
|
|
|
|
attributes = {
|
|
|
|
noteable_type: noteable_type,
|
|
|
|
line_code: line_code
|
|
|
|
}
|
2016-05-10 18:41:46 -04:00
|
|
|
|
2016-05-13 15:53:31 -04:00
|
|
|
if for_commit?
|
|
|
|
attributes[:commit_id] = commit_id
|
|
|
|
else
|
|
|
|
attributes[:noteable_id] = noteable_id
|
2016-05-10 18:41:46 -04:00
|
|
|
end
|
|
|
|
|
2016-05-13 15:53:31 -04:00
|
|
|
self.class.where(attributes).last.try(:diff)
|
2016-05-10 18:41:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Find the diff on noteable that matches our own
|
|
|
|
def find_noteable_diff
|
2016-07-27 13:00:34 -04:00
|
|
|
diffs = noteable.raw_diffs(Commit.max_diff_options)
|
2016-05-10 18:41:46 -04:00
|
|
|
diffs.find { |d| d.new_path == self.diff.new_path }
|
|
|
|
end
|
|
|
|
end
|