diff_note.rb 4.82 KB
Newer Older
1 2
# frozen_string_literal: true

Douwe Maan's avatar
Douwe Maan committed
3
# A note on merge request or commit diffs
4 5
#
# A note of this type can be resolvable.
Douwe Maan's avatar
Douwe Maan committed
6 7
class DiffNote < Note
  include NoteOnDiff
8
  include DiffPositionableNote
9
  include Gitlab::Utils::StrongMemoize
Douwe Maan's avatar
Douwe Maan committed
10

11 12 13
  def self.noteable_types
    %w(MergeRequest Commit)
  end
Douwe Maan's avatar
Douwe Maan committed
14

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

23
  before_validation :set_line_code, if: :on_text?
24
  after_save :keep_around_commits
25
  after_commit :create_diff_file, on: :create
Douwe Maan's avatar
Douwe Maan committed
26

27 28 29 30
  def discussion_class(*)
    DiffDiscussion
  end

31 32 33 34 35 36 37 38 39 40 41 42 43
  def create_diff_file
    return unless should_create_diff_file?

    diff_file = fetch_diff_file
    diff_line = diff_file.line_for_position(self.original_position)

    creation_params = diff_file.diff.to_hash
      .except(:too_large)
      .merge(diff: diff_file.diff_hunk(diff_line))

    create_note_diff_file(creation_params)
  end

44 45 46
  # Returns the diff file from `position`
  def latest_diff_file
    strong_memoize(:latest_diff_file) do
47
      position.diff_file(project.repository)
48 49 50 51
    end
  end

  # Returns the diff file from `original_position`
Douwe Maan's avatar
Douwe Maan committed
52
  def diff_file
53 54 55 56 57
    strong_memoize(:diff_file) do
      enqueue_diff_file_creation_job if should_create_diff_file?

      fetch_diff_file
    end
Douwe Maan's avatar
Douwe Maan committed
58 59 60
  end

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

64
  def original_line_code
Felipe Artur's avatar
Felipe Artur committed
65 66
    return unless on_text?

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

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

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

77 78 79 80 81 82 83 84 85
  def supports_suggestion?
    return false unless noteable.supports_suggestion? && on_text?
    # We don't want to trigger side-effects of `diff_file` call.
    return false unless file = fetch_diff_file
    return false unless line = file.line_for_position(self.original_position)

    line&.suggestible?
  end

86 87 88 89
  def discussion_first_note?
    self == discussion.first_note
  end

90
  def banzai_render_context(field)
91
    super.merge(project: project, suggestions_filter_enabled: supports_suggestion?)
92 93
  end

94 95
  private

96 97 98 99 100 101 102 103 104 105
  def enqueue_diff_file_creation_job
    # Avoid enqueuing multiple file creation jobs at once for a note (i.e.
    # parallel calls to `DiffNote#diff_file`).
    lease = Gitlab::ExclusiveLease.new("note_diff_file_creation:#{id}", timeout: 1.hour.to_i)
    return unless lease.try_obtain

    CreateNoteDiffFileWorker.perform_async(id)
  end

  def should_create_diff_file?
106
    on_text? && note_diff_file.nil? && discussion_first_note?
107 108 109
  end

  def fetch_diff_file
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    file =
      if note_diff_file
        diff = Gitlab::Git::Diff.new(note_diff_file.to_hash)
        Gitlab::Diff::File.new(diff,
                               repository: project.repository,
                               diff_refs: original_position.diff_refs)
      elsif created_at_diff?(noteable.diff_refs)
        # We're able to use the already persisted diffs (Postgres) if we're
        # presenting a "current version" of the MR discussion diff.
        # So no need to make an extra Gitaly diff request for it.
        # As an extra benefit, the returned `diff_file` already
        # has `highlighted_diff_lines` data set from Redis on
        # `Diff::FileCollection::MergeRequestDiff`.
        noteable.diffs(original_position.diff_options).diff_files.first
      else
        original_position.diff_file(self.project.repository)
      end

    # Since persisted diff files already have its content "unfolded"
    # there's no need to make it pass through the unfolding process.
    file&.unfold_diff_lines(position) unless note_diff_file

    file
133 134
  end

135
  def supported?
Connor Shea's avatar
Connor Shea committed
136
    for_commit? || self.noteable.has_complete_diff_refs?
137 138 139 140 141 142
  end

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

Douwe Maan's avatar
Douwe Maan committed
143 144 145 146 147 148 149 150 151 152 153
  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
154

155 156 157 158 159 160
  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

161
  def keep_around_commits
162 163 164 165 166
    shas = [
      self.original_position.base_sha,
      self.original_position.start_sha,
      self.original_position.head_sha
    ]
167 168

    if self.position != self.original_position
169 170 171
      shas << self.position.base_sha
      shas << self.position.start_sha
      shas << self.position.head_sha
172
    end
173 174

    project.repository.keep_around(*shas)
175
  end
Douwe Maan's avatar
Douwe Maan committed
176
end