Class: RailVerdict::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/rail_verdict/configuration.rb

Constant Summary collapse

DEFAULT_FILENAME =
".railverdict.yml"
CONFIGURATION_VERSION =
1.5
SUPPORTED_VERSIONS =
[1, 1.1, 1.2, 1.3, 1.4, 1.5].freeze
DEFAULT_ANALYZER_TIMEOUT_SECONDS =
30
UTF8_BOM =
"\xEF\xBB\xBF".b

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, source_path, bytes) ⇒ Configuration

Returns a new instance of Configuration.



79
80
81
82
83
84
85
86
87
# File 'lib/rail_verdict/configuration.rb', line 79

def initialize(data, source_path, bytes)
  @version = data.fetch("version")
  @mode = data.fetch("mode").freeze
  @analyzers = deep_freeze(data.fetch("analyzers"))
  @raw_data = deep_freeze(data.dup)
  @source_path = source_path.dup.freeze
  @digest = Digest::SHA256.hexdigest(bytes).freeze
  freeze
end

Instance Attribute Details

#analyzersObject (readonly)

Returns the value of attribute analyzers.



13
14
15
# File 'lib/rail_verdict/configuration.rb', line 13

def analyzers
  @analyzers
end

#digestObject (readonly)

Returns the value of attribute digest.



13
14
15
# File 'lib/rail_verdict/configuration.rb', line 13

def digest
  @digest
end

#modeObject (readonly)

Returns the value of attribute mode.



13
14
15
# File 'lib/rail_verdict/configuration.rb', line 13

def mode
  @mode
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



13
14
15
# File 'lib/rail_verdict/configuration.rb', line 13

def source_path
  @source_path
end

#versionObject (readonly)

Returns the value of attribute version.



13
14
15
# File 'lib/rail_verdict/configuration.rb', line 13

def version
  @version
end

Class Method Details

.decode_utf8(bytes, source_path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rail_verdict/configuration.rb', line 31

def self.decode_utf8(bytes, source_path)
  if bytes.start_with?(UTF8_BOM)
    raise ConfigurationError.new(
      "#{source_path}: configuration must be UTF-8 without a byte order mark",
      source_path: source_path
    )
  end

  text = bytes.dup.force_encoding(Encoding::UTF_8)
  unless text.valid_encoding?
    raise ConfigurationError.new(
      "#{source_path}: configuration must be valid UTF-8",
      source_path: source_path
    )
  end

  text
end

.load(path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rail_verdict/configuration.rb', line 15

def self.load(path)
  source_path = path.to_s
  unless File.file?(source_path)
    raise ConfigurationError.new(
      "configuration file not found: #{source_path}",
      source_path: source_path
    )
  end

  bytes = File.binread(source_path)
  text = decode_utf8(bytes, source_path)
  data = load_strict_yaml(text, source_path)
  validate_schema(data, source_path)
  new(data, source_path, bytes)
end

.load_strict_yaml(text, source_path) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/rail_verdict/configuration.rb', line 50

def self.load_strict_yaml(text, source_path)
  StrictYaml.parse(text, source_path)
rescue StrictYaml::Error => error
  raise ConfigurationError.new(
    "#{error.message}",
    source_path: source_path,
    property_path: error.property_path
  )
end

.validate_schema(data, source_path) ⇒ Object

Raises:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rail_verdict/configuration.rb', line 60

def self.validate_schema(data, source_path)
  unless data.is_a?(Hash)
    raise ConfigurationError.new(
      "#{source_path}: $: configuration must be a mapping",
      source_path: source_path,
      property_path: "$"
    )
  end

  errors = SchemaValidator.validate_configuration(data)
  return if errors.empty?

  raise ConfigurationError.new(
    "invalid configuration #{source_path}: #{errors.join('; ')}",
    source_path: source_path,
    property_path: errors.first.to_s.split(":").first
  )
end

Instance Method Details

#ai_budgetsObject



150
151
152
# File 'lib/rail_verdict/configuration.rb', line 150

def ai_budgets
  ai_config&.fetch("budgets", {}) || {}
end

#ai_cache_enabled?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/rail_verdict/configuration.rb', line 154

def ai_cache_enabled?
  ai_config&.dig("cache", "enabled") == true
end

#ai_configObject



130
131
132
# File 'lib/rail_verdict/configuration.rb', line 130

def ai_config
  @raw_data&.dig("ai")
end

#ai_enabled?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/rail_verdict/configuration.rb', line 134

def ai_enabled?
  ai_config&.fetch("enabled", false) == true
end

#ai_modeObject



138
139
140
# File 'lib/rail_verdict/configuration.rb', line 138

def ai_mode
  ai_config&.fetch("mode", "off")
end

#ai_remote_enabled?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/rail_verdict/configuration.rb', line 142

def ai_remote_enabled?
  ai_enabled? && ai_config&.dig("remote", "enabled") == true
end

#ai_remote_trustObject



146
147
148
# File 'lib/rail_verdict/configuration.rb', line 146

def ai_remote_trust
  ai_config&.dig("remote", "trust") || "redacted"
end

#analyzer_enabled?(name) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
# File 'lib/rail_verdict/configuration.rb', line 93

def analyzer_enabled?(name)
  selection = @analyzers[name]
  return false if selection.nil?

  selection.fetch("enabled")
end

#analyzer_present?(name) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/rail_verdict/configuration.rb', line 104

def analyzer_present?(name)
  @analyzers.key?(name)
end

#analyzer_required?(name) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/rail_verdict/configuration.rb', line 100

def analyzer_required?(name)
  @analyzers.fetch(name).fetch("required")
end

#analyzer_selection(name) ⇒ Object



89
90
91
# File 'lib/rail_verdict/configuration.rb', line 89

def analyzer_selection(name)
  @analyzers.fetch(name)
end

#analyzer_timeout_seconds(name) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/rail_verdict/configuration.rb', line 116

def analyzer_timeout_seconds(name)
  selection = @analyzers[name.to_s]
  return DEFAULT_ANALYZER_TIMEOUT_SECONDS unless selection

  value = selection["timeout_seconds"]
  return DEFAULT_ANALYZER_TIMEOUT_SECONDS if value.nil?

  Integer(value)
end

#baseline_pathObject



108
109
110
# File 'lib/rail_verdict/configuration.rb', line 108

def baseline_path
  @raw_data&.dig("baseline", "path")
end

#git_baseObject



126
127
128
# File 'lib/rail_verdict/configuration.rb', line 126

def git_base
  @raw_data&.dig("git", "base")
end

#waivers_pathObject



112
113
114
# File 'lib/rail_verdict/configuration.rb', line 112

def waivers_path
  @raw_data&.dig("waivers", "path")
end