Class: Admin::SetFeatureFlagService

Inherits:
Object
  • Object
show all
Defined in:
app/services/admin/set_feature_flag_service.rb

Constant Summary collapse

UnknownOperationError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(feature_flag_name:, params:) ⇒ SetFeatureFlagService

Returns a new instance of SetFeatureFlagService.



7
8
9
10
11
12
# File 'app/services/admin/set_feature_flag_service.rb', line 7

def initialize(feature_flag_name:, params:)
  @name = feature_flag_name
  @target = Feature::Target.new(params)
  @params = params
  @force = params[:force]
end

Instance Method Details

#executeObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/admin/set_feature_flag_service.rb', line 14

def execute
  unless force
    error = validate_feature_flag_name
    return ServiceResponse.error(message: error, reason: :invalid_feature_flag) if error
  end

  if target.gate_specified?
    update_targets
  else
    update_global
  end

  feature_flag = Feature.get(name) # rubocop:disable Gitlab/AvoidFeatureGet

  ServiceResponse.success(payload: { feature_flag: feature_flag })
rescue Feature::InvalidOperation => e
  ServiceResponse.error(message: e.message, reason: :illegal_operation)
rescue UnknownOperationError => e
  ServiceResponse.error(message: e.message, reason: :illegal_operation)
rescue Feature::Target::UnknownTargetError => e
  ServiceResponse.error(message: e.message, reason: :actor_not_found)
end