From 67213f8c70761f6e93ebc34c35de99341ba701de Mon Sep 17 00:00:00 2001 From: James Smith Date: Thu, 13 May 2021 22:02:23 +0100 Subject: [PATCH 01/10] force dotenv load before config loading --- config/initializers/config.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/initializers/config.rb b/config/initializers/config.rb index fc36f55d4..43729d91a 100644 --- a/config/initializers/config.rb +++ b/config/initializers/config.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true - +Dotenv.load! Config.setup do |config| config.const_name = "Rules" end From 0eff38cdca258e025ebaaca7d1937f780fd99424 Mon Sep 17 00:00:00 2001 From: James Smith Date: Thu, 13 May 2021 22:16:25 +0100 Subject: [PATCH 02/10] Rename rules to settings, to use the defaults for the Config gem --- app/models/concerns/github_pull_request.rb | 6 +++--- app/models/concerns/votable.rb | 12 ++++++------ app/models/proposal.rb | 4 ++-- config/initializers/config.rb | 3 ++- config/initializers/rules.rb | 4 ---- config/{rules.yml => settings.yml} | 0 spec/config/initializers/rules_spec.rb | 4 ++-- spec/models/concerns/votable_spec.rb | 2 +- 8 files changed, 16 insertions(+), 19 deletions(-) delete mode 100644 config/initializers/rules.rb rename config/{rules.yml => settings.yml} (100%) diff --git a/app/models/concerns/github_pull_request.rb b/app/models/concerns/github_pull_request.rb index 904059ffc..dd7c40cfb 100644 --- a/app/models/concerns/github_pull_request.rb +++ b/app/models/concerns/github_pull_request.rb @@ -44,7 +44,7 @@ def set_vote_build_status elsif passed? set_build_status(:success, I18n.t("build_status.votes.agreed"), status) else - remaining_votes = Rules.pass_threshold - score + remaining_votes = Settings.pass_threshold - score set_build_status(:pending, I18n.t("build_status.votes.waiting", remaining: remaining_votes), status) end @@ -54,10 +54,10 @@ def set_time_build_status status = "groupthink/time" if too_old? set_build_status(:failure, - I18n.t("build_status.time.too_old", max_age: Rules.max_age, age: age), status) + I18n.t("build_status.time.too_old", max_age: Settings.max_age, age: age), status) elsif too_new? set_build_status(:pending, - I18n.t("build_status.time.too_new", min_age: Rules.min_age, age: age), status) + I18n.t("build_status.time.too_new", min_age: Settings.min_age, age: age), status) else set_build_status(:success, I18n.t("build_status.time.success", age: age), status) end diff --git a/app/models/concerns/votable.rb b/app/models/concerns/votable.rb index 6b964a558..a242f9823 100644 --- a/app/models/concerns/votable.rb +++ b/app/models/concerns/votable.rb @@ -12,9 +12,9 @@ def queue_vote_count def score weights = { - yes: Rules.yes_weight, - no: Rules.no_weight, - block: Rules.block_weight, + yes: Settings.yes_weight, + no: Settings.no_weight, + block: Settings.block_weight, } interactions.all.inject(0) do |sum, i| sum + (weights[i.last_vote.try(:to_sym)] || 0) @@ -22,11 +22,11 @@ def score end def passed? - score >= Rules.pass_threshold + score >= Settings.pass_threshold end def blocked? - score < Rules.block_threshold + score < Settings.block_threshold end def count_votes! @@ -80,7 +80,7 @@ def post_instructions proposal_number: number, repo: ENV.fetch("GITHUB_REPO"), proposer: proposer.login, - }.merge(Rules) + }.merge(Settings) github_add_comment [INSTRUCTION_HEADER, I18n.t("help.instruction_comment", vars)].join("\n") end end diff --git a/app/models/proposal.rb b/app/models/proposal.rb index cb18134c3..7c06034a1 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -45,11 +45,11 @@ def age end def too_old? - age >= Rules.max_age + age >= Settings.max_age end def too_new? - age < Rules.min_age + age < Settings.min_age end def update_state! diff --git a/config/initializers/config.rb b/config/initializers/config.rb index 43729d91a..b0ec55151 100644 --- a/config/initializers/config.rb +++ b/config/initializers/config.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true Dotenv.load! + Config.setup do |config| - config.const_name = "Rules" + config.const_name = "Settings" end diff --git a/config/initializers/rules.rb b/config/initializers/rules.rb deleted file mode 100644 index 10cb529ce..000000000 --- a/config/initializers/rules.rb +++ /dev/null @@ -1,4 +0,0 @@ -# frozen_string_literal: true - -Rules.add_source! Rails.root.join("config/rules.yml").to_s -Rules.reload! diff --git a/config/rules.yml b/config/settings.yml similarity index 100% rename from config/rules.yml rename to config/settings.yml diff --git a/spec/config/initializers/rules_spec.rb b/spec/config/initializers/rules_spec.rb index ef279b99a..5435d4641 100644 --- a/spec/config/initializers/rules_spec.rb +++ b/spec/config/initializers/rules_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" -RSpec.describe Rules do +RSpec.describe Settings do around do |example| env = { YES_WEIGHT: "1", @@ -19,7 +19,7 @@ end end - it "creates a global Rules object" do + it "creates a global Settings object" do expect(described_class).to be_present end diff --git a/spec/models/concerns/votable_spec.rb b/spec/models/concerns/votable_spec.rb index 661be0f8c..ace4f40aa 100644 --- a/spec/models/concerns/votable_spec.rb +++ b/spec/models/concerns/votable_spec.rb @@ -30,7 +30,7 @@ def mock_vote(vote: "✅", created_at: 1.hour.ago, login: "test") GITHUB_REPO: "example/repo" } ClimateControl.modify env do - Rules.reload! + Settings.reload! example.run end end From fd1e0971d41a2fa04ba0d2881732d44a331406b4 Mon Sep 17 00:00:00 2001 From: James Smith Date: Thu, 13 May 2021 22:19:09 +0100 Subject: [PATCH 03/10] generate full Config gem initializer so we can use more features easily --- .gitignore | 4 +++ config/initializers/config.rb | 57 ++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7fc1e8399..faf74f47d 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,7 @@ spec/examples.txt doc .vscode dump.rdb + +config/settings.local.yml +config/settings/*.local.yml +config/environments/*.local.yml diff --git a/config/initializers/config.rb b/config/initializers/config.rb index b0ec55151..286a119ef 100644 --- a/config/initializers/config.rb +++ b/config/initializers/config.rb @@ -2,5 +2,60 @@ Dotenv.load! Config.setup do |config| - config.const_name = "Settings" + # Name of the constant exposing loaded settings + config.const_name = 'Settings' + + # Ability to remove elements of the array set in earlier loaded settings file. For example value: '--'. + # + # config.knockout_prefix = nil + + # Overwrite an existing value when merging a `nil` value. + # When set to `false`, the existing value is retained after merge. + # + # config.merge_nil_values = true + + # Overwrite arrays found in previously loaded settings file. When set to `false`, arrays will be merged. + # + # config.overwrite_arrays = true + + # Load environment variables from the `ENV` object and override any settings defined in files. + # + # config.use_env = false + + # Define ENV variable prefix deciding which variables to load into config. + # + # Reading variables from ENV is case-sensitive. If you define lowercase value below, ensure your ENV variables are + # prefixed in the same way. + # + # When not set it defaults to `config.const_name`. + # + config.env_prefix = 'SETTINGS' + + # What string to use as level separator for settings loaded from ENV variables. Default value of '.' works well + # with Heroku, but you might want to change it for example for '__' to easy override settings from command line, where + # using dots in variable names might not be allowed (eg. Bash). + # + # config.env_separator = '.' + + # Ability to process variables names: + # * nil - no change + # * :downcase - convert to lower case + # + # config.env_converter = :downcase + + # Parse numeric values as integers instead of strings. + # + # config.env_parse_values = true + + # Validate presence and type of specific config values. Check https://github.com/dry-rb/dry-validation for details. + # + # config.schema do + # required(:name).filled + # required(:age).maybe(:int?) + # required(:email).filled(format?: EMAIL_REGEX) + # end + + # Evaluate ERB in YAML config files at load time. + # + # config.evaluate_erb_yaml = true end From cb8bcc37f16c47373cad99b78dc29af5a15d1931 Mon Sep 17 00:00:00 2001 From: James Smith Date: Thu, 13 May 2021 22:22:40 +0100 Subject: [PATCH 04/10] use built-in Config capabilities to load from ENV --- config/initializers/config.rb | 48 +++-------------------------------- config/settings.yml | 7 ----- 2 files changed, 3 insertions(+), 52 deletions(-) delete mode 100644 config/settings.yml diff --git a/config/initializers/config.rb b/config/initializers/config.rb index 286a119ef..12005d567 100644 --- a/config/initializers/config.rb +++ b/config/initializers/config.rb @@ -2,49 +2,10 @@ Dotenv.load! Config.setup do |config| - # Name of the constant exposing loaded settings config.const_name = 'Settings' - - # Ability to remove elements of the array set in earlier loaded settings file. For example value: '--'. - # - # config.knockout_prefix = nil - - # Overwrite an existing value when merging a `nil` value. - # When set to `false`, the existing value is retained after merge. - # - # config.merge_nil_values = true - - # Overwrite arrays found in previously loaded settings file. When set to `false`, arrays will be merged. - # - # config.overwrite_arrays = true - - # Load environment variables from the `ENV` object and override any settings defined in files. - # - # config.use_env = false - - # Define ENV variable prefix deciding which variables to load into config. - # - # Reading variables from ENV is case-sensitive. If you define lowercase value below, ensure your ENV variables are - # prefixed in the same way. - # - # When not set it defaults to `config.const_name`. - # - config.env_prefix = 'SETTINGS' - - # What string to use as level separator for settings loaded from ENV variables. Default value of '.' works well - # with Heroku, but you might want to change it for example for '__' to easy override settings from command line, where - # using dots in variable names might not be allowed (eg. Bash). - # - # config.env_separator = '.' - - # Ability to process variables names: - # * nil - no change - # * :downcase - convert to lower case - # - # config.env_converter = :downcase - - # Parse numeric values as integers instead of strings. - # + config.use_env = true + config.env_prefix = '' + config.env_converter = :downcase # config.env_parse_values = true # Validate presence and type of specific config values. Check https://github.com/dry-rb/dry-validation for details. @@ -55,7 +16,4 @@ # required(:email).filled(format?: EMAIL_REGEX) # end - # Evaluate ERB in YAML config files at load time. - # - # config.evaluate_erb_yaml = true end diff --git a/config/settings.yml b/config/settings.yml deleted file mode 100644 index d7cb0ac4a..000000000 --- a/config/settings.yml +++ /dev/null @@ -1,7 +0,0 @@ -yes_weight: <%= ENV.fetch("YES_WEIGHT") %> -no_weight: <%= ENV.fetch("NO_WEIGHT") %> -block_weight: <%= ENV.fetch("BLOCK_WEIGHT") %> -pass_threshold: <%= ENV.fetch("PASS_THRESHOLD") %> -block_threshold: <%= ENV.fetch("BLOCK_THRESHOLD") %> -min_age: <%= ENV.fetch("MIN_AGE") %> -max_age: <%= ENV.fetch("MAX_AGE") %> \ No newline at end of file From 08ae29a71fcb8b833494cd418169f9f599fb2201 Mon Sep 17 00:00:00 2001 From: James Smith Date: Thu, 13 May 2021 22:37:12 +0100 Subject: [PATCH 05/10] add required config keys with schema definition --- config/initializers/config.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/config/initializers/config.rb b/config/initializers/config.rb index 12005d567..43d38c05a 100644 --- a/config/initializers/config.rb +++ b/config/initializers/config.rb @@ -8,12 +8,15 @@ config.env_converter = :downcase # config.env_parse_values = true - # Validate presence and type of specific config values. Check https://github.com/dry-rb/dry-validation for details. - # - # config.schema do - # required(:name).filled - # required(:age).maybe(:int?) - # required(:email).filled(format?: EMAIL_REGEX) - # end + # Validate presence and type of specific config values. + # Check https://github.com/dry-rb/dry-validation for details. + config.schema do + required(:max_age) + required(:min_age) + required(:pass_threshold) + required(:yes_weight) + required(:no_weight) + required(:block_weight) + end end From d2e64abdb2396ba1b8b9e32b978991c4c6d1e608 Mon Sep 17 00:00:00 2001 From: James Smith Date: Thu, 13 May 2021 22:38:52 +0100 Subject: [PATCH 06/10] add block_threshold to required schema --- config/initializers/config.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/initializers/config.rb b/config/initializers/config.rb index 43d38c05a..3ed0da3e4 100644 --- a/config/initializers/config.rb +++ b/config/initializers/config.rb @@ -17,6 +17,7 @@ required(:yes_weight) required(:no_weight) required(:block_weight) + required(:block_threshold) end end From dacc6587e0558e84029a0a7b8fe92d45590648db Mon Sep 17 00:00:00 2001 From: James Smith Date: Thu, 13 May 2021 22:40:07 +0100 Subject: [PATCH 07/10] lint fixes --- config/initializers/config.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/initializers/config.rb b/config/initializers/config.rb index 3ed0da3e4..0d039a0a1 100644 --- a/config/initializers/config.rb +++ b/config/initializers/config.rb @@ -1,10 +1,11 @@ # frozen_string_literal: true + Dotenv.load! Config.setup do |config| - config.const_name = 'Settings' + config.const_name = "Settings" config.use_env = true - config.env_prefix = '' + config.env_prefix = "" config.env_converter = :downcase # config.env_parse_values = true @@ -19,5 +20,4 @@ required(:block_weight) required(:block_threshold) end - end From 1890901a49d7a437d00c0016db7c5a0e72c29d57 Mon Sep 17 00:00:00 2001 From: James Smith Date: Thu, 13 May 2021 22:40:35 +0100 Subject: [PATCH 08/10] rename settings test file --- spec/config/initializers/{rules_spec.rb => settings_spec.rb} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename spec/config/initializers/{rules_spec.rb => settings_spec.rb} (100%) diff --git a/spec/config/initializers/rules_spec.rb b/spec/config/initializers/settings_spec.rb similarity index 100% rename from spec/config/initializers/rules_spec.rb rename to spec/config/initializers/settings_spec.rb From f3606d71b998b307922d490751f681478cd21ba4 Mon Sep 17 00:00:00 2001 From: James Smith Date: Thu, 13 May 2021 22:44:27 +0100 Subject: [PATCH 09/10] use dotenv/rails-now for eager loading before config instead of manually calling Dotenv.load! --- Gemfile | 11 +++++++---- config/initializers/config.rb | 3 --- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Gemfile b/Gemfile index 1dcf53b64..dcd97cca4 100644 --- a/Gemfile +++ b/Gemfile @@ -33,6 +33,13 @@ gem "jbuilder", "~> 2.11" # Use Capistrano for deployment # gem 'capistrano-rails', group: :development +group :development, :test do + gem 'dotenv-rails', require: 'dotenv/rails-now' +end + +# Custom application configuration +gem "config" + # Github gem "octokit", "~> 4.21" @@ -57,9 +64,6 @@ gem "rinku", require: "rails_rinku" gem "kaminari" -# Custom application configuration -gem "config" - gem "sucker_punch", "~> 3.0" # Postgres in production for Heroku @@ -77,7 +81,6 @@ group :development, :test do gem "simplecov-lcov" gem "rspec-rails" gem "rspec_junit_formatter" # For CircleCI test reporting - gem "dotenv-rails" gem "database_cleaner" gem "email_spec" gem "factory_bot_rails" diff --git a/config/initializers/config.rb b/config/initializers/config.rb index 0d039a0a1..92507515d 100644 --- a/config/initializers/config.rb +++ b/config/initializers/config.rb @@ -1,7 +1,4 @@ # frozen_string_literal: true - -Dotenv.load! - Config.setup do |config| config.const_name = "Settings" config.use_env = true From c5b8e2e25ac07e5087646fed9cb750dff417dddf Mon Sep 17 00:00:00 2001 From: James Smith Date: Thu, 13 May 2021 22:49:51 +0100 Subject: [PATCH 10/10] lint --- Gemfile | 2 +- config/initializers/config.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index dcd97cca4..e7afa254e 100644 --- a/Gemfile +++ b/Gemfile @@ -34,7 +34,7 @@ gem "jbuilder", "~> 2.11" # gem 'capistrano-rails', group: :development group :development, :test do - gem 'dotenv-rails', require: 'dotenv/rails-now' + gem "dotenv-rails", require: "dotenv/rails-now" end # Custom application configuration diff --git a/config/initializers/config.rb b/config/initializers/config.rb index 92507515d..1f147c2ec 100644 --- a/config/initializers/config.rb +++ b/config/initializers/config.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + Config.setup do |config| config.const_name = "Settings" config.use_env = true