Class: RailVerdict::WaiverStore

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

Constant Summary collapse

DEFAULT_FILENAME =
".railverdict-waivers.json"

Class Method Summary collapse

Class Method Details

.default_path(repository_root) ⇒ Object



10
11
12
# File 'lib/rail_verdict/waiver_store.rb', line 10

def self.default_path(repository_root)
  File.join(File.realpath(repository_root), DEFAULT_FILENAME)
end

.read(path) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rail_verdict/waiver_store.rb', line 27

def self.read(path)
  unless File.file?(path)
    raise Waiver::IncompatibleError, "waiver file not found at #{path}"
  end
  raw = File.binread(path)
  begin
    data = JSON.parse(raw)
  rescue JSON::ParserError => error
    raise Waiver::IncompatibleError, "waiver file at #{path} is corrupted: #{error.message}; fix the file or remove the waiver"
  end
  errors = SchemaValidator.validate_waivers(data)
  unless errors.empty?
    raise Waiver::IncompatibleError, "waiver file at #{path} is incompatible: #{errors.join('; ')}; fix the file or remove the waiver"
  end
  seen = {}
  data.fetch("waivers").each do |waiver|
    fingerprint = waiver.fetch("fingerprint")
    if seen.key?(fingerprint)
      raise Waiver::IncompatibleError, "waiver file at #{path} contains duplicate fingerprint #{fingerprint}"
    end
    seen[fingerprint] = true
    Waiver.validate_hash(waiver)
  end
  data["waivers"].sort_by { |waiver| waiver.fetch("fingerprint") }.map { |hash| Waiver.new(hash) }
end

.read_optional(path) ⇒ Object



53
54
55
56
57
# File 'lib/rail_verdict/waiver_store.rb', line 53

def self.read_optional(path)
  return [] unless File.file?(path)

  read(path)
end

.resolve_path(repository_root:, configuration:, waiver_override: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rail_verdict/waiver_store.rb', line 14

def self.resolve_path(repository_root:, configuration:, waiver_override: nil)
  root = File.realpath(repository_root)
  if waiver_override && !waiver_override.to_s.strip.empty?
    path = waiver_override.to_s
    return Pathname.new(path).absolute? ? path : File.expand_path(path, root)
  end
  if configuration.respond_to?(:waivers_path) && configuration.waivers_path
    configured = configuration.waivers_path.to_s
    return Pathname.new(configured).absolute? ? configured : File.expand_path(configured, root)
  end
  File.join(root, DEFAULT_FILENAME)
end