Skip to content

Commit

Permalink
Reduce the spread of application config across the app
Browse files Browse the repository at this point in the history
  • Loading branch information
martinemde committed Dec 4, 2024
1 parent a786900 commit 3a828bd
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/avo/actions/create_user.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Avo::Actions::CreateUser < Avo::Actions::ApplicationAction
self.name = "Create User"
self.visible = lambda {
current_user.team_member?("rubygems-org") && view == :index && !Rails.env.production?
current_user.team_member?("rubygems-org") && view == :index && !Clearance.configuration.allow_sign_up?
}
self.standalone = true

Expand Down
2 changes: 1 addition & 1 deletion app/models/gem_download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def downloads_by_gem(rubygem_ids)
end

def update_query(id, downloads, version_downloads)
{ update: { _index: "rubygems-#{Rails.env}",
{ update: { _index: Gemcutter::SEARCH_INDEX_NAME,
_id: id,
data: { doc: { downloads: downloads, version_downloads: version_downloads } } } }
end
Expand Down
5 changes: 3 additions & 2 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ def self.config
Rails.application.config.rubygems
end

DEPLOYMENT_ENV_NAME = Rails.env.to_s.freeze
DEFAULT_PAGE = 1
DEFAULT_PAGINATION = 20
EMAIL_TOKEN_EXPIRES_AFTER = 3.hours
HOST = config["host"].freeze
HOST_DISPLAY = Rails.env.production? || Rails.env.development? || Rails.env.test? ? "RubyGems.org" : "RubyGems.org #{Rails.env}"
HOST_DISPLAY = ENV.fetch("HOST_DISPLAY") { Rails.env.production? || Rails.env.development? || Rails.env.test? ? "RubyGems.org" : "RubyGems.org #{Rails.env}" }
NEWS_DAYS_LIMIT = 7.days
NEWS_MAX_PAGES = 10
NEWS_PER_PAGE = 10
Expand All @@ -98,7 +99,7 @@ def self.config
POPULAR_DAYS_LIMIT = 70.days
PROTOCOL = config["protocol"]
REMEMBER_FOR = 2.weeks
SEARCH_INDEX_NAME = "rubygems-#{Rails.env}".freeze
SEARCH_INDEX_NAME = ENV.fetch("SEARCH_INDEX_NAME", "rubygems-#{Rails.env}".freeze)
SEARCH_NUM_REPLICAS = ENV.fetch("SEARCH_NUM_REPLICAS", 1).to_i
SEARCH_MAX_PAGES = 100 # Limit max page as ES result window is upper bounded by 10_000 records
STATS_MAX_PAGES = 10
Expand Down
4 changes: 4 additions & 0 deletions config/deploy/web.yaml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ spec:
value: "<%= environment == 'production' ? 8 : 2 %>"
- name: RAILS_MAX_THREADS
value: "5"
- name: HOST_DISPLAY
value: "RubyGems.org<%= environment == 'production' ? '' : " #{environment.capitalize}" %>"
- name: DD_AGENT_HOST
valueFrom:
fieldRef:
Expand Down Expand Up @@ -139,6 +141,8 @@ spec:
key: elasticsearch_url
- name: SEARCH_NUM_REPLICAS
value: "<%= environment == 'production' ? 2 : 1 %>"
- name: SEARCH_INDEX_NAME
value: "rubygems-<%= environment %>"
- name: MEMCACHED_ENDPOINT
valueFrom:
secretKeyRef:
Expand Down
4 changes: 2 additions & 2 deletions config/initializers/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
config.current_user_method = :admin_user
config.authenticate_with do
if !Rails.env.local? && !(Avo.license.valid? && Avo.license.advanced?)
raise "Avo::Pro is missing in #{Rails.env}." \
raise "Avo::Pro is missing on #{Gemcutter::HOST} (env:#{Rails.env})." \
"\nRails.groups=#{Rails.groups.inspect}" \
"\nAvo.license=#{Avo.license.inspect}" \
"\nAvo.configuration.license=#{Avo.configuration.license.inspect}"
Expand Down Expand Up @@ -66,7 +66,7 @@
# config.cache_resource_filters = ->(current_user:, resource:) { current_user.cache_resource_filters?}

## == Customization ==
config.app_name = "RubyGems.org (#{Rails.env})"
config.app_name = Gemcutter::HOST_DISPLAY
# config.timezone = 'UTC'
# config.currency = 'USD'
# config.hide_layout_when_printing = false
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/content_security_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
ddtags: {
service: "rubygems.org",
version: AppRevision.version,
env: Rails.env,
env: Gemcutter::DEPLOYMENT_ENV_NAME,
trace_id: Datadog::Tracing.correlation&.trace_id,
"gemcutter.user.id": (current_user.id if respond_to?(:signed_in?) && signed_in?)
}.compact.map { |k, v| "#{k}:#{v}" }.join(',')
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/datadog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

c.service = "rubygems.org"
c.version = AppRevision.version
c.env = Rails.env
c.env = Gemcutter::DEPLOYMENT_ENV_NAME

# Enabling datadog functionality

Expand Down
2 changes: 1 addition & 1 deletion config/initializers/rack_attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def self.api_key_owner_id(req)

event = {
timestamp: ::Time.now.utc,
env: Rails.env,
env: Gemcutter::DEPLOYMENT_ENV_NAME,
message: "[429] #{method} #{request.env['REQUEST_PATH']}",
http: {
request_id: request.env["action_dispatch.request_id"],
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/semantic_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
def append_info_to_payload(payload)
payload.merge!(
timestamp: Time.now.utc,
env: Rails.env,
env: Gemcutter::DEPLOYMENT_ENV_NAME,
network: {
client: {
ip: request.ip
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/statsd.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
StatsD.logger = SemanticLogger[StatsD]

StatsD.singleton_client = StatsD.singleton_client.clone_with_options(
default_tags: ["env:#{Rails.env}"]
default_tags: ["env:#{Gemcutter::DEPLOYMENT_ENV_NAME}"]
)

ActiveSupport::Notifications.subscribe(/process_action.action_controller/) do |event|
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/es_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ def es_version_downloads(id)

def get_response(id)
Rubygem.searchkick_index.refresh
Searchkick.client.get index: "rubygems-#{Rails.env}", id: id
Searchkick.client.get index: Gemcutter::SEARCH_INDEX_NAME, id: id
end
end
3 changes: 1 addition & 2 deletions test/models/deletion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ class DeletionTest < ActiveSupport::TestCase

perform_enqueued_jobs

response = Searchkick.client.get index: "rubygems-#{Rails.env}",
id: @version.rubygem_id
response = Searchkick.client.get index: Gemcutter::SEARCH_INDEX_NAME, id: @version.rubygem_id

assert response["_source"]["yanked"]
end
Expand Down
4 changes: 2 additions & 2 deletions test/models/pusher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ def two_cert_chain(signing_key:, root_not_before: Time.current, cert_not_before:
should "create rubygem index" do
@rubygem.update_column("updated_at", Date.new(2016, 07, 04))
perform_enqueued_jobs only: ReindexRubygemJob
response = Searchkick.client.get index: "rubygems-#{Rails.env}",
response = Searchkick.client.get index: Gemcutter::SEARCH_INDEX_NAME,
id: @rubygem.id
expected_response = {
"name" => "gemsgemsgems",
Expand Down Expand Up @@ -733,7 +733,7 @@ def two_cert_chain(signing_key:, root_not_before: Time.current, cert_not_before:

should "update rubygem index" do
perform_enqueued_jobs only: ReindexRubygemJob
response = Searchkick.client.get index: "rubygems-#{Rails.env}",
response = Searchkick.client.get index: Gemcutter::SEARCH_INDEX_NAME,
id: @rubygem.id

assert_equal "new summary", response["_source"]["summary"]
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def requires_avo_pro
return if Avo.configuration.license == "advanced" && defined?(Avo::Pro)

if ActiveRecord::Type::Boolean.new.cast(ENV["REQUIRE_AVO_PRO"])
raise "REQUIRE_AVO_PRO is set but Avo::Pro is missing in #{Rails.env}." \
raise "REQUIRE_AVO_PRO is set but Avo::Pro is missing on #{Gemcutter::HOST} (env:#{Rails.env})." \
"\nRAILS_GROUPS=#{ENV['RAILS_GROUPS'].inspect}\nAvo.license=#{Avo.license.inspect}"
end
skip "avo pro is not present but was required for this test"
Expand Down

0 comments on commit 3a828bd

Please sign in to comment.