diff_note.rb 4.1 KB
Newer Older
D
Douwe Maan 已提交
1
# A note on merge request or commit diffs
2 3
#
# A note of this type can be resolvable.
D
Douwe Maan 已提交
4 5 6
class DiffNote < Note
  include NoteOnDiff

D
Douwe Maan 已提交
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
D
Douwe Maan 已提交
12 13 14

  validates :original_position, presence: true
  validates :position, presence: true
F
Felipe Artur 已提交
15 16
  validates :diff_line, presence: true, if: :on_text?
  validates :line_code, presence: true, line_code: true, if: :on_text?
D
Douwe Maan 已提交
17
  validates :noteable_type, inclusion: { in: NOTEABLE_TYPES }
D
Douwe Maan 已提交
18 19
  validate :positions_complete
  validate :verify_supported
20
  validate :diff_refs_match_commit, if: :for_commit?
D
Douwe Maan 已提交
21

22 23
  before_validation :set_original_position, on: :create
  before_validation :update_position, on: :create, if: :on_text?
24
  before_validation :set_line_code
25
  after_save :keep_around_commits
D
Douwe Maan 已提交
26

27 28 29 30
  def discussion_class(*)
    DiffDiscussion
  end

31
  %i(original_position position change_position).each do |meth|
32
    define_method "#{meth}=" do |new_position|
33 34 35
      if new_position.is_a?(String)
        new_position = JSON.parse(new_position) rescue nil
      end
D
Douwe Maan 已提交
36

37 38 39 40
      if new_position.is_a?(Hash)
        new_position = new_position.with_indifferent_access
        new_position = Gitlab::Diff::Position.new(new_position)
      end
D
Douwe Maan 已提交
41

42 43
      return if new_position == read_attribute(meth)

44 45
      super(new_position)
    end
D
Douwe Maan 已提交
46 47
  end

F
Felipe Artur 已提交
48 49 50 51 52 53 54 55
  def on_text?
    position.position_type == "text"
  end

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

D
Douwe Maan 已提交
56 57 58 59 60
  def diff_file
    @diff_file ||= self.original_position.diff_file(self.project.repository)
  end

  def diff_line
61
    @diff_line ||= diff_file&.line_for_position(self.original_position)
D
Douwe Maan 已提交
62 63 64 65 66 67
  end

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

68
  def original_line_code
F
Felipe Artur 已提交
69 70
    return unless on_text?

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

D
Douwe Maan 已提交
74 75 76 77
  def active?(diff_refs = nil)
    return false unless supported?
    return true if for_commit?

78
    diff_refs ||= noteable.diff_refs
D
Douwe Maan 已提交
79 80 81 82

    self.position.diff_refs == diff_refs
  end

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

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

90 91 92
  private

  def supported?
C
Connor Shea 已提交
93
    for_commit? || self.noteable.has_complete_diff_refs?
94 95 96
  end

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

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

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

    return if active?

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

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

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

D
Douwe Maan 已提交
127 128 129 130 131 132 133 134 135 136 137
  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
138

139 140 141 142 143 144
  def diff_refs_match_commit
    return if self.original_position.diff_refs == self.commit.diff_refs

    errors.add(:commit_id, 'does not match the diff refs')
  end

145 146 147 148 149 150 151 152 153 154 155
  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
D
Douwe Maan 已提交
156
end