Validate label params against all labels available to project on the API

parent 033ea9d1
...@@ -71,6 +71,10 @@ module API ...@@ -71,6 +71,10 @@ module API
@project ||= find_project(params[:id]) @project ||= find_project(params[:id])
end end
def available_labels
@available_labels ||= LabelsFinder.new(current_user, project_id: user_project.id).execute
end
def find_project(id) def find_project(id)
project = Project.find_with_namespace(id) || Project.find_by(id: id) project = Project.find_with_namespace(id) || Project.find_by(id: id)
...@@ -118,7 +122,7 @@ module API ...@@ -118,7 +122,7 @@ module API
end end
def find_project_label(id) def find_project_label(id)
label = user_project.labels.find_by_id(id) || user_project.labels.find_by_title(id) label = available_labels.find_by_id(id) || available_labels.find_by_title(id)
label || not_found!('Label') label || not_found!('Label')
end end
...@@ -197,16 +201,11 @@ module API ...@@ -197,16 +201,11 @@ module API
def validate_label_params(params) def validate_label_params(params)
errors = {} errors = {}
if params[:labels].present? params[:labels].to_s.split(',').each do |label_name|
params[:labels].split(',').each do |label_name| label = available_labels.find_or_initialize_by(title: label_name.strip)
label = user_project.labels.create_with( next if label.valid?
color: Label::DEFAULT_COLOR).find_or_initialize_by(
title: label_name.strip)
if label.invalid? errors[label.title] = label.errors
errors[label.title] = label.errors
end
end
end end
errors errors
......
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