2018-10-04 17:45:00 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
module DiffPositionableNote
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
before_validation :set_original_position, on: :create
|
2019-12-04 10:11:23 -05:00
|
|
|
before_validation :update_position, on: :create, if: :on_text?, unless: :importing?
|
2018-10-04 17:45:00 -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
|
2019-07-18 08:09:35 -04:00
|
|
|
|
|
|
|
validate :diff_refs_match_commit, if: :for_commit?
|
2021-12-06 22:12:22 -05:00
|
|
|
validates :position, json_schema: { filename: "position", hash_conversion: true }
|
2018-10-04 17:45:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
%i(original_position position change_position).each do |meth|
|
|
|
|
define_method "#{meth}=" do |new_position|
|
|
|
|
if new_position.is_a?(String)
|
2022-08-09 08:11:57 -04:00
|
|
|
new_position = begin
|
|
|
|
Gitlab::Json.parse(new_position)
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
|
|
|
end
|
2018-10-04 17:45:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if new_position.is_a?(Hash)
|
|
|
|
new_position = new_position.with_indifferent_access
|
|
|
|
new_position = Gitlab::Diff::Position.new(new_position)
|
2020-05-13 11:08:23 -04:00
|
|
|
elsif !new_position.is_a?(Gitlab::Diff::Position)
|
|
|
|
new_position = nil
|
2018-10-04 17:45:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
return if new_position == read_attribute(meth)
|
|
|
|
|
|
|
|
super(new_position)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-11 13:15:16 -05:00
|
|
|
def on_text?
|
|
|
|
!!position&.on_text?
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_image?
|
|
|
|
!!position&.on_image?
|
|
|
|
end
|
|
|
|
|
2018-10-04 17:45:00 -04:00
|
|
|
def supported?
|
|
|
|
for_commit? || self.noteable.has_complete_diff_refs?
|
|
|
|
end
|
|
|
|
|
|
|
|
def active?(diff_refs = nil)
|
|
|
|
return false unless supported?
|
|
|
|
return true if for_commit?
|
|
|
|
|
|
|
|
diff_refs ||= noteable.diff_refs
|
|
|
|
|
|
|
|
self.position.diff_refs == diff_refs
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_original_position
|
|
|
|
return unless position
|
|
|
|
|
|
|
|
self.original_position = self.position.dup unless self.original_position&.complete?
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_position
|
|
|
|
return unless supported?
|
|
|
|
return if for_commit?
|
|
|
|
|
|
|
|
return if active?
|
|
|
|
return unless position
|
|
|
|
|
|
|
|
tracer = Gitlab::Diff::PositionTracer.new(
|
|
|
|
project: self.project,
|
|
|
|
old_diff_refs: self.position.diff_refs,
|
|
|
|
new_diff_refs: self.noteable.diff_refs,
|
|
|
|
paths: self.position.paths
|
|
|
|
)
|
|
|
|
|
|
|
|
result = tracer.trace(self.position)
|
|
|
|
return unless result
|
|
|
|
|
|
|
|
if result[:outdated]
|
|
|
|
self.change_position = result[:position]
|
|
|
|
else
|
|
|
|
self.position = result[:position]
|
|
|
|
end
|
|
|
|
end
|
2019-07-18 08:09:35 -04:00
|
|
|
|
|
|
|
def diff_refs_match_commit
|
|
|
|
return if self.original_position.diff_refs == commit&.diff_refs
|
|
|
|
|
|
|
|
errors.add(:commit_id, 'does not match the diff refs')
|
|
|
|
end
|
2018-10-04 17:45:00 -04:00
|
|
|
end
|