Commit 9dcf1c12 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'pedropombeiro/321368/allow-limitable-to-use-override-ff' into 'master'

Allow Limitable to use an override feature flag

See merge request gitlab-org/gitlab!67151
parents 320e1896 d9d4ac4a
...@@ -9,6 +9,7 @@ module Limitable ...@@ -9,6 +9,7 @@ module Limitable
class_attribute :limit_relation class_attribute :limit_relation
class_attribute :limit_name class_attribute :limit_name
class_attribute :limit_feature_flag class_attribute :limit_feature_flag
class_attribute :limit_feature_flag_for_override # Allows selectively disabling by actor (as per https://docs.gitlab.com/ee/development/feature_flags/#selectively-disable-by-actor)
self.limit_name = self.name.demodulize.tableize self.limit_name = self.name.demodulize.tableize
validate :validate_plan_limit_not_exceeded, on: :create validate :validate_plan_limit_not_exceeded, on: :create
...@@ -28,6 +29,7 @@ module Limitable ...@@ -28,6 +29,7 @@ module Limitable
scope_relation = self.public_send(limit_scope) # rubocop:disable GitlabSecurity/PublicSend scope_relation = self.public_send(limit_scope) # rubocop:disable GitlabSecurity/PublicSend
return unless scope_relation return unless scope_relation
return if limit_feature_flag && ::Feature.disabled?(limit_feature_flag, scope_relation, default_enabled: :yaml) return if limit_feature_flag && ::Feature.disabled?(limit_feature_flag, scope_relation, default_enabled: :yaml)
return if limit_feature_flag_for_override && ::Feature.enabled?(limit_feature_flag_for_override, scope_relation, default_enabled: :yaml)
relation = limit_relation ? self.public_send(limit_relation) : self.class.where(limit_scope => scope_relation) # rubocop:disable GitlabSecurity/PublicSend relation = limit_relation ? self.public_send(limit_relation) : self.class.where(limit_scope => scope_relation) # rubocop:disable GitlabSecurity/PublicSend
limits = scope_relation.actual_limits limits = scope_relation.actual_limits
......
...@@ -32,6 +32,7 @@ module RuboCop ...@@ -32,6 +32,7 @@ module RuboCop
SELF_METHODS = %i[ SELF_METHODS = %i[
push_frontend_feature_flag push_frontend_feature_flag
limit_feature_flag= limit_feature_flag=
limit_feature_flag_for_override=
].freeze + EXPERIMENT_METHODS + RUGGED_METHODS + WORKER_METHODS ].freeze + EXPERIMENT_METHODS + RUGGED_METHODS + WORKER_METHODS
RESTRICT_ON_SEND = FEATURE_METHODS + EXPERIMENTATION_METHODS + GRAPHQL_METHODS + SELF_METHODS RESTRICT_ON_SEND = FEATURE_METHODS + EXPERIMENTATION_METHODS + GRAPHQL_METHODS + SELF_METHODS
......
# frozen_string_literal: true # frozen_string_literal: true
require 'fast_spec_helper' require 'spec_helper'
require 'active_model'
RSpec.describe Limitable do RSpec.describe Limitable do
let(:minimal_test_class) do let(:minimal_test_class) do
...@@ -17,7 +16,7 @@ RSpec.describe Limitable do ...@@ -17,7 +16,7 @@ RSpec.describe Limitable do
end end
before do before do
stub_const("MinimalTestClass", minimal_test_class) stub_const('MinimalTestClass', minimal_test_class)
end end
it { expect(MinimalTestClass.limit_name).to eq('test_classes') } it { expect(MinimalTestClass.limit_name).to eq('test_classes') }
...@@ -37,25 +36,50 @@ RSpec.describe Limitable do ...@@ -37,25 +36,50 @@ RSpec.describe Limitable do
instance.valid?(:create) instance.valid?(:create)
end end
context 'with custom relation' do context 'with custom relation and feature flags' do
before do using RSpec::Parameterized::TableSyntax
MinimalTestClass.limit_relation = :custom_relation
where(:limit_feature_flag, :limit_feature_flag_value, :limit_feature_flag_for_override, :limit_feature_flag_override_value, :expect_limit_applied?) do
nil | nil | nil | nil | true
:some_feature_flag | false | nil | nil | false
:some_feature_flag | true | nil | nil | true
:some_feature_flag | true | :some_feature_flag_disable | false | true
:some_feature_flag | false | :some_feature_flag_disable | false | false
:some_feature_flag | false | :some_feature_flag_disable | true | false
:some_feature_flag | true | :some_feature_flag_disable | true | false
end end
it 'triggers custom limit_relation' do with_them do
instance = MinimalTestClass.new let(:instance) { MinimalTestClass.new }
def instance.project before do
@project ||= Object.new def instance.project
end @project ||= stub_feature_flag_gate('CustomActor')
end
stub_feature_flags("#{limit_feature_flag}": limit_feature_flag_value ? [instance.project] : false) if limit_feature_flag
stub_feature_flags("#{limit_feature_flag_for_override}": limit_feature_flag_override_value ? [instance.project] : false) if limit_feature_flag_for_override
skip_feature_flags_yaml_validation
skip_default_enabled_yaml_check
limits = Object.new MinimalTestClass.limit_relation = :custom_relation
custom_relation = Object.new MinimalTestClass.limit_feature_flag = limit_feature_flag
expect(instance).to receive(:custom_relation).and_return(custom_relation) MinimalTestClass.limit_feature_flag_for_override = limit_feature_flag_for_override
expect(instance.project).to receive(:actual_limits).and_return(limits) end
expect(limits).to receive(:exceeded?).with(instance.class.name.demodulize.tableize, custom_relation).and_return(false)
instance.valid?(:create) it 'acts according to the feature flag settings' do
limits = Object.new
custom_relation = Object.new
if expect_limit_applied?
expect(instance).to receive(:custom_relation).and_return(custom_relation)
expect(instance.project).to receive(:actual_limits).and_return(limits)
expect(limits).to receive(:exceeded?).with(instance.class.name.demodulize.tableize, custom_relation).and_return(false)
else
expect(instance).not_to receive(:custom_relation)
end
instance.valid?(:create)
end
end end
end end
end end
......
...@@ -194,6 +194,10 @@ RSpec.describe RuboCop::Cop::Gitlab::MarkUsedFeatureFlags do ...@@ -194,6 +194,10 @@ RSpec.describe RuboCop::Cop::Gitlab::MarkUsedFeatureFlags do
include_examples 'sets flag as used', 'self.limit_feature_flag = :foo', 'foo' include_examples 'sets flag as used', 'self.limit_feature_flag = :foo', 'foo'
end end
describe 'self.limit_feature_flag_for_override = :foo' do
include_examples 'sets flag as used', 'self.limit_feature_flag_for_override = :foo', 'foo'
end
describe 'FEATURE_FLAG = :foo' do describe 'FEATURE_FLAG = :foo' do
include_examples 'sets flag as used', 'FEATURE_FLAG = :foo', 'foo' include_examples 'sets flag as used', 'FEATURE_FLAG = :foo', 'foo'
end end
......
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