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
|
|
|
|
|
2017-03-17 15:25:52 -04:00
|
|
|
NOTEABLE_TYPES = %w(MergeRequest Commit).freeze
|
|
|
|
|
2017-07-03 10:01:41 -04:00
|
|
|
serialize :original_position, Gitlab::Diff::Position # rubocop:disable Cop/ActiveRecordSerialize
|
|
|
|
serialize :position, Gitlab::Diff::Position # rubocop:disable Cop/ActiveRecordSerialize
|
|
|
|
serialize :change_position, Gitlab::Diff::Position # rubocop:disable Cop/ActiveRecordSerialize
|
2016-06-20 13:20:39 -04:00
|
|
|
|
|
|
|
validates :original_position, presence: true
|
|
|
|
validates :position, presence: true
|
|
|
|
validates :diff_line, presence: true
|
|
|
|
validates :line_code, presence: true, line_code: true
|
2017-03-17 15:25:52 -04:00
|
|
|
validates :noteable_type, inclusion: { in: NOTEABLE_TYPES }
|
2016-06-20 13:20:39 -04:00
|
|
|
validate :positions_complete
|
|
|
|
validate :verify_supported
|
|
|
|
|
2016-06-20 13:22:39 -04:00
|
|
|
before_validation :set_original_position, :update_position, on: :create
|
2017-03-09 20:29:11 -05:00
|
|
|
before_validation :set_line_code
|
2016-07-04 12:26:28 -04:00
|
|
|
after_save :keep_around_commits
|
2016-06-20 13:20:39 -04:00
|
|
|
|
2017-03-09 20:29:11 -05:00
|
|
|
def discussion_class(*)
|
|
|
|
DiffDiscussion
|
|
|
|
end
|
|
|
|
|
2017-05-21 16:38:33 -04:00
|
|
|
%i(original_position position change_position).each do |meth|
|
2017-03-30 20:38:21 -04:00
|
|
|
define_method "#{meth}=" do |new_position|
|
2017-03-09 20:29:11 -05:00
|
|
|
if new_position.is_a?(String)
|
|
|
|
new_position = JSON.parse(new_position) rescue nil
|
|
|
|
end
|
2016-06-20 13:20:39 -04:00
|
|
|
|
2017-03-09 20:29:11 -05:00
|
|
|
if new_position.is_a?(Hash)
|
|
|
|
new_position = new_position.with_indifferent_access
|
|
|
|
new_position = Gitlab::Diff::Position.new(new_position)
|
|
|
|
end
|
2016-06-20 13:20:39 -04:00
|
|
|
|
2017-05-21 16:38:33 -04:00
|
|
|
return if new_position == read_attribute(meth)
|
|
|
|
|
2017-03-09 20:29:11 -05:00
|
|
|
super(new_position)
|
|
|
|
end
|
2016-06-20 13:20:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def diff_file
|
|
|
|
@diff_file ||= self.original_position.diff_file(self.project.repository)
|
|
|
|
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
|
|
|
|
|
|
|
|
def for_line?(line)
|
|
|
|
diff_file.position(line) == self.original_position
|
|
|
|
end
|
|
|
|
|
2016-08-19 13:38:40 -04:00
|
|
|
def original_line_code
|
|
|
|
self.diff_file.line_code(self.diff_line)
|
|
|
|
end
|
|
|
|
|
2016-06-20 13:20:39 -04:00
|
|
|
def active?(diff_refs = nil)
|
|
|
|
return false unless supported?
|
|
|
|
return true if for_commit?
|
|
|
|
|
2017-05-15 14:19:49 -04:00
|
|
|
diff_refs ||= noteable.diff_refs
|
2016-06-20 13:20:39 -04:00
|
|
|
|
|
|
|
self.position.diff_refs == diff_refs
|
|
|
|
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
|
|
|
|
|
2016-07-07 16:13:55 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def supported?
|
2016-08-17 14:29:05 -04:00
|
|
|
for_commit? || self.noteable.has_complete_diff_refs?
|
2016-07-07 16:13:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_original_position
|
2017-03-09 20:29:11 -05:00
|
|
|
self.original_position = self.position.dup unless self.original_position&.complete?
|
2016-07-07 16:13:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_line_code
|
|
|
|
self.line_code = self.position.line_code(self.project.repository)
|
|
|
|
end
|
|
|
|
|
2016-06-20 13:22:39 -04:00
|
|
|
def update_position
|
|
|
|
return unless supported?
|
|
|
|
return if for_commit?
|
|
|
|
|
|
|
|
return if active?
|
|
|
|
|
2017-05-31 15:31:33 -04:00
|
|
|
tracer = Gitlab::Diff::PositionTracer.new(
|
|
|
|
project: self.project,
|
2016-06-20 13:22:39 -04:00
|
|
|
old_diff_refs: self.position.diff_refs,
|
2017-05-31 15:31:33 -04:00
|
|
|
new_diff_refs: self.noteable.diff_refs,
|
2016-06-20 13:22:39 -04:00
|
|
|
paths: self.position.paths
|
2017-05-31 15:31:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
result = tracer.trace(self.position)
|
|
|
|
return unless result
|
|
|
|
|
|
|
|
if result[:outdated]
|
|
|
|
self.change_position = result[:position]
|
|
|
|
else
|
|
|
|
self.position = result[:position]
|
|
|
|
end
|
2016-06-20 13:22:39 -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?
|
|
|
|
|
|
|
|
errors.add(:position, "is invalid")
|
|
|
|
end
|
2016-07-04 12:26:28 -04:00
|
|
|
|
|
|
|
def keep_around_commits
|
|
|
|
project.repository.keep_around(self.original_position.base_sha)
|
|
|
|
project.repository.keep_around(self.original_position.start_sha)
|
|
|
|
project.repository.keep_around(self.original_position.head_sha)
|
|
|
|
|
|
|
|
if self.position != self.original_position
|
|
|
|
project.repository.keep_around(self.position.base_sha)
|
|
|
|
project.repository.keep_around(self.position.start_sha)
|
|
|
|
project.repository.keep_around(self.position.head_sha)
|
|
|
|
end
|
|
|
|
end
|
2016-06-20 13:20:39 -04:00
|
|
|
end
|