Skip to content
Snippets Groups Projects
Verified Commit 7fe00dac authored by Jesse Hall's avatar Jesse Hall
Browse files

Fix for #222780, batch suggestions now accomodate suggestions that can add or remove lines.

parent cb0bfc12
No related branches found
No related tags found
No related merge requests found
Pipeline #157457944 canceled
......@@ -51,17 +51,24 @@ module Gitlab
@current_content ||= blob.nil? ? [''] : blob_data_lines
end
def sorted_suggestions
@sorted_suggestions ||= suggestions.sort_by(&:from_line_index)
end
def _new_content
offset = 0
current_content.tap do |content|
suggestions.each do |suggestion|
range = line_range(suggestion)
sorted_suggestions.each do |suggestion|
range = line_range(suggestion, offset)
content[range] = suggestion.to_content
offset = suggestion.to_line_index - suggestion.from_line_index
end
end.join
end
def line_range(suggestion)
suggestion.from_line_index..suggestion.to_line_index
def line_range(suggestion, offset = 0)
(suggestion.from_line_index - offset)..suggestion.to_line_index
end
def for_different_file?(suggestion)
......
......@@ -123,11 +123,25 @@ describe Suggestions::ApplyService do
let(:position) { build_position }
let(:position2) do
Gitlab::Diff::Position.new(old_path: "files/ruby/popen.rb",
new_path: "files/ruby/popen.rb",
old_line: nil,
new_line: 15,
diff_refs: merge_request
.diff_refs)
end
let(:diff_note) do
create(:diff_note_on_merge_request, noteable: merge_request,
position: position, project: project)
end
let(:diff_note2) do
create(:diff_note_on_merge_request, noteable: merge_request,
position: position2, project: project)
end
let(:suggestion) do
create(:suggestion, :content_from_repo, note: diff_note,
to_content: " raise RuntimeError, 'Explosion'\n # explosion?\n")
......@@ -387,14 +401,8 @@ describe Suggestions::ApplyService do
# multi
# line
vars = {
"PWD" => path
}
options = {
chdir: path
}
# multi_1
# line_2
unless File.directory?(path)
FileUtils.mkdir_p(path)
end
......@@ -427,7 +435,14 @@ describe Suggestions::ApplyService do
to_content: "# multi\n# line\n")
end
let(:suggestions) { [suggestion] }
let(:suggestion2) do
create(:suggestion, :content_from_repo, note: diff_note2,
lines_above: 1,
lines_below: 1,
to_content: "# multi_1\n# line_2\n")
end
let(:suggestions) { [suggestion, suggestion2] }
it_behaves_like 'successfully creates commit and updates suggestions'
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment