From 51bcebe6a68975f9e0f053e7a9ae7a0aa3e8415b Mon Sep 17 00:00:00 2001 From: Ryan Brandenburg Date: Fri, 12 Jul 2024 21:05:41 -0700 Subject: [PATCH] Sorbet for environment (#10204) --- updater/lib/dependabot/environment.rb | 63 ++++++++++++++++++++------- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/updater/lib/dependabot/environment.rb b/updater/lib/dependabot/environment.rb index 7069d877547..230423dd223 100644 --- a/updater/lib/dependabot/environment.rb +++ b/updater/lib/dependabot/environment.rb @@ -1,64 +1,97 @@ -# typed: true +# typed: strict # frozen_string_literal: true +require "sorbet-runtime" + module Dependabot module Environment + extend T::Sig + extend T::Generic + + sig { returns(String) } def self.job_id - @job_id ||= environment_variable("DEPENDABOT_JOB_ID") + @job_id ||= T.let(environment_variable("DEPENDABOT_JOB_ID"), T.nilable(String)) end + sig { returns(String) } def self.job_token - @job_token ||= environment_variable("DEPENDABOT_JOB_TOKEN") + @job_token ||= T.let(environment_variable("DEPENDABOT_JOB_TOKEN"), T.nilable(String)) end + sig { returns(T::Boolean) } def self.debug_enabled? - @debug_enabled ||= job_debug_enabled? || environment_debug_enabled? + @debug_enabled ||= T.let(job_debug_enabled? || environment_debug_enabled?, T.nilable(T::Boolean)) end + sig { returns(Symbol) } def self.log_level debug_enabled? ? :debug : :info end + sig { returns(String) } def self.api_url - @api_url ||= environment_variable("DEPENDABOT_API_URL", "http://localhost:3001") + @api_url ||= T.let(environment_variable("DEPENDABOT_API_URL", "http://localhost:3001"), T.nilable(String)) end + sig { returns(String) } def self.job_path - @job_path ||= environment_variable("DEPENDABOT_JOB_PATH") + @job_path ||= T.let(environment_variable("DEPENDABOT_JOB_PATH"), T.nilable(String)) end + sig { returns(String) } def self.output_path - @output_path ||= environment_variable("DEPENDABOT_OUTPUT_PATH") + @output_path ||= T.let(environment_variable("DEPENDABOT_OUTPUT_PATH"), T.nilable(String)) end + sig { returns(T.nilable(String)) } def self.repo_contents_path - @repo_contents_path ||= environment_variable("DEPENDABOT_REPO_CONTENTS_PATH", nil) + @repo_contents_path ||= T.let(environment_variable("DEPENDABOT_REPO_CONTENTS_PATH", nil), T.nilable(String)) end + sig { returns(T::Boolean) } def self.github_actions? - @github_actions ||= environment_variable("GITHUB_ACTIONS", false) + b = T.cast(environment_variable("GITHUB_ACTIONS", false), T::Boolean) + @github_actions ||= T.let(b, T.nilable(T::Boolean)) end + sig { returns(T::Boolean) } def self.deterministic_updates? - @deterministic_updates ||= environment_variable("UPDATER_DETERMINISTIC", false) + b = T.cast(environment_variable("UPDATER_DETERMINISTIC", false), T::Boolean) + @deterministic_updates ||= T.let(b, T.nilable(T::Boolean)) end + sig { returns(T::Hash[String, T.untyped]) } def self.job_definition - @job_definition ||= JSON.parse(File.read(job_path)) + @job_definition ||= T.let(JSON.parse(File.read(job_path)), T.nilable(T::Hash[String, T.untyped])) end + sig do + type_parameters(:T) + .params(variable_name: String, default: T.any(Symbol, T.type_parameter(:T))) + .returns(T.any(String, T.type_parameter(:T))) + end private_class_method def self.environment_variable(variable_name, default = :_undefined) - return ENV.fetch(variable_name, default) unless default == :_undefined - - ENV.fetch(variable_name) do - raise ArgumentError, "Missing environment variable #{variable_name}" + case default + when :_undefined + ENV.fetch(variable_name) do + raise ArgumentError, "Missing environment variable #{variable_name}" + end + else + val = ENV.fetch(variable_name, T.cast(default, T.type_parameter(:T))) + case val + when String + val = T.must(val.casecmp("true")).zero? if [true, false].include? default + end + T.cast(val, T.type_parameter(:T)) end end + sig { returns(T::Boolean) } private_class_method def self.job_debug_enabled? !!job_definition.dig("job", "debug") end + sig { returns(T::Boolean) } private_class_method def self.environment_debug_enabled? !!environment_variable("DEPENDABOT_DEBUG", false) end