diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb index 0d765f9280eda65a55801bfe023b1981299b8488..8726379bf3ce4ebade27236bbc18f69aa134f39e 100644 --- a/lib/api/merge_requests.rb +++ b/lib/api/merge_requests.rb @@ -78,8 +78,8 @@ module API attrs = attributes_for_keys [:source_branch, :target_branch, :assignee_id, :title, :target_project_id, :description] # Validate label names in advance - if validate_label_params(params) - return render_api_error!('Label names invalid', 405) + if (errors = validate_label_params(params)).any? + render_api_error!({ labels: errors }, 400) end merge_request = ::MergeRequests::CreateService.new(user_project, current_user, attrs).execute @@ -117,15 +117,16 @@ module API authorize! :modify_merge_request, merge_request # Validate label names in advance - if validate_label_params(params) - return render_api_error!('Label names invalid', 405) + if (errors = validate_label_params(params)).any? + render_api_error!({ labels: errors }, 400) end merge_request = ::MergeRequests::UpdateService.new(user_project, current_user, attrs).execute(merge_request) if merge_request.valid? # Find or create labels and attach to issue - if params[:labels].present? + unless params[:labels].nil? + merge_request.remove_labels merge_request.add_labels_by_names(params[:labels].split(",")) end diff --git a/spec/requests/api/merge_requests_spec.rb b/spec/requests/api/merge_requests_spec.rb index 58cf7f139dc92f63462d6ff896e84cfc70b87279..06a25c5e3a5ae54eed3a13b3acd38bf7ae35a5c6 100644 --- a/spec/requests/api/merge_requests_spec.rb +++ b/spec/requests/api/merge_requests_spec.rb @@ -112,15 +112,16 @@ describe API::API, api: true do response.status.should == 400 end - it 'should return 405 on invalid label names' do + it 'should return 400 on invalid label names' do post api("/projects/#{project.id}/merge_requests", user), title: 'Test merge_request', source_branch: 'stable', target_branch: 'master', author: user, labels: 'label, ?' - response.status.should == 405 - json_response['message'].should == 'Label names invalid' + response.status.should == 400 + json_response['message']['labels']['?']['title'].should == + ['is invalid'] end end @@ -252,13 +253,13 @@ describe API::API, api: true do json_response['target_branch'].should == 'wiki' end - it 'should return 405 on invalid label names' do + it 'should return 400 on invalid label names' do put api("/projects/#{project.id}/merge_request/#{merge_request.id}", user), title: 'new issue', labels: 'label, ?' - response.status.should == 405 - json_response['message'].should == 'Label names invalid' + response.status.should == 400 + json_response['message']['labels']['?']['title'].should == ['is invalid'] end end