1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
# frozen_string_literal: true
module Integrations
class UpdateService
include ::Services::ReturnServiceResponses
include Gitlab::Utils::StrongMemoize
def initialize(current_user:, integration:, attributes:)
@current_user = current_user
@integration = integration
@attributes = attributes
end
def execute
return error('Integration not found.', :not_found) unless integration
if handle_inherited_settings?
handle_inherited_settings
else
handle_default_settings
end
end
private
attr_reader :current_user, :integration, :attributes
def handle_inherited_settings?
if attributes.key?(:use_inherited_settings)
Gitlab::Utils.to_boolean(attributes[:use_inherited_settings], default: false)
else
integration.inherit_from_id?
end
end
def default_integration
::Integration.default_integration(integration.type, integration.parent)
end
strong_memoize_attr :default_integration
def handle_inherited_settings
return error('Default integration not found.', :not_found) unless default_integration
integration.inherit_from_id = default_integration.id
unless integration.save(context: :manual_change)
return error("Failed to update integration. #{integration.errors.messages}", :bad_request)
end
if integration.project_level?
::Integrations::Propagation::BulkUpdateService.new(default_integration, [integration]).execute
end
success(integration)
end
def handle_default_settings
attributes.delete(:use_inherited_settings)
integration.inherit_from_id = nil
integration.attributes = attributes
if integration.save(context: :manual_change)
success(integration)
else
error("Failed to update integration. #{integration.errors.messages}", :bad_request)
end
end
end
end
|