Commit daf46aad authored by Peter Leitzen's avatar Peter Leitzen

Allow enabling feature flag per project

parent e0934f66
Pipeline #55898435 failed with stages
in 173 minutes and 58 seconds
......@@ -597,9 +597,11 @@ module EE
import_state.set_next_execution_to_now
end
def licensed_feature_available?(feature, user = nil)
def licensed_feature_available?(feature, thing = nil)
thing ||= self
# This feature might not be behind a feature flag at all, so default to true
return false unless ::Feature.enabled?(feature, user, default_enabled: true)
return false unless ::Feature.enabled?(feature, thing, default_enabled: true)
available_features = strong_memoize(:licensed_feature_available) do
Hash.new do |h, f|
......@@ -611,7 +613,7 @@ module EE
end
def load_licensed_feature_available(feature)
globally_available = License.feature_available?(feature)
globally_available = License.feature_available?(feature, self)
if ::Gitlab::CurrentSettings.should_check_namespace_plan? && namespace
globally_available &&
......
......@@ -306,11 +306,11 @@ class License < ApplicationRecord
@features ||= (self.class.features_for_plan(plan) + features_from_add_ons).to_set
end
def feature_available?(feature)
def feature_available?(feature, thing = nil)
return false if trial? && expired?
# This feature might not be behind a feature flag at all, so default to true
return false unless ::Feature.enabled?(feature, default_enabled: true)
return false unless ::Feature.enabled?(feature, thing, default_enabled: true)
features.include?(feature)
end
......
......@@ -581,7 +581,7 @@ describe License do
end
end
context 'when feature is enabled by a feature flag' do
context 'when feature is enabled by a feature flag globally' do
it 'returns true' do
feature = license.features.first
stub_feature_flags(feature => true)
......@@ -589,6 +589,17 @@ describe License do
expect(license.feature_available?(feature)).to eq(true)
end
end
context 'when feature is enabled by a feature flag per project' do
it 'returns true' do
project = create(:project)
feature = license.features.first
stub_feature_flags(feature => { enabled: true, thing: project })
expect(license.feature_available?(feature)).to eq(false)
expect(license.feature_available?(feature, project)).to eq(true)
end
end
end
def build_license_with_add_ons(add_ons, plan: nil)
......
......@@ -21,6 +21,8 @@ module StubFeatureFlags
if thing
allow(Feature).to receive(:enabled?).with(feature_name, thing, any_args) { enabled }
allow(Feature).to receive(:enabled?).with(feature_name.to_s, thing, any_args) { enabled }
allow(Feature).to receive(:enabled?).with(feature_name, nil, any_args) { !enabled }
allow(Feature).to receive(:enabled?).with(feature_name.to_s, nil, any_args) { !enabled }
else
allow(Feature).to receive(:enabled?).with(feature_name, any_args) { enabled }
allow(Feature).to receive(:enabled?).with(feature_name.to_s, any_args) { enabled }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment