Commit 2d75b454 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Store diff with line note. It makes possible to see inline notes with proper...

Store diff with line note. It makes possible to see inline notes with proper diff even if MR diff changed
parent e0635297
...@@ -50,6 +50,9 @@ class Note < ActiveRecord::Base ...@@ -50,6 +50,9 @@ class Note < ActiveRecord::Base
scope :inc_author_project, ->{ includes(:project, :author) } scope :inc_author_project, ->{ includes(:project, :author) }
scope :inc_author, ->{ includes(:author) } scope :inc_author, ->{ includes(:author) }
serialize :st_diff
before_create :set_diff, if: ->(n) { n.noteable_type == 'MergeRequest' && n.line_code.present? }
def self.create_status_change_note(noteable, author, status) def self.create_status_change_note(noteable, author, status)
create({ create({
noteable: noteable, noteable: noteable,
...@@ -67,14 +70,25 @@ class Note < ActiveRecord::Base ...@@ -67,14 +70,25 @@ class Note < ActiveRecord::Base
nil nil
end end
def diff def find_diff
if noteable.diffs.present? return nil unless noteable.diffs.present?
noteable.diffs.select do |d|
if d.new_path @diff ||= noteable.diffs.find do |d|
Digest::SHA1.hexdigest(d.new_path) == diff_file_index Digest::SHA1.hexdigest(d.new_path) == diff_file_index if d.new_path
end
end end
end.first
def set_diff
# First lets find notes with same diff
# before iterating over all mr diffs
diff = self.noteable.notes.where(line_code: self.line_code).last.try(:diff)
diff ||= find_diff
self.st_diff = diff.to_hash if diff
end end
def diff
@diff ||= Gitlab::Git::Diff.new(st_diff) if st_diff.respond_to?(:map)
end end
def diff_file_index def diff_file_index
...@@ -82,7 +96,7 @@ class Note < ActiveRecord::Base ...@@ -82,7 +96,7 @@ class Note < ActiveRecord::Base
end end
def diff_file_name def diff_file_name
diff.new_path diff.new_path if diff
end end
def diff_new_line def diff_new_line
......
desc "GITLAB | Migrate inline notes"
task migrate_inline_notes: :environment do
Note.where(noteable_type: 'MergeRequest').find_each(batch_size: 100) do |note|
begin
note.set_diff
if note.save
print '.'
else
print 'F'
end
rescue
print 'F'
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment