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
simplify setting up feature flags
ulferts2d5efb953351
diff --git a/config/initializers/feature_decisions.rb b/config/initializers/feature_decisions.rbnew file mode 100644index 00000000000..407769fe3cd--- /dev/null+++ b/config/initializers/feature_decisions.rb@@ -0,0 +1,40 @@+# --copyright+# OpenProject is an open source project management software.+# Copyright (C) 2022 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# ++++require_relative '../../lib_static/open_project/feature_decisions'++# Add feature flags here via e.g.+#+# OpenProject::FeatureDecisions.add :some_flag+#+# If the feature to be flag-guarded stems from a module, add an initializer+# to that module's engine:+#+# initializer 'the_engine.feature_decisions' do+# OpenProject::FeatureDecisions.add :some_flag+# enddiff --git a/docs/api/apiv3/components/schemas/configuration_model.yml b/docs/api/apiv3/components/schemas/configuration_model.ymlindex 026aa429cc1..da44a320379 100644--- a/docs/api/apiv3/components/schemas/configuration_model.yml+++ b/docs/api/apiv3/components/schemas/configuration_model.yml@@ -6,21 +6,17 @@ properties:type: integerdescription: The maximum allowed size of an attachment in BytesreadOnly: true+ hostName:+ type: string+ description: The host name configured for the system+ readOnly: trueperPageOptions:type: arraydescription: Page size steps to be offered in paginated list UIitems:type: integer-example:- _type: Configuration- _links:- self:- href: "/api/v3/configuration"- userPreferences:- href: "/api/v3/my_preferences"- maximumAttachmentFileSize: 5242880- hostName: 'example.com:8080'- perPageOptions:- - 1- - 10- - 100+ activeFeatureFlags:+ type: array+ description: The list of all feature flags that are active+ items:+ type: stringdiff --git a/docs/api/apiv3/paths/configuration.yml b/docs/api/apiv3/paths/configuration.ymlindex c488c2d9af5..06923a2fd98 100644--- a/docs/api/apiv3/paths/configuration.yml+++ b/docs/api/apiv3/paths/configuration.yml@@ -8,17 +8,21 @@ get:examples:response:value:+ _type: Configuration_links:self:href: "/api/v3/configuration"userPreferences:href: "/api/v3/my_preferences"- _type: ConfigurationmaximumAttachmentFileSize: 5242880+ hostName: 'example.com:8080'perPageOptions:- - 1- - 10- - 100+ - 1+ - 10+ - 100+ activeFeatureFlags:+ - 'aFeatureFlag'+ - 'anotherFeatureFlag'schema:"$ref": "../components/schemas/configuration_model.yml"description: OKdiff --git a/docs/api/apiv3/tags/configuration.yml b/docs/api/apiv3/tags/configuration.ymlindex f0f22926498..ebf0c3127ea 100644--- a/docs/api/apiv3/tags/configuration.yml+++ b/docs/api/apiv3/tags/configuration.yml@@ -16,9 +16,10 @@ description: |-## Local Properties- | Property | Description | Type | Condition | Supported operations |- | :-----------------------: | -------------------------------------------------- | ---------- | --------------------------------- | -------------------- |- | maximumAttachmentFileSize | The maximum allowed size of an attachment in Bytes | Integer | | READ |- | perPageOptions | Page size steps to be offered in paginated list UI | Integer[] | | READ |- | hostName | The host name configured for the system | String | | READ |+ | Property | Description | Type | Condition | Supported operations |+ | :-----------------------: | -------------------------------------------------- | ---------- | ----------------- | -------------------- |+ | maximumAttachmentFileSize | The maximum allowed size of an attachment in Bytes | Integer | | READ |+ | perPageOptions | Page size steps to be offered in paginated list UI | Integer[] | | READ |+ | hostName | The host name configured for the system | String | | READ |+ | activeFeatureFlags | The list of all feature flags that are active | String[] | | READ |name: Configurationdiff --git a/lib/api/v3/configuration/configuration_representer.rb b/lib/api/v3/configuration/configuration_representer.rbindex 9da92a4337c..edc5eb1896d 100644--- a/lib/api/v3/configuration/configuration_representer.rb+++ b/lib/api/v3/configuration/configuration_representer.rb@@ -79,6 +79,13 @@ module API},render_nil: true+ property :active_feature_flags,+ getter: ->(*) {+ OpenProject::FeatureDecisions+ .active+ .map { |flag| flag.camelize(:lower) }+ }+property :user_preferences,embedded: true,exec_context: :decorator,diff --git a/lib_static/open_project/feature_decisions.rb b/lib_static/open_project/feature_decisions.rbnew file mode 100644index 00000000000..2198e28ab9e--- /dev/null+++ b/lib_static/open_project/feature_decisions.rb@@ -0,0 +1,86 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2022 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module OpenProject+ ##+ # This module is the container for temporary or permanent feature flags.+ #+ # New feature flags can automatically be added by calling+ #+ # OpenProject::Application.initializer 'set flag' do+ # OpenProject::FeatureDecisions.add :the_name_of_the_flag+ # end+ #+ # This will set up:+ # * the method `.the_name_of_the_flag_active?` for querying the state+ # of the flag. By default, it is false.+ # * fetching the overwritten value from+ # * ENV variable (`OPENPROJECT_THE_NAME_OF_THE_FLAG_ACTIVE = 'true'`)+ # * configuration.yml file (`the_name_of_the_flag_active: true`)+ # * from the settings database table (`Setting.feature_the_name_of_the_flag_active = true)+ # * including the flag in the `.active` array in case it is enabled+ # * including the flag in the `.all`+ #+ # The setup should be carried out inside an initializer for the overwriting from ENV or configuration.yml+ # to be picked up.+ #+ # A spec in which such a flag is to be enabled can do so via:+ #+ # context 'some description', with_flag: { the_name_of_the_flag: true } do+ # ...+ # end+ #+ module FeatureDecisions+ module_function++ def add(flag_name)+ all << flag_name+ define_flag_methods(flag_name)+ define_setting_definition(flag_name)+ end++ def active+ all.filter { |flag_name| send("#{flag_name}_active?") }.map(&:to_s)+ end++ def all+ @all ||= []+ end++ def define_flag_methods(flag_name)+ define_singleton_method "#{flag_name}_active?" do+ Setting.send("feature_#{flag_name}_active?")+ end+ end++ def define_setting_definition(flag_name)+ Settings::Definition.add :"feature_#{flag_name}_active",+ default: false+ end+ end+enddiff --git a/spec/constants/settings/definition_spec.rb b/spec/constants/settings/definition_spec.rbindex 098ff0c3afc..ba1a3d6a286 100644--- a/spec/constants/settings/definition_spec.rb+++ b/spec/constants/settings/definition_spec.rb@@ -29,19 +29,6 @@require 'spec_helper'describe Settings::Definition do- shared_context 'with clean definitions' do- let!(:definitions_before) { described_class.all.dup }-- before do- described_class.send(:reset)- end-- after do- described_class.send(:reset)- described_class.instance_variable_set(:@all, definitions_before)- end- end-describe '.all' dosubject(:all) { described_class.all }@@ -71,7 +58,7 @@ describe Settings::Definition doendcontext 'when overriding from ENV' do- include_context 'with clean definitions'+ include_context 'with clean setting definitions'def value_for(name)all.detect { |d| d.name == name }.value@@ -375,7 +362,7 @@ describe Settings::Definition doendcontext 'when overriding from file' do- include_context 'with clean definitions'+ include_context 'with clean setting definitions'let(:file_contents) do<<~YAML@@ -505,7 +492,7 @@ describe Settings::Definition doendcontext 'when adding an additional setting' do- include_context 'with clean definitions'+ include_context 'with clean setting definitions'it 'includes the setting' doall@@ -551,7 +538,7 @@ describe Settings::Definition doendcontext 'when adding a setting late' do- include_context 'with clean definitions'+ include_context 'with clean setting definitions'let(:key) { 'bogus' }before do@@ -978,7 +965,7 @@ describe Settings::Definition doenddescribe '#on_change' do- include_context 'with clean definitions'+ include_context 'with clean setting definitions'context 'for a definition with a callback' dolet(:callback) { -> { 'foobar ' } }diff --git a/spec/lib/api/v3/configuration/configuration_representer_spec.rb b/spec/lib/api/v3/configuration/configuration_representer_spec.rbindex 7e986bf36fa..800c11cd0fa 100644--- a/spec/lib/api/v3/configuration/configuration_representer_spec.rb+++ b/spec/lib/api/v3/configuration/configuration_representer_spec.rb@@ -44,34 +44,34 @@ describe ::API::V3::Configuration::ConfigurationRepresenter dodescribed_class.new(represented, current_user:, embed_links:)end- context 'generation' do- subject { representer.to_json }+ subject { representer.to_json }- describe '_links' do- it_behaves_like 'has an untitled link' do- let(:link) { 'self' }- let(:href) { api_v3_paths.configuration }- end+ describe '_links' do+ it_behaves_like 'has an untitled link' do+ let(:link) { 'self' }+ let(:href) { api_v3_paths.configuration }+ end- context 'userPreferences' do- context 'if logged in' do- it_behaves_like 'has an untitled link' do- let(:link) { 'userPreferences' }- let(:href) { api_v3_paths.user_preferences(current_user.id) }- end+ describe 'userPreferences' do+ context 'if logged in' do+ it_behaves_like 'has an untitled link' do+ let(:link) { 'userPreferences' }+ let(:href) { api_v3_paths.user_preferences(current_user.id) }end+ end- context 'if not logged in' do- let(:current_user) { build_stubbed(:anonymous) }+ context 'if not logged in' do+ let(:current_user) { build_stubbed(:anonymous) }- it_behaves_like 'has an untitled link' do- let(:link) { 'userPreferences' }- let(:href) { api_v3_paths.user_preferences(current_user.id) }- end+ it_behaves_like 'has an untitled link' do+ let(:link) { 'userPreferences' }+ let(:href) { api_v3_paths.user_preferences(current_user.id) }endendend+ end+ describe 'properties' doit 'indicates its type' doexpect(subject).to be_json_eql('Configuration'.to_json).at_path('_type')end@@ -86,7 +86,7 @@ describe ::API::V3::Configuration::ConfigurationRepresenter doexpect(subject).to be_json_eql([1, 50, 100].to_json).at_path('perPageOptions')end- context 'timeFormat' do+ describe 'timeFormat' docontext 'with time format', with_settings: { time_format: '%I:%M %p' } doit 'indicates the timeFormat' doexpect(subject)@@ -112,7 +112,7 @@ describe ::API::V3::Configuration::ConfigurationRepresenter doendend- context 'dateFormat' do+ describe 'dateFormat' docontext 'without a date format', with_settings: { date_format: '' } doit 'indicates the dateFormat' doexpect(subject)@@ -129,14 +129,6 @@ describe ::API::V3::Configuration::ConfigurationRepresenter doendend- context 'with date format (%d/%m/%Y)', with_settings: { date_format: '%Y-%m-%d' } do- it 'indicates the dateFormat' do- expect(subject)- .to be_json_eql('YYYY-MM-DD'.to_json)- .at_path('dateFormat')- end- end-context 'with date format (%d.%m.%Y)', with_settings: { date_format: '%d.%m.%Y' } doit 'indicates the dateFormat' doexpect(subject)@@ -194,7 +186,7 @@ describe ::API::V3::Configuration::ConfigurationRepresenter doendend- context 'startOfWeek' do+ describe 'startOfWeek' docontext 'without a setting', with_settings: { start_of_week: '' } doit 'is null' doexpect(subject)@@ -212,23 +204,47 @@ describe ::API::V3::Configuration::ConfigurationRepresenter doendend- describe '_embedded' do- context 'userPreferences' do- context 'if embedding' do- let(:embed_links) { true }+ describe 'activeFeatureFlags' do+ context 'without any active flags' do+ it 'is an empty array' do+ expect(subject)+ .to be_json_eql([].to_json)+ .at_path('activeFeatureFlags')+ end+ end++ context 'with active flags' do+ before do+ allow(OpenProject::FeatureDecisions)+ .to receive(:active)+ .and_return(%w(the active_flags))+ end++ it 'is an array of strings of those flags' do+ expect(subject)+ .to be_json_eql(%w(the activeFlags).to_json)+ .at_path('activeFeatureFlags')+ end+ end+ end+ end++ describe '_embedded' do+ describe 'userPreferences' do+ context 'if embedding' do+ let(:embed_links) { true }- it 'embedds the user preferences' do- expect(subject)- .to be_json_eql('UserPreferences'.to_json)- .at_path('_embedded/userPreferences/_type')- end+ it 'embedds the user preferences' do+ expect(subject)+ .to be_json_eql('UserPreferences'.to_json)+ .at_path('_embedded/userPreferences/_type')end+ end- context 'if not embedding' do- it 'embedds the user preferences' do- expect(subject)- .not_to have_json_path('_embedded/userPreferences/_type')- end+ context 'if not embedding' do+ it 'embedds the user preferences' do+ expect(subject)+ .not_to have_json_path('_embedded/userPreferences/_type')endendenddiff --git a/spec/lib/open_project/feature_decisions_spec.rb b/spec/lib/open_project/feature_decisions_spec.rbnew file mode 100644index 00000000000..acb835df827--- /dev/null+++ b/spec/lib/open_project/feature_decisions_spec.rb@@ -0,0 +1,97 @@+# --copyright+# OpenProject is an open source project management software.+# Copyright (C) 2010-2022 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# +++require 'spec_helper'++describe OpenProject::FeatureDecisions do+ let(:flag_name) { :example_flag }++ include_context 'with clean feature decisions'++ shared_context 'when adding without env variable' do+ before do+ described_class.add flag_name+ end+ end++ shared_context 'when adding with env variable set to true' do+ before do+ stub_const('ENV', 'OPENPROJECT_FEATURE_EXAMPLE_FLAG_ACTIVE' => 'true')++ described_class.add flag_name+ end+ end++ include_context 'with clean setting definitions'++ describe '`flag_name`_active?' do+ context 'without an ENV variable' do+ include_context 'when adding without env variable'++ it 'is false by default' do+ expect(described_class.send("#{flag_name}_active?"))+ .to be false+ end+ end++ context 'with an ENV variable (set to true)' do+ include_context 'when adding with env variable set to true'++ it 'is true' do+ expect(described_class.send("#{flag_name}_active?"))+ .to be true+ end+ end+ end++ describe 'active' do+ context 'without any flags defined' do+ it 'returns an empty array' do+ expect(described_class.active)+ .to eq []+ end+ end++ context 'with a flag defined but not enabled' do+ include_context 'when adding without env variable'++ it 'returns an empty array' do+ expect(described_class.active)+ .to eq []+ end+ end++ context 'with a flag defined that is enabled via env' do+ include_context 'when adding with env variable set to true'++ it 'returns an empty array' do+ expect(described_class.active)+ .to eq [flag_name.to_s]+ end+ end+ end+enddiff --git a/spec/requests/api/v3/configuration_resource_spec.rb b/spec/requests/api/v3/configuration_resource_spec.rbindex 539165df881..0291c08fcb5 100644--- a/spec/requests/api/v3/configuration_resource_spec.rb+++ b/spec/requests/api/v3/configuration_resource_spec.rb@@ -36,17 +36,15 @@ describe 'API v3 Configuration resource', type: :request dolet(:user) { create(:user) }let(:configuration_path) { api_v3_paths.configuration }- subject(:response) { last_response }+ current_user { user }- before do- login_as(user)+ subject(:response) do+ get configuration_path++ last_responseenddescribe '#GET' do- before do- get configuration_path- end-it 'returns 200 OK' doexpect(subject.status).to eq(200)end@@ -54,17 +52,44 @@ describe 'API v3 Configuration resource', type: :request doit 'returns the configuration', with_settings: { per_page_options: '3, 5, 8, 13' } doexpect(subject.body).to be_json_eql('Configuration'.to_json)- .at_path("_type")+ .at_path("_type")expect(subject.body).to be_json_eql([3, 5, 8, 13].to_json)- .at_path('perPageOptions')+ .at_path('perPageOptions')endit 'embedds the current user preferences' doexpect(subject.body).to be_json_eql('UserPreferences'.to_json)- .at_path('_embedded/userPreferences/_type')+ .at_path('_embedded/userPreferences/_type')+ end++ it 'does not embed the preferences' do+ expect(subject.body)+ .not_to have_json_path('_embedded/user_preferences')+ end++ context 'with feature flags' do+ include_context 'with clean setting definitions'++ before do+ stub_const('ENV',+ 'OPENPROJECT_FEATURE_AN_EXAMPLE_ACTIVE' => 'true',+ 'OPENPROJECT_FEATURE_ANOTHER_EXAMPLE_ACTIVE' => 'true',+ 'OPENPROJECT_FEATURE_INACTIVE_EXAMPLE_ACTIVE' => 'false')++ OpenProject::FeatureDecisions.add :an_example+ OpenProject::FeatureDecisions.add :another_example+ OpenProject::FeatureDecisions.add :deactivated_example+ OpenProject::FeatureDecisions.add :default_example+ end++ it 'lists the active feature flags' do+ expect(subject.body)+ .to be_json_eql(%w[anExample anotherExample].to_json)+ .at_path('activeFeatureFlags')+ endendcontext 'for a non logged in user' do@@ -72,10 +97,5 @@ describe 'API v3 Configuration resource', type: :request doexpect(subject.status).to eq(200)endend-- it 'does not embed the preferences' do- expect(subject.body)- .not_to have_json_path('_embedded/user_preferences')- endendenddiff --git a/lib/open_project/feature_decisions.rb b/spec/support/clean_feature_decisions.rbsimilarity index 72%rename from lib/open_project/feature_decisions.rbrename to spec/support/clean_feature_decisions.rbindex 28e95e8d0a1..27bf16b06a7 100644--- a/lib/open_project/feature_decisions.rb+++ b/spec/support/clean_feature_decisions.rb@@ -26,18 +26,14 @@# See COPYRIGHT and LICENSE files for more details.#++-module OpenProject- module FeatureDecisions- ##- # This module is the container for the temporary or permanent feature flags.- # Having a flag method instead of a plain Setting, enables- # incorporating more logic into flags, such as release date restrictions.- #- # For example:- #- # def self.your_module_active?- # release_date = Date.new(2022,01,01)- # Setting.feature_your_module_active || Date.today > release_date- # end+RSpec.shared_context 'with clean feature decisions' do+ let!(:decisions_before) { OpenProject::FeatureDecisions.all.dup }++ before do+ OpenProject::FeatureDecisions.instance_variable_set(:@all, [])+ end++ after do+ OpenProject::FeatureDecisions.instance_variable_set(:@all, decisions_before)endenddiff --git a/spec/support/settings/clean_definitions.rb b/spec/support/settings/clean_definitions.rbnew file mode 100644index 00000000000..9aff3a58868--- /dev/null+++ b/spec/support/settings/clean_definitions.rb@@ -0,0 +1,40 @@+# --copyright+# OpenProject is an open source project management software.+# Copyright (C) 2022 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# ++++RSpec.shared_context 'with clean setting definitions' do+ let!(:definitions_before) { Settings::Definition.all.dup }++ before do+ Settings::Definition.send(:reset)+ end++ after do+ Settings::Definition.send(:reset)+ Settings::Definition.instance_variable_set(:@all, definitions_before)+ end+enddiff --git a/spec/support/shared/with_flag.rb b/spec/support/shared/with_flag.rbindex c2dc7eaf44b..ea95f6417be 100644--- a/spec/support/shared/with_flag.rb+++ b/spec/support/shared/with_flag.rb@@ -29,7 +29,7 @@ module WithFlagMixindef with_flags(flags)flags.each do |k, value|- name = k.end_with?('?') ? k : "#{k}?"+ name = "#{/(?:feature_)?(\w+?)(?:_active)?\??$/.match(k)[1]}_active?"raise "#{k} is not a valid flag" unless OpenProject::FeatureDecisions.respond_to?(name)