OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
allow forcing a feature flag to be active
ulfertse910d42ed6ff
diff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rbindex e1241b4d3ca..d9f41207843 100644--- a/config/constants/settings/definition.rb+++ b/config/constants/settings/definition.rb@@ -1334,6 +1334,8 @@ module Settings# @param [nil] env_alias Alternative for the default env name to also look up. E.g. with the alias set to# `OPENPROJECT_2FA` for a definition with the name `two_factor_authentication`, the value is fetched# from the ENV OPENPROJECT_2FA as well.+ # @param [TrueClass|FalseClass] disallow_override Disables the usual possibility of overriding the value+ # from ENV or configuration file.def add(name,default:,default_by_env: {},@@ -1342,7 +1344,8 @@ module Settingswritable: true,allowed: nil,env_alias: nil,- string_values: false)+ string_values: false,+ disallow_override: false)name = name.to_symreturn if exists?(name)@@ -1355,7 +1358,7 @@ module Settingsallowed:,env_alias:,string_values:)- override_value(definition)+ override_value(definition) unless disallow_overrideall[name] = definitionenddiff --git a/lib_static/open_project/feature_decisions.rb b/lib_static/open_project/feature_decisions.rbindex b81da25a09f..317d3e2d72e 100644--- a/lib_static/open_project/feature_decisions.rb+++ b/lib_static/open_project/feature_decisions.rb@@ -60,10 +60,10 @@ module OpenProjectmodule FeatureDecisionsmodule_function- def add(flag_name, description: nil)+ def add(flag_name, description: nil, force_active: false)all << flag_namedefine_flag_methods(flag_name)- define_setting_definition(flag_name, description:)+ define_setting_definition(flag_name, description:, force_active:)enddef active@@ -80,10 +80,12 @@ module OpenProjectendend- def define_setting_definition(flag_name, description: nil)+ def define_setting_definition(flag_name, description: nil, force_active: false)Settings::Definition.add :"feature_#{flag_name}_active",description:,- default: Rails.env.development?+ default: force_active || Rails.env.development?,+ writable: !force_active,+ disallow_override: force_activeendendenddiff --git a/spec/constants/settings/definition_spec.rb b/spec/constants/settings/definition_spec.rbindex 41a1c2ab069..108bfb8098c 100644--- a/spec/constants/settings/definition_spec.rb+++ b/spec/constants/settings/definition_spec.rb@@ -972,4 +972,33 @@ RSpec.describe Settings::Definition, :settings_reset doendendend++ describe ".add" do+ context "when overriding from ENV",+ with_env: {+ "OPENPROJECT_BOGUS_SETTING" => "true"+ } do+ it "allows overriding configuration" do+ described_class.add "bogus_setting",+ default: false++ expect(described_class.all[:bogus_setting].value)+ .to be true+ end+ end++ context "when overriding from ENV with disallow_override set to true",+ with_env: {+ "OPENPROJECT_BOGUS_SETTING" => "true"+ } do+ it "allows overriding configuration" do+ described_class.add "bogus_setting",+ default: false,+ disallow_override: true++ expect(described_class.all[:bogus_setting].value)+ .to be false+ end+ end+ endenddiff --git a/spec/lib/open_project/feature_decisions_spec.rb b/spec/lib/open_project/feature_decisions_spec.rbindex e47c210a8de..1ed85550dfe 100644--- a/spec/lib/open_project/feature_decisions_spec.rb+++ b/spec/lib/open_project/feature_decisions_spec.rb@@ -32,21 +32,21 @@ RSpec.describe OpenProject::FeatureDecisions, :settings_reset doinclude_context "with clean feature decisions"- shared_context "when adding without env variable" do+ shared_context "when adding the feature flag" dobefore dodescribed_class.add flag_nameendend- shared_context "when adding the given feature flag" do+ shared_context "when adding the feature flag as always active" dobefore do- described_class.add flag_name+ described_class.add flag_name, force_active: trueendenddescribe "`flag_name`_active?" docontext "without an ENV variable" do- include_context "when adding without env variable"+ include_context "when adding the feature flag"it "is false by default" doexpect(described_class.send(:"#{flag_name}_active?"))@@ -54,9 +54,28 @@ RSpec.describe OpenProject::FeatureDecisions, :settings_reset doendend+ context "without an ENV variable but set to active" do+ include_context "when adding the feature flag as always active"++ it "is true by default" do+ expect(described_class.send(:"#{flag_name}_active?"))+ .to be true+ end+ end+context "with an ENV variable (set to true)",with_env: { "OPENPROJECT_FEATURE_EXAMPLE_FLAG_ACTIVE" => "true" } do- include_context "when adding the given feature flag"+ include_context "when adding the feature flag"++ it "is true" do+ expect(described_class.send(:"#{flag_name}_active?"))+ .to be true+ end+ end++ context "with a flag defined that is disabled via env and set to active",+ with_env: { "OPENPROJECT_FEATURE_EXAMPLE_FLAG_ACTIVE" => "false" } do+ include_context "when adding the feature flag as always active"it "is true" doexpect(described_class.send(:"#{flag_name}_active?"))@@ -74,7 +93,7 @@ RSpec.describe OpenProject::FeatureDecisions, :settings_reset doendcontext "with a flag defined but not enabled" do- include_context "when adding without env variable"+ include_context "when adding the feature flag"it "returns an empty array" doexpect(described_class.active)@@ -82,11 +101,30 @@ RSpec.describe OpenProject::FeatureDecisions, :settings_reset doendend+ context "without an ENV variable but set to active" do+ include_context "when adding the feature flag as always active"++ it "is an array with the flag included" do+ expect(described_class.active)+ .to eq [flag_name.to_s]+ end+ end+context "with a flag defined that is enabled via env",with_env: { "OPENPROJECT_FEATURE_EXAMPLE_FLAG_ACTIVE" => "true" } do- include_context "when adding the given feature flag"+ include_context "when adding the feature flag"- it "returns an empty array" do+ it "is an array with the flag included" do+ expect(described_class.active)+ .to eq [flag_name.to_s]+ end+ end++ context "with a flag defined that is disabled via env and set to active",+ with_env: { "OPENPROJECT_FEATURE_EXAMPLE_FLAG_ACTIVE" => "false" } do+ include_context "when adding the feature flag as always active"++ it "is an array with the flag included" doexpect(described_class.active).to eq [flag_name.to_s]end