BigW Consortium Gitlab

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

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 committed
12 13 14

  validates :original_position, presence: true
  validates :position, presence: true
15 16
  validates :diff_line, presence: true, if: :on_text?
  validates :line_code, presence: true, line_code: true, if: :on_text?
Douwe Maan committed
17
  validates :noteable_type, inclusion: { in: NOTEABLE_TYPES }
Douwe Maan committed
18 19
  validate :positions_complete
  validate :verify_supported
20
  validate :diff_refs_match_commit, if: :for_commit?
Douwe Maan committed
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, if: :on_text?
25
  after_save :keep_around_commits
Douwe Maan committed
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
Douwe Maan committed
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
Douwe Maan committed
41

42 43
      return if new_position == read_attribute(meth)

44 45
      super(new_position)
    end
Douwe Maan committed
46 47
  end

48 49 50 51 52 53 54 55
  def on_text?
    position.position_type == "text"
  end

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

Douwe Maan committed
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)
Douwe Maan committed
62 63
  end

64
  def original_line_code
65 66
    return unless on_text?

67 68 69
    self.diff_file.line_code(self.diff_line)
  end

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 committed
75 76 77 78

    self.position.diff_refs == diff_refs
  end

79 80 81
  def created_at_diff?(diff_refs)
    return false unless supported?
    return true if for_commit?
82

83
    self.original_position.diff_refs == diff_refs
84 85
  end

86 87 88
  private

  def supported?
89
    for_commit? || self.noteable.has_complete_diff_refs?
90 91 92
  end

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

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

100 101 102 103 104 105
  def update_position
    return unless supported?
    return if for_commit?

    return if active?

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

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

    if result[:outdated]
      self.change_position = result[:position]
    else
      self.position = result[:position]
    end
121 122
  end

Douwe Maan committed
123 124 125 126 127 128 129 130 131 132 133
  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
134

135 136 137 138 139 140
  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

141 142 143 144 145 146 147 148 149 150 151
  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 committed
152
end