Class: Ra10ke::Validate::Validation

Inherits:
Object
  • Object
show all
Includes:
PuppetfileParser
Defined in:
lib/ra10ke/validate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PuppetfileParser

#forge_modules, #git_modules, #modules, #parse_module_args, #process_args

Constructor Details

#initialize(file) ⇒ Validation

Returns a new instance of Validation.



37
38
39
40
41
# File 'lib/ra10ke/validate.rb', line 37

def initialize(file)
  file ||= './Puppetfile'
  @puppetfile = File.expand_path(file)
  abort("Puppetfile does not exist at #{puppetfile}") unless File.readable?(puppetfile)
end

Instance Attribute Details

#puppetfileObject (readonly)

Returns the value of attribute puppetfile.



35
36
37
# File 'lib/ra10ke/validate.rb', line 35

def puppetfile
  @puppetfile
end

Instance Method Details

#all_modulesArray[Hash]

Returns array of module information and git status.

Returns:

  • (Array[Hash])

    array of module information and git status



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ra10ke/validate.rb', line 44

def all_modules
  @all_modules ||= begin
    r10k_branch = Ra10ke::GitRepo.current_branch(File.dirname(puppetfile))
    git_modules(puppetfile).map do |mod|
      repo = Ra10ke::GitRepo.new(mod[:args][:git])
      ref = mod[:args][:ref] || mod[:args][:tag] || mod[:args][:branch] || mod[:args][:commit]
      # If using control_branch, try to guesstimate what the target branch should be
      if ref == ':control_branch'
        ref = ENV['CONTROL_BRANCH'] \
              || r10k_branch \
              || mod[:args][:default_branch_override] \
              || ENV['CONTROL_BRANCH_FALLBACK'] \
              || mod[:args][:default_branch] \
              || 'main'
      end
      valid_ref = repo.valid_ref?(ref) || repo.valid_commit?(mod[:args][:ref])
      {
        name: mod[:name],
        url: repo.url,
        ref: ref,
        valid_url?: repo.valid_url?,
        valid_ref?: valid_ref,
        status: valid_ref ? Ra10ke::Validate::GOOD_EMOJI : Ra10ke::Validate::BAD_EMOJI,
      }
    end
  end
end

#bad_mods?Boolean

Returns - true if there are any bad mods.

Returns:

  • (Boolean)
    • true if there are any bad mods



73
74
75
# File 'lib/ra10ke/validate.rb', line 73

def bad_mods?
  all_modules.find_all { |mod| !mod[:valid_ref?] }.count > 0
end

#sorted_modsHash

Returns - sorts the mods based on good/bad.

Returns:

  • (Hash)
    • sorts the mods based on good/bad



78
79
80
# File 'lib/ra10ke/validate.rb', line 78

def sorted_mods
  all_modules.sort_by { |a| a[:valid_ref?] ? 1 : 0 }
end