Commit 2d75b454ef87a87bff942dc31837687919113468
1 parent
e0635297
Exists in
master
and in
4 other branches
Store diff with line note. It makes possible to see inline notes with proper dif…
…f even if MR diff changed
Showing
2 changed files
with
38 additions
and
8 deletions
Show diff stats
app/models/note.rb
@@ -50,6 +50,9 @@ class Note < ActiveRecord::Base | @@ -50,6 +50,9 @@ class Note < ActiveRecord::Base | ||
50 | scope :inc_author_project, ->{ includes(:project, :author) } | 50 | scope :inc_author_project, ->{ includes(:project, :author) } |
51 | scope :inc_author, ->{ includes(:author) } | 51 | scope :inc_author, ->{ includes(:author) } |
52 | 52 | ||
53 | + serialize :st_diff | ||
54 | + before_create :set_diff, if: ->(n) { n.noteable_type == 'MergeRequest' && n.line_code.present? } | ||
55 | + | ||
53 | def self.create_status_change_note(noteable, author, status) | 56 | def self.create_status_change_note(noteable, author, status) |
54 | create({ | 57 | create({ |
55 | noteable: noteable, | 58 | noteable: noteable, |
@@ -67,22 +70,33 @@ class Note < ActiveRecord::Base | @@ -67,22 +70,33 @@ class Note < ActiveRecord::Base | ||
67 | nil | 70 | nil |
68 | end | 71 | end |
69 | 72 | ||
70 | - def diff | ||
71 | - if noteable.diffs.present? | ||
72 | - noteable.diffs.select do |d| | ||
73 | - if d.new_path | ||
74 | - Digest::SHA1.hexdigest(d.new_path) == diff_file_index | ||
75 | - end | ||
76 | - end.first | 73 | + def find_diff |
74 | + return nil unless noteable.diffs.present? | ||
75 | + | ||
76 | + @diff ||= noteable.diffs.find do |d| | ||
77 | + Digest::SHA1.hexdigest(d.new_path) == diff_file_index if d.new_path | ||
77 | end | 78 | end |
78 | end | 79 | end |
79 | 80 | ||
81 | + def set_diff | ||
82 | + # First lets find notes with same diff | ||
83 | + # before iterating over all mr diffs | ||
84 | + diff = self.noteable.notes.where(line_code: self.line_code).last.try(:diff) | ||
85 | + diff ||= find_diff | ||
86 | + | ||
87 | + self.st_diff = diff.to_hash if diff | ||
88 | + end | ||
89 | + | ||
90 | + def diff | ||
91 | + @diff ||= Gitlab::Git::Diff.new(st_diff) if st_diff.respond_to?(:map) | ||
92 | + end | ||
93 | + | ||
80 | def diff_file_index | 94 | def diff_file_index |
81 | line_code.split('_')[0] | 95 | line_code.split('_')[0] |
82 | end | 96 | end |
83 | 97 | ||
84 | def diff_file_name | 98 | def diff_file_name |
85 | - diff.new_path | 99 | + diff.new_path if diff |
86 | end | 100 | end |
87 | 101 | ||
88 | def diff_new_line | 102 | def diff_new_line |
@@ -0,0 +1,16 @@ | @@ -0,0 +1,16 @@ | ||
1 | +desc "GITLAB | Migrate inline notes" | ||
2 | +task migrate_inline_notes: :environment do | ||
3 | + Note.where(noteable_type: 'MergeRequest').find_each(batch_size: 100) do |note| | ||
4 | + begin | ||
5 | + note.set_diff | ||
6 | + if note.save | ||
7 | + print '.' | ||
8 | + else | ||
9 | + print 'F' | ||
10 | + end | ||
11 | + rescue | ||
12 | + print 'F' | ||
13 | + end | ||
14 | + end | ||
15 | +end | ||
16 | + |