gitlab-org--gitlab-foss/app/models/diff_note.rb

138 lines
3.4 KiB
Ruby
Raw Normal View History

2016-06-20 17:20:39 +00:00
class DiffNote < Note
include NoteOnDiff
serialize :original_position, Gitlab::Diff::Position
serialize :position, Gitlab::Diff::Position
validates :original_position, presence: true
validates :position, presence: true
validates :diff_line, presence: true
validates :line_code, presence: true, line_code: true
2017-02-22 17:46:57 +00:00
validates :noteable_type, inclusion: { in: %w(Commit MergeRequest) }
2016-06-20 17:20:39 +00:00
validate :positions_complete
validate :verify_supported
before_validation :set_original_position, :update_position, on: :create
before_validation :set_line_code
# We need to do this again, because it's already in `Note`, but is affected by
# `update_position` and needs to run after that.
before_validation :set_discussion_id, if: :position_changed?
2016-07-04 16:26:28 +00:00
after_save :keep_around_commits
2016-06-20 17:20:39 +00:00
def new_diff_note?
true
end
def discussion_class(*)
DiffDiscussion
end
2016-06-20 17:20:39 +00:00
def diff_attributes
{
original_position: original_position.to_json,
position: position.to_json,
}
2016-06-20 17:20:39 +00:00
end
%i(original_position= position=).each do |meth|
define_method meth do |new_position|
if new_position.is_a?(String)
new_position = JSON.parse(new_position) rescue nil
end
2016-06-20 17:20:39 +00: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 17:20:39 +00:00
super(new_position)
end
2016-06-20 17:20:39 +00:00
end
def diff_file
@diff_file ||= self.original_position.diff_file(self.project.repository)
end
def diff_line
@diff_line ||= diff_file.line_for_position(self.original_position) if diff_file
end
def for_line?(line)
diff_file.position(line) == self.original_position
end
def original_line_code
self.diff_file.line_code(self.diff_line)
end
2016-06-20 17:20:39 +00:00
def active?(diff_refs = nil)
return false unless supported?
return true if for_commit?
diff_refs ||= noteable_diff_refs
2016-06-20 17:20:39 +00:00
self.position.diff_refs == diff_refs
end
private
def supported?
2016-08-17 18:29:05 +00:00
for_commit? || self.noteable.has_complete_diff_refs?
end
def noteable_diff_refs
if noteable.respond_to?(:diff_sha_refs)
noteable.diff_sha_refs
else
noteable.diff_refs
end
end
def set_original_position
self.original_position = self.position.dup unless self.original_position&.complete?
end
def set_line_code
self.line_code = self.position.line_code(self.project.repository)
end
def update_position
return unless supported?
return if for_commit?
return if active?
Notes::DiffPositionUpdateService.new(
self.project,
nil,
old_diff_refs: self.position.diff_refs,
new_diff_refs: noteable_diff_refs,
paths: self.position.paths
).execute(self)
end
2016-06-20 17:20:39 +00: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 16:26:28 +00: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 17:20:39 +00:00
end