BigW Consortium Gitlab

Commit 413952ff by Sebastian Ziebell

Creating or updating a MR returns more informative status codes.

Using the API library to create or update a merge request at the moment a 404 error is returned. This is fine when the merge request in question does not exist, but does not provide good information that for example a required attribute is missing. A status code of 400 (Bad request) is returned when creating or updating a merge request when either `source_branch` or `target_branch` is missing. A status code of 409 is returned when `source_branch` and `target_branch` are the same. Tests are added for these cases.
parent 566de5ab
...@@ -60,6 +60,13 @@ module Gitlab ...@@ -60,6 +60,13 @@ module Gitlab
merge_request.reload_code merge_request.reload_code
present merge_request, with: Entities::MergeRequest present merge_request, with: Entities::MergeRequest
else else
if merge_request.errors[:target_branch].any?
error!(merge_request.errors[:target_branch], 400)
elsif merge_request.errors[:source_branch].any?
error!(merge_request.errors[:source_branch], 400)
elsif merge_request.errors[:base].any?
error!(merge_request.errors[:base], 422)
end
not_found! not_found!
end end
end end
...@@ -88,6 +95,13 @@ module Gitlab ...@@ -88,6 +95,13 @@ module Gitlab
merge_request.mark_as_unchecked merge_request.mark_as_unchecked
present merge_request, with: Entities::MergeRequest present merge_request, with: Entities::MergeRequest
else else
if merge_request.errors[:target_branch].any?
error!(merge_request.errors[:target_branch], 400)
elsif merge_request.errors[:source_branch].any?
error!(merge_request.errors[:source_branch], 400)
elsif merge_request.errors[:base].any?
error!(merge_request.errors[:base], 422)
end
not_found! not_found!
end end
end end
......
...@@ -41,6 +41,24 @@ describe Gitlab::API do ...@@ -41,6 +41,24 @@ describe Gitlab::API do
response.status.should == 201 response.status.should == 201
json_response['title'].should == 'Test merge_request' json_response['title'].should == 'Test merge_request'
end end
it "should return 422 when source_branch equals target_branch" do
post api("/projects/#{project.id}/merge_requests", user),
title: "Test merge_request", source_branch: "master", target_branch: "master", author: user
response.status.should == 422
end
it "should return 400 when source_branch is missing" do
post api("/projects/#{project.id}/merge_requests", user),
title: "Test merge_request", target_branch: "master", author: user
response.status.should == 400
end
it "should return 400 when target_branch is missing" do
post api("/projects/#{project.id}/merge_requests", user),
title: "Test merge_request", source_branch: "stable", author: user
response.status.should == 400
end
end end
describe "PUT /projects/:id/merge_request/:merge_request_id" do describe "PUT /projects/:id/merge_request/:merge_request_id" do
...@@ -49,6 +67,18 @@ describe Gitlab::API do ...@@ -49,6 +67,18 @@ describe Gitlab::API do
response.status.should == 200 response.status.should == 200
json_response['title'].should == 'New title' json_response['title'].should == 'New title'
end end
it "should return 422 when source_branch and target_branch are renamed the same" do
put api("/projects/#{project.id}/merge_request/#{merge_request.id}", user),
source_branch: "master", target_branch: "master"
response.status.should == 422
end
it "should return merge_request with renamed target_branch" do
put api("/projects/#{project.id}/merge_request/#{merge_request.id}", user), target_branch: "test"
response.status.should == 200
json_response['target_branch'].should == 'test'
end
end end
describe "POST /projects/:id/merge_request/:merge_request_id/comments" do describe "POST /projects/:id/merge_request/:merge_request_id/comments" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment