diff options
author | John Jarvis <jarv@gitlab.com> | 2019-04-02 11:12:32 +0300 |
---|---|---|
committer | John Jarvis <jarv@gitlab.com> | 2019-04-02 11:12:32 +0300 |
commit | 69b65a6b745e74bba290787420a0017395fd7c25 (patch) | |
tree | c328963cbe5b340c58c88230d21f338c77462ebd /app/services | |
parent | 1b6fe3ae226e4c6f481c90c886e242fcd96ab11b (diff) | |
parent | 3e81a5baf25d6ecd9ad807a2b8f4238dcc598d5e (diff) |
Merge branch 'master' of dev.gitlab.org:gitlab/gitlabhq into jarv/dev-to-gitlab-2019-04-02
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/issuable_base_service.rb | 20 | ||||
-rw-r--r-- | app/services/labels/available_labels_service.rb | 60 | ||||
-rw-r--r-- | app/services/projects/detect_repository_languages_service.rb | 10 | ||||
-rw-r--r-- | app/services/projects/repository_languages_service.rb | 24 |
4 files changed, 105 insertions, 9 deletions
diff --git a/app/services/issuable_base_service.rb b/app/services/issuable_base_service.rb index 95bd83bb03c..04dfcfbc22d 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 def filter_labels_in_param(key) @@ -99,6 +103,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) @@ -116,10 +124,6 @@ class IssuableBaseService < BaseService new_label_ids.uniq 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 diff --git a/app/services/projects/detect_repository_languages_service.rb b/app/services/projects/detect_repository_languages_service.rb index 4a837a4fb6a..b020e4d9088 100644 --- a/app/services/projects/detect_repository_languages_service.rb +++ b/app/services/projects/detect_repository_languages_service.rb @@ -2,7 +2,7 @@ module Projects class DetectRepositoryLanguagesService < BaseService - attr_reader :detected_repository_languages, :programming_languages + attr_reader :programming_languages # rubocop: disable CodeReuse/ActiveRecord def execute @@ -25,6 +25,8 @@ module Projects RepositoryLanguage.table_name, detection.insertions(matching_programming_languages) ) + + set_detected_repository_languages end project.repository_languages.reload @@ -56,5 +58,11 @@ module Projects retry end # rubocop: enable CodeReuse/ActiveRecord + + def set_detected_repository_languages + return if project.detected_repository_languages? + + project.update_column(:detected_repository_languages, true) + end end end diff --git a/app/services/projects/repository_languages_service.rb b/app/services/projects/repository_languages_service.rb new file mode 100644 index 00000000000..e75851c7da4 --- /dev/null +++ b/app/services/projects/repository_languages_service.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module Projects + class RepositoryLanguagesService < BaseService + def execute + perform_language_detection unless project.detected_repository_languages? + persisted_repository_languages + end + + private + + def perform_language_detection + if persisted_repository_languages.blank? + ::DetectRepositoryLanguagesWorker.perform_async(project.id, current_user.id) + else + project.update_column(:detected_repository_languages, true) + end + end + + def persisted_repository_languages + project.repository_languages + end + end +end |