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, using the legacy implementation.
|
|
|
|
#
|
|
|
|
# All new diff notes are of the type `DiffNote`, but any diff notes created
|
|
|
|
# before the introduction of the new implementation still use `LegacyDiffNote`.
|
|
|
|
#
|
|
|
|
# A note of this type is never resolvable.
|
2016-06-02 11:05:42 +05:30
|
|
|
class LegacyDiffNote < Note
|
2016-08-24 12:49:21 +05:30
|
|
|
include NoteOnDiff
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
serialize :st_diff # rubocop:disable Cop/ActiveRecordSerialize
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
validates :line_code, presence: true, line_code: true
|
|
|
|
|
|
|
|
before_create :set_diff
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def discussion_class(*)
|
|
|
|
LegacyDiffDiscussion
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def project_repository
|
2018-12-05 23:21:45 +05:30
|
|
|
Gitlab::SafeRequestStore.fetch("project:#{project_id}:repository") { self.project.repository }
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def diff_file_hash
|
|
|
|
line_code.split('_')[0] if line_code
|
|
|
|
end
|
|
|
|
|
|
|
|
def diff
|
|
|
|
@diff ||= Gitlab::Git::Diff.new(st_diff) if st_diff.respond_to?(:map)
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def diff_file
|
2016-09-13 17:45:13 +05:30
|
|
|
@diff_file ||= Gitlab::Diff::File.new(diff, repository: project_repository) if diff
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def diff_line
|
2018-03-17 18:26:18 +05:30
|
|
|
@diff_line ||= diff_file&.line_for_line_code(self.line_code)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def original_line_code
|
|
|
|
self.line_code
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
# 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-08-17 22:00:37 +05:30
|
|
|
def active?(diff_refs = nil)
|
2016-06-02 11:05:42 +05:30
|
|
|
return @active if defined?(@active)
|
|
|
|
return true if for_commit?
|
2016-08-24 12:49:21 +05:30
|
|
|
return true unless diff_line
|
2016-06-02 11:05:42 +05:30
|
|
|
return false unless noteable
|
2017-09-10 17:25:29 +05:30
|
|
|
return false if diff_refs && diff_refs != noteable.diff_refs
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
noteable_diff = find_noteable_diff
|
|
|
|
|
|
|
|
if noteable_diff
|
|
|
|
parsed_lines = Gitlab::Diff::Parser.new.parse(noteable_diff.diff.each_line)
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
@active = parsed_lines.any? { |line_obj| line_obj.text == diff_line.text }
|
2016-06-02 11:05:42 +05:30
|
|
|
else
|
|
|
|
@active = false
|
|
|
|
end
|
|
|
|
|
|
|
|
@active
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_diff
|
|
|
|
return nil unless noteable
|
|
|
|
return @diff if defined?(@diff)
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
@diff = noteable.raw_diffs(Commit.max_diff_options).find do |d|
|
2016-06-02 11:05:42 +05:30
|
|
|
d.new_path && Digest::SHA1.hexdigest(d.new_path) == diff_file_hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
self.st_diff = diff.to_hash if diff
|
|
|
|
end
|
|
|
|
|
|
|
|
def diff_for_line_code
|
|
|
|
attributes = {
|
|
|
|
noteable_type: noteable_type,
|
|
|
|
line_code: line_code
|
|
|
|
}
|
|
|
|
|
|
|
|
if for_commit?
|
|
|
|
attributes[:commit_id] = commit_id
|
|
|
|
else
|
|
|
|
attributes[:noteable_id] = noteable_id
|
|
|
|
end
|
|
|
|
|
|
|
|
self.class.where(attributes).last.try(:diff)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Find the diff on noteable that matches our own
|
|
|
|
def find_noteable_diff
|
2016-09-13 17:45:13 +05:30
|
|
|
diffs = noteable.raw_diffs(Commit.max_diff_options)
|
2016-06-02 11:05:42 +05:30
|
|
|
diffs.find { |d| d.new_path == self.diff.new_path }
|
|
|
|
end
|
|
|
|
end
|