diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-06 12:09:20 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-06 12:09:20 +0300 |
commit | c203c40cda9df83cc33f9d69a24593ef4de1148d (patch) | |
tree | f13d457cff113e393e256132534296ec9e7ad2d7 /app/services | |
parent | 2553d13d9064a16e98720b4d25add589b1e43e28 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/groups/update_shared_runners_service.rb | 50 | ||||
-rw-r--r-- | app/services/issuable/bulk_update_service.rb | 53 |
2 files changed, 82 insertions, 21 deletions
diff --git a/app/services/groups/update_shared_runners_service.rb b/app/services/groups/update_shared_runners_service.rb new file mode 100644 index 00000000000..63f57104510 --- /dev/null +++ b/app/services/groups/update_shared_runners_service.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +module Groups + class UpdateSharedRunnersService < Groups::BaseService + def execute + return error('Operation not allowed', 403) unless can?(current_user, :admin_group, group) + + validate_params + + enable_or_disable_shared_runners! + allow_or_disallow_descendants_override_disabled_shared_runners! + + success + + rescue Group::UpdateSharedRunnersError => error + error(error.message) + end + + private + + def validate_params + if Gitlab::Utils.to_boolean(params[:shared_runners_enabled]) && !params[:allow_descendants_override_disabled_shared_runners].nil? + raise Group::UpdateSharedRunnersError, 'Cannot set shared_runners_enabled to true and allow_descendants_override_disabled_shared_runners' + end + end + + def enable_or_disable_shared_runners! + return if params[:shared_runners_enabled].nil? + + if Gitlab::Utils.to_boolean(params[:shared_runners_enabled]) + group.enable_shared_runners! + else + group.disable_shared_runners! + end + end + + def allow_or_disallow_descendants_override_disabled_shared_runners! + return if params[:allow_descendants_override_disabled_shared_runners].nil? + + # Needs to reset group because if both params are present could result in error + group.reset + + if Gitlab::Utils.to_boolean(params[:allow_descendants_override_disabled_shared_runners]) + group.allow_descendants_override_disabled_shared_runners! + else + group.disallow_descendants_override_disabled_shared_runners! + end + end + end +end diff --git a/app/services/issuable/bulk_update_service.rb b/app/services/issuable/bulk_update_service.rb index 2902385da4a..79be771b3fb 100644 --- a/app/services/issuable/bulk_update_service.rb +++ b/app/services/issuable/bulk_update_service.rb @@ -11,40 +11,29 @@ module Issuable end def execute(type) - model_class = type.classify.constantize - update_class = type.classify.pluralize.constantize::UpdateService - ids = params.delete(:issuable_ids).split(",") - items = find_issuables(parent, model_class, ids) + set_update_params(type) + items = update_issuables(type, ids) + response_success(payload: { count: items.count }) + rescue ArgumentError => e + response_error(e.message, 422) + end + + private + + def set_update_params(type) params.slice!(*permitted_attrs(type)) params.delete_if { |k, v| v.blank? } if params[:assignee_ids] == [IssuableFinder::Params::NONE.to_s] params[:assignee_ids] = [] end - - items.each do |issuable| - next unless can?(current_user, :"update_#{type}", issuable) - - update_class.new(issuable.issuing_parent, current_user, params).execute(issuable) - end - - { - count: items.count, - success: !items.count.zero? - } end - private - def permitted_attrs(type) attrs = %i(state_event milestone_id add_label_ids remove_label_ids subscription_event) - issuable_specific_attrs(type, attrs) - end - - def issuable_specific_attrs(type, attrs) if type == 'issue' || type == 'merge_request' attrs.push(:assignee_ids) else @@ -52,6 +41,20 @@ module Issuable end end + def update_issuables(type, ids) + model_class = type.classify.constantize + update_class = type.classify.pluralize.constantize::UpdateService + items = find_issuables(parent, model_class, ids) + + items.each do |issuable| + next unless can?(current_user, :"update_#{type}", issuable) + + update_class.new(issuable.issuing_parent, current_user, params).execute(issuable) + end + + items + end + def find_issuables(parent, model_class, ids) if parent.is_a?(Project) model_class.id_in(ids).of_projects(parent) @@ -59,6 +62,14 @@ module Issuable model_class.id_in(ids).of_projects(parent.all_projects) end end + + def response_success(message: nil, payload: nil) + ServiceResponse.success(message: message, payload: payload) + end + + def response_error(message, http_status) + ServiceResponse.error(message: message, http_status: http_status) + end end end |