summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouwe Maan <douwe@selenight.nl>2016-10-20 12:15:29 +0200
committerDouwe Maan <douwe@selenight.nl>2016-10-20 12:15:29 +0200
commit64e2d884d6c8d822ae6e7d4d26af054396b74921 (patch)
treeb824e78b950b1d4476ebeb230c056f9e548b3ab7
parent1f949c0a6b08563f3abcd9fd4c9e750c4097b44b (diff)
downloadgitlab-ce-labels-api.tar.gz
Return conflict error in label API when title is taken by group labellabels-api
-rw-r--r--app/models/project.rb4
-rw-r--r--lib/api/labels.rb6
-rw-r--r--spec/requests/api/labels_spec.rb15
3 files changed, 17 insertions, 8 deletions
diff --git a/app/models/project.rb b/app/models/project.rb
index 653c38322c5..2bd04547a58 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -1062,10 +1062,6 @@ class Project < ActiveRecord::Base
forks.count
end
- def find_label(name)
- labels.find_by(name: name)
- end
-
def origin_merge_requests
merge_requests.where(source_project_id: self.id)
end
diff --git a/lib/api/labels.rb b/lib/api/labels.rb
index 642e6345b9e..40d7f4a5151 100644
--- a/lib/api/labels.rb
+++ b/lib/api/labels.rb
@@ -29,8 +29,8 @@ module API
required_attributes! [:name, :color]
attrs = attributes_for_keys [:name, :color, :description]
- label = user_project.find_label(attrs[:name])
+ label = available_labels.find_by(title: attrs[:name])
conflict!('Label already exists') if label
label = user_project.labels.create(attrs)
@@ -54,7 +54,7 @@ module API
authorize! :admin_label, user_project
required_attributes! [:name]
- label = user_project.find_label(params[:name])
+ label = user_project.labels.find_by(title: params[:name])
not_found!('Label') unless label
label.destroy
@@ -75,7 +75,7 @@ module API
authorize! :admin_label, user_project
required_attributes! [:name]
- label = user_project.find_label(params[:name])
+ label = user_project.labels.find_by(title: params[:name])
not_found!('Label not found') unless label
attrs = attributes_for_keys [:new_name, :color, :description]
diff --git a/spec/requests/api/labels_spec.rb b/spec/requests/api/labels_spec.rb
index 1da9988978b..840112299bc 100644
--- a/spec/requests/api/labels_spec.rb
+++ b/spec/requests/api/labels_spec.rb
@@ -83,7 +83,20 @@ describe API::API, api: true do
expect(json_response['message']['title']).to eq(['is invalid'])
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),
name: 'label1',
color: '#FFAABB'