diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-10-14 00:34:44 +0300 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-10-19 19:58:26 +0300 |
commit | bf710b5119dce329a1ffd43b01b3a4dbb3e94b09 (patch) | |
tree | 3877c14e0163bf911b3b476561e591a0d5320a4b /lib | |
parent | 033ea9d1e80544df5236ca045c88f649e41afbc7 (diff) |
Validate label params against all labels available to project on the API
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/helpers.rb | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 67473f300c9..45120898b76 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -71,6 +71,10 @@ module API @project ||= find_project(params[:id]) end + def available_labels + @available_labels ||= LabelsFinder.new(current_user, project_id: user_project.id).execute + end + def find_project(id) project = Project.find_with_namespace(id) || Project.find_by(id: id) @@ -118,7 +122,7 @@ module API end 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') end @@ -197,16 +201,11 @@ module API def validate_label_params(params) errors = {} - if params[:labels].present? - params[:labels].split(',').each do |label_name| - label = user_project.labels.create_with( - color: Label::DEFAULT_COLOR).find_or_initialize_by( - title: label_name.strip) + params[:labels].to_s.split(',').each do |label_name| + label = available_labels.find_or_initialize_by(title: label_name.strip) + next if label.valid? - if label.invalid? - errors[label.title] = label.errors - end - end + errors[label.title] = label.errors end errors |