diff_note.rb 4.08 KB
Newer Older
Douwe Maan's avatar
Douwe Maan committed
1 2 3 4 5 6
class DiffNote < Note
  include NoteOnDiff

  serialize :original_position, Gitlab::Diff::Position
  serialize :position, Gitlab::Diff::Position

7 8
  belongs_to :resolved_by, class_name: "User"

Douwe Maan's avatar
Douwe Maan committed
9 10 11 12 13
  validates :original_position, presence: true
  validates :position, presence: true
  validates :diff_line, presence: true
  validates :line_code, presence: true, line_code: true
  validates :noteable_type, inclusion: { in: ['Commit', 'MergeRequest'] }
14
  validates :resolved_by, presence: true, if: :resolved?
Douwe Maan's avatar
Douwe Maan committed
15 16 17
  validate :positions_complete
  validate :verify_supported

18
  before_validation :set_original_position, :update_position, on: :create
Douwe Maan's avatar
Douwe Maan committed
19
  before_validation :set_line_code
20
  after_save :keep_around_commits
Douwe Maan's avatar
Douwe Maan committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

  class << self
    def build_discussion_id(noteable_type, noteable_id, position)
      [super(noteable_type, noteable_id), *position.key].join("-")
    end
  end

  def new_diff_note?
    true
  end

  def diff_attributes
    { position: position.to_json }
  end

  def discussion_id
Douwe Maan's avatar
Douwe Maan committed
37
    @discussion_id ||= Digest::SHA1.hexdigest(self.class.build_discussion_id(noteable_type, noteable_id || commit_id, position))
Douwe Maan's avatar
Douwe Maan committed
38 39 40
  end

  def original_discussion_id
Douwe Maan's avatar
Douwe Maan committed
41
    @original_discussion_id ||= Digest::SHA1.hexdigest(self.class.build_discussion_id(noteable_type, noteable_id || commit_id, original_position))
Douwe Maan's avatar
Douwe Maan committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  end

  def position=(new_position)
    if new_position.is_a?(String)
      new_position = JSON.parse(new_position) rescue nil
    end

    if new_position.is_a?(Hash)
      new_position = new_position.with_indifferent_access
      new_position = Gitlab::Diff::Position.new(new_position)
    end

    super(new_position)
  end

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

  def diff_line
    @diff_line ||= diff_file.line_for_position(self.original_position) if diff_file
  end

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

  def active?(diff_refs = nil)
    return false unless supported?
    return true if for_commit?

    diff_refs ||= self.noteable.diff_refs

    self.position.diff_refs == diff_refs
  end

78
  def resolvable?
Douwe Maan's avatar
Douwe Maan committed
79
    !system? && for_merge_request?
80 81 82 83 84 85 86 87
  end

  def resolved?
    return false unless resolvable?

    self.resolved_at.present?
  end

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  def resolve!(current_user)
    return unless resolvable?
    return if resolved?

    self.resolved_at = Time.now
    self.resolved_by = current_user
    save!
  end

  def unresolve!
    return unless resolvable?
    return unless resolved?

    self.resolved_at = nil
    self.resolved_by = nil
    save!
  end

Douwe Maan's avatar
Douwe Maan committed
106 107 108 109 110 111 112 113 114 115 116
  def discussion
    return unless resolvable?

    discussion_notes = self.noteable.notes.fresh.select { |n| n.discussion_id == self.discussion_id }
    Discussion.new(discussion_notes)
  end

  def as_discussion
    Discussion.new([self])
  end

117 118 119
  private

  def supported?
Douwe Maan's avatar
Douwe Maan committed
120
    for_commit? || self.noteable.support_new_diff_notes?
121 122 123 124 125 126 127 128 129 130
  end

  def set_original_position
    self.original_position = self.position.dup
  end

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

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  def update_position
    return unless supported?
    return if for_commit?

    return if active?

    Notes::DiffPositionUpdateService.new(
      self.project,
      nil,
      old_diff_refs: self.position.diff_refs,
      new_diff_refs: self.noteable.diff_refs,
      paths: self.position.paths
    ).execute(self)
  end

Douwe Maan's avatar
Douwe Maan committed
146 147 148 149 150 151 152 153 154 155 156
  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
157 158 159 160 161 162 163 164 165 166 167 168

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