Skip to content
Snippets Groups Projects

Users can apply multiple suggestions at once.

Merged Jesse Hall requested to merge jessehall3/gitlab-ee:25486-batch-suggestions into master
Compare and Show latest version
1 file
+ 65
79
Compare changes
  • Side-by-side
  • Inline
@@ -7,8 +7,7 @@
let(:project) { create(:project, :repository) }
let(:merge_request) do
create(:merge_request, source_project: project,
target_project: project)
create(:merge_request, source_project: project, target_project: project)
end
let(:position) do
@@ -19,26 +18,45 @@
diff_refs: merge_request.diff_refs)
end
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: position,
project: project)
position: position2,
project: project)
end
let(:suggestion) do
create(:suggestion, note: diff_note,
from_content: " raise RuntimeError, \"System commands must be given as an array of strings\"\n",
to_content: " raise RuntimeError, 'Explosion'\n # explosion?")
end
let(:unappliable_suggestion) do
create(:suggestion, :unappliable, note: diff_note2)
end
describe "PUT /suggestions/:id/apply" do
let(:url) { "/suggestions/#{suggestion.id}/apply" }
context 'when successfully applies patch' do
let(:suggestion) do
create(:suggestion, note: diff_note,
from_content: " raise RuntimeError, \"System commands must be given as an array of strings\"\n",
to_content: " raise RuntimeError, 'Explosion'\n # explosion?")
end
it 'renders an ok response and returns json content' do
project.add_maintainer(user)
put api(url, user), params: { id: suggestion.id }
put api(url, user)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response)
@@ -48,14 +66,12 @@
end
context 'when not able to apply patch' do
let(:suggestion) do
create(:suggestion, :unappliable, note: diff_note)
end
let(:url) { "/suggestions/#{unappliable_suggestion.id}/apply" }
it 'renders a bad request error and returns json content' do
project.add_maintainer(user)
put api(url, user), params: { id: suggestion.id }
put api(url, user)
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response).to eq({ 'message' => 'A suggestion is not applicable.' })
@@ -68,7 +84,7 @@
it 'renders a not found error and returns json content' do
project.add_maintainer(user)
put api(url, user), params: { id: "foo-123" }
put api(url, user)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response).to eq({ 'message' => 'Suggestion is not applicable as the suggestion was not found.' })
@@ -76,16 +92,10 @@
end
context 'when unauthorized' do
let(:suggestion) do
create(:suggestion, note: diff_note,
from_content: " raise RuntimeError, \"System commands must be given as an array of strings\"\n",
to_content: " raise RuntimeError, 'Explosion'\n # explosion?")
end
it 'renders a forbidden error and returns json content' do
project.add_reporter(user)
put api(url, user), params: { id: suggestion.id }
put api(url, user)
expect(response).to have_gitlab_http_status(:forbidden)
expect(json_response).to eq({ 'message' => '403 Forbidden' })
@@ -94,35 +104,15 @@
end
describe "PUT /suggestions/batch_apply" do
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_note2) do
create(:diff_note_on_merge_request, noteable: merge_request,
position: position2,
project: project)
let(:suggestion2) do
create(:suggestion, note: diff_note2,
from_content: " \"PWD\" => path\n",
to_content: " *** FOO ***\n")
end
let(:url) { "/suggestions/batch_apply" }
context 'when successfully applies multiple patches as a batch' do
let(:suggestion) do
create(:suggestion, note: diff_note,
from_content: " raise RuntimeError, \"System commands must be given as an array of strings\"\n",
to_content: " raise RuntimeError, 'Explosion'\n # explosion?")
end
let(:suggestion2) do
create(:suggestion, note: diff_note2,
from_content: " \"PWD\" => path\n",
to_content: " *** FOO ***\n")
end
it 'renders an ok response and returns json content' do
project.add_maintainer(user)
@@ -136,22 +126,12 @@
end
end
context 'when not able to apply patches' do
let(:suggestion) do
create(:suggestion, note: diff_note,
from_content: " raise RuntimeError, \"System commands must be given as an array of strings\"\n",
to_content: " raise RuntimeError, 'Explosion'\n # explosion?")
end
let(:suggestion2) do
create(:suggestion, :unappliable, note: diff_note2)
end
context 'when not able to apply one or more of the patches' do
it 'renders a bad request error and returns json content' do
project.add_maintainer(user)
put api(url, user),
params: { ids: "#{suggestion.id},#{suggestion2.id}" }
params: { ids: "#{suggestion.id},#{unappliable_suggestion.id}" }
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response).to eq({ 'message' => 'A suggestion is not applicable.' })
@@ -159,14 +139,10 @@
end
context 'with missing suggestions' do
let(:suggestion2) do
create(:suggestion, :unappliable, note: diff_note2)
end
it 'renders a not found error and returns json content if any suggestion is not found' do
project.add_maintainer(user)
put api(url, user), params: { ids: "foo-123,#{suggestion2.id}" }
put api(url, user), params: { ids: "#{suggestion.id}, foo-123" }
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response)
@@ -185,19 +161,7 @@
end
context 'when unauthorized' do
let(:suggestion) do
create(:suggestion, note: diff_note,
from_content: " raise RuntimeError, \"System commands must be given as an array of strings\"\n",
to_content: " raise RuntimeError, 'Explosion'\n # explosion?")
end
let(:suggestion2) do
create(:suggestion, note: diff_note2,
from_content: " \"PWD\" => path\n",
to_content: " *** FOO ***\n")
end
it 'renders a forbidden error and returns json content for a single suggestion' do
it 'renders a forbidden error and returns json content' do
project.add_reporter(user)
put api(url, user),
@@ -206,12 +170,34 @@
expect(response).to have_gitlab_http_status(:forbidden)
expect(json_response).to eq({ 'message' => '403 Forbidden' })
end
end
# TODO: Fix test, does not work as desired.
context 'when not all suggestions belong to the same branch' do
it 'renders a forbidden error and return json content' do
merge_request2 = create(:merge_request,
:conflict,
source_project: project,
target_project: project)
position3 = 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_request2
.diff_refs)
diff_note3 = create(:diff_note_on_merge_request,
noteable: merge_request2,
position: position3,
project: project)
other_branch_suggestion = create(:suggestion, note: diff_note3)
it 'renders a forbidden error and returns json content for a batch of suggestions' do
project.add_reporter(user)
put api(url, user),
params: { ids: "#{suggestion.id},#{suggestion2.id}" }
params: { ids: "#{suggestion.id},#{other_branch_suggestion.id}" }
expect(response).to have_gitlab_http_status(:forbidden)
expect(json_response).to eq({ 'message' => '403 Forbidden' })
Loading