diff_note.rb 3.46 KB
Newer Older
Douwe Maan's avatar
Douwe Maan committed
1 2 3
class DiffNote < Note
  include NoteOnDiff

Douwe Maan's avatar
Douwe Maan committed
4 5
  NOTEABLE_TYPES = %w(MergeRequest Commit).freeze

Douwe Maan's avatar
Douwe Maan committed
6 7 8 9 10 11 12
  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
Douwe Maan's avatar
Douwe Maan committed
13
  validates :noteable_type, inclusion: { in: NOTEABLE_TYPES }
Douwe Maan's avatar
Douwe Maan committed
14 15 16
  validate :positions_complete
  validate :verify_supported

17
  before_validation :set_original_position, :update_position, on: :create
18
  before_validation :set_line_code
19 20
  # We need to do this again, because it's already in `Note`, but is affected by
  # `update_position` and needs to run after that.
21
  before_validation :set_discussion_id, if: :position_changed?
22
  after_save :keep_around_commits
Douwe Maan's avatar
Douwe Maan committed
23 24 25 26 27

  def new_diff_note?
    true
  end

28 29 30 31
  def discussion_class(*)
    DiffDiscussion
  end

Douwe Maan's avatar
Douwe Maan committed
32
  def diff_attributes
33 34 35 36
    {
      original_position: original_position.to_json,
      position: position.to_json,
    }
Douwe Maan's avatar
Douwe Maan committed
37 38
  end

39 40 41 42 43
  %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
Douwe Maan's avatar
Douwe Maan committed
44

45 46 47 48
      if new_position.is_a?(Hash)
        new_position = new_position.with_indifferent_access
        new_position = Gitlab::Diff::Position.new(new_position)
      end
Douwe Maan's avatar
Douwe Maan committed
49

50 51
      super(new_position)
    end
Douwe Maan's avatar
Douwe Maan committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65
  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

66 67 68 69
  def original_line_code
    self.diff_file.line_code(self.diff_line)
  end

Douwe Maan's avatar
Douwe Maan committed
70 71 72 73
  def active?(diff_refs = nil)
    return false unless supported?
    return true if for_commit?

74
    diff_refs ||= noteable_diff_refs
Douwe Maan's avatar
Douwe Maan committed
75 76 77 78

    self.position.diff_refs == diff_refs
  end

79 80 81
  private

  def supported?
Connor Shea's avatar
Connor Shea committed
82
    for_commit? || self.noteable.has_complete_diff_refs?
83 84
  end

85 86 87 88 89 90 91 92
  def noteable_diff_refs
    if noteable.respond_to?(:diff_sha_refs)
      noteable.diff_sha_refs
    else
      noteable.diff_refs
    end
  end

93
  def set_original_position
94
    self.original_position = self.position.dup unless self.original_position&.complete?
95 96 97 98 99 100
  end

  def set_line_code
    self.line_code = self.position.line_code(self.project.repository)
  end

101 102 103 104 105 106 107 108 109 110
  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,
111
      new_diff_refs: noteable_diff_refs,
112 113 114 115
      paths: self.position.paths
    ).execute(self)
  end

Douwe Maan's avatar
Douwe Maan committed
116 117 118 119 120 121 122 123 124 125 126
  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
127 128 129 130 131 132 133 134 135 136 137 138

  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
Douwe Maan's avatar
Douwe Maan committed
139
end