Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor environment vars to use Config gem for everything #1119

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ spec/examples.txt
doc
.vscode
dump.rdb

config/settings.local.yml
config/settings/*.local.yml
config/environments/*.local.yml
11 changes: 7 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions app/models/concerns/github_pull_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions app/models/concerns/votable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ 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)
end
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!
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions app/models/proposal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
18 changes: 17 additions & 1 deletion config/initializers/config.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# frozen_string_literal: true

Config.setup do |config|
config.const_name = "Rules"
config.const_name = "Settings"
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.
config.schema do
required(:max_age)
required(:min_age)
required(:pass_threshold)
required(:yes_weight)
required(:no_weight)
required(:block_weight)
required(:block_threshold)
end
end
4 changes: 0 additions & 4 deletions config/initializers/rules.rb

This file was deleted.

7 changes: 0 additions & 7 deletions config/rules.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "rails_helper"

RSpec.describe Rules do
RSpec.describe Settings do
around do |example|
env = {
YES_WEIGHT: "1",
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/models/concerns/votable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down