Skip to content

Commit

Permalink
Merge pull request #39 from selectiveci/implement-config-validation
Browse files Browse the repository at this point in the history
Implement basic config validation
  • Loading branch information
benjaminwood authored Jan 12, 2024
2 parents 8ca7029 + fae3962 commit f29a642
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/bin/build_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ fi
# Output the JSON
cat <<EOF
{
"api_key": "$SELECTIVE_API_KEY",
"host": "${SELECTIVE_HOST:-wss://app.selective.ci}",
"platform": "$platform",
"branch": "$branch",
"pr_title": "$SELECTIVE_PR_TITLE",
Expand Down
31 changes: 24 additions & 7 deletions lib/selective/ruby/core/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ class Controller
include Helper
@@selective_suppress_reporting = false

REQUIRED_CONFIGURATION = {
"host" => "SELECTIVE_HOST",
"api_key" => "SELECTIVE_API_KEY",
"platform" => "SELECTIVE_PLATFORM",
"run_id" => "SELECTIVE_RUN_ID",
"branch" => "SELECTIVE_BRANCH"
}.freeze

def initialize(runner, debug: false, log: false)
@debug = debug
@runner = runner
Expand Down Expand Up @@ -101,12 +109,8 @@ def generate_runner_id

def transport_url(reconnect: false)
@transport_url ||= begin
api_key = ENV.fetch("SELECTIVE_API_KEY")
host = ENV.fetch("SELECTIVE_HOST", "wss://app.selective.ci")

# Validate that host is a valid websocket url(starts with ws:// or wss://)
raise "Invalid host: #{host}" unless host.match?(/^wss?:\/\//)

api_key = build_env.delete("api_key")
host = build_env.delete("host")
run_id = build_env.delete("run_id")
run_attempt = build_env.delete("run_attempt")
run_attempt = SecureRandom.uuid if run_attempt.nil? || run_attempt.empty?
Expand Down Expand Up @@ -134,7 +138,20 @@ def transport_url(reconnect: false)
def build_env
@build_env ||= begin
result = `#{File.join(ROOT_GEM_PATH, "lib", "bin", "build_env.sh")}`
JSON.parse(result)
JSON.parse(result).tap do |env|
validate_build_env(env)
end
end
end

def validate_build_env(env)
missing = REQUIRED_CONFIGURATION.each_with_object([]) do |(key, env_var), arry|
arry << env_var if env[key].nil? || env[key].empty?
end

with_error_handling do
raise "Missing required environment variables: #{missing.join(", ")}" unless missing.empty?
raise "Invalid host: #{env['host']}" unless env['host'].match?(/^wss?:\/\//)
end
end

Expand Down
11 changes: 11 additions & 0 deletions spec/selective/ruby/core/controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@
end
end

describe "#validate_build_env" do
before do
allow(controller).to receive(:puts_indented)
end

it 'raises an error when required configuration is missing' do
controller.validate_build_env({})
expect(controller).to have_received(:puts_indented).with(/Missing required environment variables: SELECTIVE_HOST/)
end
end

describe "exec" do
context "when an error occurs" do
before do
Expand Down

0 comments on commit f29a642

Please sign in to comment.