From 7d8191f9cbd954a3af5ae94f98883437622d139c Mon Sep 17 00:00:00 2001 From: Melody Date: Wed, 10 Jul 2024 09:08:31 -0400 Subject: [PATCH] improve version information --- app/controllers/ping_controller.rb | 2 +- lib/config_manager.rb | 49 ++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/app/controllers/ping_controller.rb b/app/controllers/ping_controller.rb index cc62025c26..6cf31d5cd9 100644 --- a/app/controllers/ping_controller.rb +++ b/app/controllers/ping_controller.rb @@ -9,7 +9,7 @@ def show @tests = {} @tests[:dj_running] = Utils::DelayedJobChecker.instance.ok? @ok = @tests.values.all? - @version = Cnfg.system_version + @version = Cnfg.system_version(detailed: true) render(layout: nil, formats: :text, status: @ok ? :ok : :service_unavailable) end end diff --git a/lib/config_manager.rb b/lib/config_manager.rb index f56ae530a7..974b0bb8ba 100644 --- a/lib/config_manager.rb +++ b/lib/config_manager.rb @@ -13,8 +13,33 @@ def offline_mode? end # read system version from file - def system_version - @system_version ||= File.read(Rails.root.join("VERSION")).strip + def system_version(detailed: false) + return @system_version if @system_version.present? && !detailed + return @detailed_system_version if @detailed_system_version.present? + + case Rails.env + when "development!" + git_describe_tags = `git describe --tags` + git_branch = `git rev-parse --abbrev-ref HEAD` + @system_version = "#{git_describe_tags.strip} (#{git_branch.strip})" + @detailed_system_version = @system_version + else + version_file = File.read(Rails.root.join("VERSION")).strip if File.exist?(Rails.root.join("VERSION")) + version = version_file + + @system_version = version + return @system_version unless detailed + + revision_file = File.read(Rails.root.join("REVISION")) if File.exist?(Rails.root.join("REVISION")) + branch_file = File.read(Rails.root.join("BRANCH")) if File.exist?(Rails.root.join("BRANCH")) + + revision = revision_file&.strip&.slice(0..8)&.then { |rev| "(#{rev})" }.presence + branch = branch_file&.strip&.then { |br| "[#{br}]" }.presence + + @detailed_system_version = [version, revision, branch].compact.join(" ") + end + + detailed ? @detailed_system_version : @system_version end # Locales we support that are displayed right-to-left. @@ -59,7 +84,7 @@ def url_port # Returns a hash of url options (port, protocol, host). Omits port if it's default for protocol. def url_options options = {protocol: url_protocol, host: url_host} - return options if url_protocol == "http" && url_port == 80 || url_protocol == "https" && url_port == 443 + return options if (url_protocol == "http" && url_port == 80) || (url_protocol == "https" && url_port == 443) options[:port] = url_port options end @@ -73,7 +98,7 @@ def smtp_port end def smtp_domain - ENV["NEMO_SMTP_DOMAIN"] + ENV.fetch("NEMO_SMTP_DOMAIN", nil) end def smtp_authentication @@ -81,11 +106,11 @@ def smtp_authentication end def smtp_user_name - ENV["NEMO_SMTP_USER_NAME"] + ENV.fetch("NEMO_SMTP_USER_NAME", nil) end def smtp_password - ENV["NEMO_SMTP_PASSWORD"] + ENV.fetch("NEMO_SMTP_PASSWORD", nil) end # Returns a hash of SMTP options, omitting anything that's blank @@ -102,15 +127,15 @@ def smtp_options end def google_maps_key - ENV["NEMO_GOOGLE_MAPS_API_KEY"] + ENV.fetch("NEMO_GOOGLE_MAPS_API_KEY", nil) end def scout_key - ENV["NEMO_SCOUT_KEY"] + ENV.fetch("NEMO_SCOUT_KEY", nil) end def sentry_dsn - ENV["NEMO_SENTRY_DSN"] + ENV.fetch("NEMO_SENTRY_DSN", nil) end def allow_missionless_sms? @@ -118,15 +143,15 @@ def allow_missionless_sms? end def universal_sms_token - allow_missionless_sms? ? ENV["NEMO_UNIVERSAL_SMS_TOKEN"] : nil + allow_missionless_sms? ? ENV.fetch("NEMO_UNIVERSAL_SMS_TOKEN", nil) : nil end def recaptcha_public_key - ENV["NEMO_RECAPTCHA_PUBLIC_KEY"] + ENV.fetch("NEMO_RECAPTCHA_PUBLIC_KEY", nil) end def recaptcha_private_key - ENV["NEMO_RECAPTCHA_PRIVATE_KEY"] + ENV.fetch("NEMO_RECAPTCHA_PRIVATE_KEY", nil) end def storage_service