2020-08-11 15:10:08 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Extend the Feature class with the ability to stub feature flags.
|
|
|
|
module StubbedFeature
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2020-10-14 12:08:58 +00:00
|
|
|
prepended do
|
|
|
|
cattr_reader(:persist_used) do
|
|
|
|
# persist feature flags in CI
|
|
|
|
# nil: indicates that we do not want to persist used feature flags
|
|
|
|
Gitlab::Utils.to_boolean(ENV['CI']) ? {} : nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-11 15:10:08 +00:00
|
|
|
class_methods do
|
|
|
|
# Turn stubbed feature flags on or off.
|
|
|
|
def stub=(stub)
|
|
|
|
@stub = stub
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub?
|
|
|
|
@stub.nil? ? true : @stub
|
|
|
|
end
|
|
|
|
|
|
|
|
# Wipe any previously set feature flags.
|
|
|
|
def reset_flipper
|
|
|
|
@flipper = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Replace #flipper method with the optional stubbed/unstubbed version.
|
|
|
|
def flipper
|
|
|
|
if stub?
|
|
|
|
@flipper ||= Flipper.new(Flipper::Adapters::Memory.new)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Replace #enabled? method with the optional stubbed/unstubbed version.
|
2020-10-05 09:08:17 +00:00
|
|
|
def enabled?(*args, **kwargs)
|
|
|
|
feature_flag = super
|
2020-08-11 15:10:08 +00:00
|
|
|
return feature_flag unless stub?
|
|
|
|
|
2020-10-14 12:08:58 +00:00
|
|
|
persist_used!(args.first)
|
|
|
|
|
2020-08-11 15:10:08 +00:00
|
|
|
# If feature flag is not persisted we mark the feature flag as enabled
|
|
|
|
# We do `m.call` as we want to validate the execution of method arguments
|
|
|
|
# and a feature flag state if it is not persisted
|
|
|
|
unless Feature.persisted_name?(args.first)
|
2020-09-08 12:08:41 +00:00
|
|
|
feature_flag = true
|
2020-08-11 15:10:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
feature_flag
|
|
|
|
end
|
2020-10-14 12:08:58 +00:00
|
|
|
|
|
|
|
# This method creates a temporary file in `tmp/feature_flags`
|
|
|
|
# if feature flag was touched during execution
|
|
|
|
def persist_used!(name)
|
|
|
|
return unless persist_used
|
|
|
|
return if persist_used[name]
|
|
|
|
|
|
|
|
persist_used[name] = true
|
|
|
|
FileUtils.touch(
|
|
|
|
Rails.root.join('tmp', 'feature_flags', name.to_s + ".used")
|
|
|
|
)
|
|
|
|
end
|
2020-08-11 15:10:08 +00:00
|
|
|
end
|
|
|
|
end
|