diff options
-rw-r--r-- | app/models/note.rb | 30 | ||||
-rw-r--r-- | lib/tasks/migrate/migrate_inline_notes.rake | 16 |
2 files changed, 38 insertions, 8 deletions
diff --git a/app/models/note.rb b/app/models/note.rb index c23aab03bcc..b0875a0761b 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -50,6 +50,9 @@ class Note < ActiveRecord::Base scope :inc_author_project, ->{ includes(:project, :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) create({ noteable: noteable, @@ -67,22 +70,33 @@ class Note < ActiveRecord::Base nil end - def diff - if noteable.diffs.present? - noteable.diffs.select do |d| - if d.new_path - Digest::SHA1.hexdigest(d.new_path) == diff_file_index - end - end.first + def find_diff + return nil unless noteable.diffs.present? + + @diff ||= noteable.diffs.find do |d| + Digest::SHA1.hexdigest(d.new_path) == diff_file_index if d.new_path end end + 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 + + def diff + @diff ||= Gitlab::Git::Diff.new(st_diff) if st_diff.respond_to?(:map) + end + def diff_file_index line_code.split('_')[0] end def diff_file_name - diff.new_path + diff.new_path if diff end def diff_new_line diff --git a/lib/tasks/migrate/migrate_inline_notes.rake b/lib/tasks/migrate/migrate_inline_notes.rake new file mode 100644 index 00000000000..ec338259abc --- /dev/null +++ b/lib/tasks/migrate/migrate_inline_notes.rake @@ -0,0 +1,16 @@ +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 + |