diff options
author | Jarka Košanová <jarka@gitlab.com> | 2019-02-12 09:35:34 +0100 |
---|---|---|
committer | Jarka Košanová <jarka@gitlab.com> | 2019-03-28 12:06:31 +0100 |
commit | 96153f2055dc0bcd2ee8b34a36f33529f6ccc50e (patch) | |
tree | 3ff4d56cedc85b600ae3c66bf0c851769fdbc4fd /app | |
parent | c29a92fc31acaefcc60abf1c9354d148ffc6985c (diff) | |
download | gitlab-ce-96153f2055dc0bcd2ee8b34a36f33529f6ccc50e.tar.gz |
Check if labels are available for target issuable
- labels have to be in the same project/group
as an issuable
Diffstat (limited to 'app')
-rw-r--r-- | app/models/label.rb | 4 | ||||
-rw-r--r-- | app/services/issuable_base_service.rb | 20 | ||||
-rw-r--r-- | app/services/labels/available_labels_service.rb | 60 |
3 files changed, 76 insertions, 8 deletions
diff --git a/app/models/label.rb b/app/models/label.rb index 96bdb7f17c5..e13a2322c6b 100644 --- a/app/models/label.rb +++ b/app/models/label.rb @@ -133,6 +133,10 @@ class Label < ActiveRecord::Base 1 end + def self.by_ids(ids) + where(id: ids) + end + def open_issues_count(user = nil) issues_count(user, state: 'opened') end diff --git a/app/services/issuable_base_service.rb b/app/services/issuable_base_service.rb index f35ad2a9d8b..49739083868 100644 --- a/app/services/issuable_base_service.rb +++ b/app/services/issuable_base_service.rb @@ -70,10 +70,14 @@ class IssuableBaseService < BaseService end def filter_labels - filter_labels_in_param(:add_label_ids) - filter_labels_in_param(:remove_label_ids) - filter_labels_in_param(:label_ids) - find_or_create_label_ids + params[:add_label_ids] = labels_service.filter_labels_ids_in_param(:add_label_ids) if params[:add_label_ids] + params[:remove_label_ids] = labels_service.filter_labels_ids_in_param(:remove_label_ids) if params[:remove_label_ids] + + if params[:label_ids] + params[:label_ids] = labels_service.filter_labels_ids_in_param(:label_ids) + elsif params[:labels] + params[:label_ids] = labels_service.find_or_create_by_titles.map(&:id) + end end # rubocop: disable CodeReuse/ActiveRecord @@ -101,6 +105,10 @@ class IssuableBaseService < BaseService end.compact end + def labels_service + @labels_service ||= ::Labels::AvailableLabelsService.new(current_user, parent, params) + end + def process_label_ids(attributes, existing_label_ids: nil) label_ids = attributes.delete(:label_ids) add_label_ids = attributes.delete(:add_label_ids) @@ -118,10 +126,6 @@ class IssuableBaseService < BaseService new_label_ids end - def available_labels - @available_labels ||= LabelsFinder.new(current_user, project_id: @project.id, include_ancestor_groups: true).execute - end - def handle_quick_actions_on_create(issuable) merge_quick_actions_into_params!(issuable) end diff --git a/app/services/labels/available_labels_service.rb b/app/services/labels/available_labels_service.rb new file mode 100644 index 00000000000..fe477d96970 --- /dev/null +++ b/app/services/labels/available_labels_service.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true +module Labels + class AvailableLabelsService + attr_reader :current_user, :parent, :params + + def initialize(current_user, parent, params) + @current_user = current_user + @parent = parent + @params = params + end + + def find_or_create_by_titles + labels = params.delete(:labels) + + return [] unless labels + + labels = labels.split(',') if labels.is_a?(String) + + labels.map do |label_name| + label = Labels::FindOrCreateService.new( + current_user, + parent, + include_ancestor_groups: true, + title: label_name.strip, + available_labels: available_labels + ).execute + + label + end.compact + end + + def filter_labels_ids_in_param(key) + return [] if params[key].to_a.empty? + + # rubocop:disable CodeReuse/ActiveRecord + available_labels.by_ids(params[key]).pluck(:id) + # rubocop:enable CodeReuse/ActiveRecord + end + + private + + def available_labels + @available_labels ||= LabelsFinder.new(current_user, finder_params).execute + end + + def finder_params + params = { include_ancestor_groups: true } + + case parent + when Group + params[:group_id] = parent.id + params[:only_group_labels] = true + when Project + params[:project_id] = parent.id + end + + params + end + end +end |