diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-17 14:59:07 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-17 14:59:07 +0300 |
commit | 8b573c94895dc0ac0e1d9d59cf3e8745e8b539ca (patch) | |
tree | 544930fb309b30317ae9797a9683768705d664c4 /lib/feature/definition.rb | |
parent | 4b1de649d0168371549608993deac953eb692019 (diff) |
Add latest changes from gitlab-org/gitlab@13-7-stable-eev13.7.0-rc42
Diffstat (limited to 'lib/feature/definition.rb')
-rw-r--r-- | lib/feature/definition.rb | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/lib/feature/definition.rb b/lib/feature/definition.rb index 0ba1bdc4799..8d9b2fa5234 100644 --- a/lib/feature/definition.rb +++ b/lib/feature/definition.rb @@ -13,6 +13,12 @@ class Feature end end + TYPES.each do |type, _| + define_method("#{type}?") do + attributes[:type].to_sym == type + end + end + def initialize(path, opts = {}) @path = path @attributes = {} @@ -65,9 +71,7 @@ class Feature "a valid syntax: #{TYPES.dig(type, :example)}" end - # We accept an array of defaults as some features are undefined - # and have `default_enabled: true/false` - unless Array(default_enabled).include?(default_enabled_in_code) + unless default_enabled_in_code == :yaml || default_enabled == default_enabled_in_code # Raise exception in test and dev raise Feature::InvalidFeatureFlagError, "The `default_enabled:` of `#{key}` is not equal to config: " \ "#{default_enabled_in_code} vs #{default_enabled}. Ensure to update #{path}" @@ -90,12 +94,20 @@ class Feature @definitions ||= load_all! end + def get(key) + definitions[key.to_sym] + end + def reload! @definitions = load_all! end + def has_definition?(key) + definitions.has_key?(key.to_sym) + end + def valid_usage!(key, type:, default_enabled:) - if definition = definitions[key.to_sym] + if definition = get(key) definition.valid_usage!(type_in_code: type, default_enabled_in_code: default_enabled) elsif type_definition = self::TYPES[type] raise InvalidFeatureFlagError, "Missing feature definition for `#{key}`" unless type_definition[:optional] @@ -104,6 +116,17 @@ class Feature end end + def default_enabled?(key) + if definition = get(key) + definition.default_enabled + else + Gitlab::ErrorTracking.track_and_raise_for_dev_exception( + InvalidFeatureFlagError.new("The feature flag YAML definition for '#{key}' does not exist")) + + false + end + end + def register_hot_reloader! # Reload feature flags on change of this file or any `.yml` file_watcher = Rails.configuration.file_watcher.new(reload_files, reload_directories) do @@ -119,10 +142,6 @@ class Feature private def load_all! - # We currently do not load feature flag definitions - # in production environments - return [] unless Gitlab.dev_or_test_env? - paths.each_with_object({}) do |glob_path, definitions| load_all_from_path!(definitions, glob_path) end |