Commit d38dd3c4 authored by Rémy Coutable's avatar Rémy Coutable

Merge remote-tracking branch 'origin/labels-api'

See merge request !7014
Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parents d12e7647 64e2d884
...@@ -1067,10 +1067,6 @@ class Project < ActiveRecord::Base ...@@ -1067,10 +1067,6 @@ class Project < ActiveRecord::Base
forks.count forks.count
end end
def find_label(name)
labels.find_by(name: name)
end
def origin_merge_requests def origin_merge_requests
merge_requests.where(source_project_id: self.id) merge_requests.where(source_project_id: self.id)
end end
......
...@@ -25,7 +25,7 @@ module API ...@@ -25,7 +25,7 @@ module API
post ':id/labels' do post ':id/labels' do
authorize! :admin_label, user_project authorize! :admin_label, user_project
label = user_project.find_label(params[:name]) label = available_labels.find_by(title: params[:name])
conflict!('Label already exists') if label conflict!('Label already exists') if label
label = user_project.labels.create(declared(params, include_parent_namespaces: false).to_h) label = user_project.labels.create(declared(params, include_parent_namespaces: false).to_h)
...@@ -46,7 +46,7 @@ module API ...@@ -46,7 +46,7 @@ module API
delete ':id/labels' do delete ':id/labels' do
authorize! :admin_label, user_project authorize! :admin_label, user_project
label = user_project.find_label(params[:name]) label = user_project.labels.find_by(title: params[:name])
not_found!('Label') unless label not_found!('Label') unless label
present label.destroy, with: Entities::Label, current_user: current_user present label.destroy, with: Entities::Label, current_user: current_user
...@@ -65,7 +65,7 @@ module API ...@@ -65,7 +65,7 @@ module API
put ':id/labels' do put ':id/labels' do
authorize! :admin_label, user_project authorize! :admin_label, user_project
label = user_project.find_label(params[:name]) label = user_project.labels.find_by(title: params[:name])
not_found!('Label not found') unless label not_found!('Label not found') unless label
update_params = declared(params, update_params = declared(params,
......
...@@ -82,7 +82,20 @@ describe API::API, api: true do ...@@ -82,7 +82,20 @@ describe API::API, api: true do
expect(json_response['message']['title']).to eq(['is invalid']) expect(json_response['message']['title']).to eq(['is invalid'])
end end
it 'returns 409 if label already exists' do it 'returns 409 if label already exists in group' do
group = create(:group)
group_label = create(:group_label, group: group)
project.update(group: group)
post api("/projects/#{project.id}/labels", user),
name: group_label.name,
color: '#FFAABB'
expect(response).to have_http_status(409)
expect(json_response['message']).to eq('Label already exists')
end
it 'returns 409 if label already exists in project' do
post api("/projects/#{project.id}/labels", user), post api("/projects/#{project.id}/labels", user),
name: 'label1', name: 'label1',
color: '#FFAABB' color: '#FFAABB'
......
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