OSDN Git Service

Store diff with line note. It makes possible to see inline notes with proper diff...
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Sun, 4 Aug 2013 16:01:57 +0000 (19:01 +0300)
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Sun, 4 Aug 2013 16:01:57 +0000 (19:01 +0300)
app/models/note.rb
lib/tasks/migrate/migrate_inline_notes.rake [new file with mode: 0644]

index c23aab0..b0875a0 100644 (file)
@@ -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 (file)
index 0000000..ec33825
--- /dev/null
@@ -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
+