BigW Consortium Gitlab

diff_note.rb 3.63 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 15 16

  validates :original_position, presence: true
  validates :position, presence: true
  validates :diff_line, presence: true
  validates :line_code, presence: true, line_code: true
Douwe Maan committed
17
  validates :noteable_type, inclusion: { in: NOTEABLE_TYPES }
Douwe Maan committed
18 19 20
  validate :positions_complete
  validate :verify_supported

21
  before_validation :set_original_position, :update_position, on: :create
22
  before_validation :set_line_code
23
  after_save :keep_around_commits
Douwe Maan committed
24

25 26 27 28
  def discussion_class(*)
    DiffDiscussion
  end

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

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

40 41
      return if new_position == read_attribute(meth)

42 43
      super(new_position)
    end
Douwe Maan committed
44 45 46 47 48 49 50
  end

  def diff_file
    @diff_file ||= self.original_position.diff_file(self.project.repository)
  end

  def diff_line
51
    @diff_line ||= diff_file&.line_for_position(self.original_position)
Douwe Maan committed
52 53 54 55 56 57
  end

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

58 59 60 61
  def original_line_code
    self.diff_file.line_code(self.diff_line)
  end

Douwe Maan committed
62 63 64 65
  def active?(diff_refs = nil)
    return false unless supported?
    return true if for_commit?

66
    diff_refs ||= noteable.diff_refs
Douwe Maan committed
67 68 69 70

    self.position.diff_refs == diff_refs
  end

71 72 73
  def created_at_diff?(diff_refs)
    return false unless supported?
    return true if for_commit?
74

75
    self.original_position.diff_refs == diff_refs
76 77
  end

78 79 80
  private

  def supported?
81
    for_commit? || self.noteable.has_complete_diff_refs?
82 83 84
  end

  def set_original_position
85
    self.original_position = self.position.dup unless self.original_position&.complete?
86 87 88 89 90 91
  end

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

92 93 94 95 96 97
  def update_position
    return unless supported?
    return if for_commit?

    return if active?

98 99
    tracer = Gitlab::Diff::PositionTracer.new(
      project: self.project,
100
      old_diff_refs: self.position.diff_refs,
101
      new_diff_refs: self.noteable.diff_refs,
102
      paths: self.position.paths
103 104 105 106 107 108 109 110 111 112
    )

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

    if result[:outdated]
      self.change_position = result[:position]
    else
      self.position = result[:position]
    end
113 114
  end

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

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