diff_note.rb 3.85 KB
Newer Older
Douwe Maan's avatar
Douwe Maan committed
1
# A note on merge request or commit diffs
2 3
#
# A note of this type can be resolvable.
Douwe Maan's avatar
Douwe Maan committed
4 5 6
class DiffNote < Note
  include NoteOnDiff

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

9 10 11
  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
Douwe Maan's avatar
Douwe Maan committed
12 13 14

  validates :original_position, presence: true
  validates :position, presence: true
Felipe Artur's avatar
Felipe Artur committed
15 16
  validates :diff_line, presence: true, if: :on_text?
  validates :line_code, presence: true, line_code: true, if: :on_text?
Douwe Maan's avatar
Douwe Maan committed
17
  validates :noteable_type, inclusion: { in: NOTEABLE_TYPES }
Douwe Maan's avatar
Douwe Maan committed
18 19 20
  validate :positions_complete
  validate :verify_supported

21 22
  before_validation :set_original_position, on: :create
  before_validation :update_position, on: :create, if: :on_text?
23
  before_validation :set_line_code
24
  after_save :keep_around_commits
Douwe Maan's avatar
Douwe Maan committed
25

26 27 28 29
  def discussion_class(*)
    DiffDiscussion
  end

30
  %i(original_position position change_position).each do |meth|
31
    define_method "#{meth}=" do |new_position|
32 33 34
      if new_position.is_a?(String)
        new_position = JSON.parse(new_position) rescue nil
      end
Douwe Maan's avatar
Douwe Maan committed
35

36 37 38 39
      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
40

41 42
      return if new_position == read_attribute(meth)

43 44
      super(new_position)
    end
Douwe Maan's avatar
Douwe Maan committed
45 46
  end

Felipe Artur's avatar
Felipe Artur committed
47 48 49 50 51 52 53 54
  def on_text?
    position.position_type == "text"
  end

  def on_image?
    position.position_type == "image"
  end

Douwe Maan's avatar
Douwe Maan committed
55 56 57 58 59
  def diff_file
    @diff_file ||= self.original_position.diff_file(self.project.repository)
  end

  def diff_line
60
    @diff_line ||= diff_file&.line_for_position(self.original_position)
Douwe Maan's avatar
Douwe Maan committed
61 62 63 64 65 66
  end

  def for_line?(line)
    diff_file.position(line) == self.original_position
  end

67
  def original_line_code
Felipe Artur's avatar
Felipe Artur committed
68 69
    return unless on_text?

70 71 72
    self.diff_file.line_code(self.diff_line)
  end

Douwe Maan's avatar
Douwe Maan committed
73 74 75 76
  def active?(diff_refs = nil)
    return false unless supported?
    return true if for_commit?

77
    diff_refs ||= noteable.diff_refs
Douwe Maan's avatar
Douwe Maan committed
78 79 80 81

    self.position.diff_refs == diff_refs
  end

82 83 84
  def created_at_diff?(diff_refs)
    return false unless supported?
    return true if for_commit?
85

86
    self.original_position.diff_refs == diff_refs
87 88
  end

89 90 91
  private

  def supported?
Connor Shea's avatar
Connor Shea committed
92
    for_commit? || self.noteable.has_complete_diff_refs?
93 94 95
  end

  def set_original_position
96
    self.original_position = self.position.dup unless self.original_position&.complete?
97 98 99 100 101 102
  end

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

103 104 105 106 107 108
  def update_position
    return unless supported?
    return if for_commit?

    return if active?

109 110
    tracer = Gitlab::Diff::PositionTracer.new(
      project: self.project,
111
      old_diff_refs: self.position.diff_refs,
112
      new_diff_refs: self.noteable.diff_refs,
113
      paths: self.position.paths
114 115 116 117 118 119 120 121 122 123
    )

    result = tracer.trace(self.position)
    return unless result

    if result[:outdated]
      self.change_position = result[:position]
    else
      self.position = result[:position]
    end
124 125
  end

Douwe Maan's avatar
Douwe Maan committed
126 127 128 129 130 131 132 133 134 135 136
  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
137 138 139 140 141 142 143 144 145 146 147 148

  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
149
end