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.



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

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.



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

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



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
71
# File 'lib/ra10ke/validate.rb', line 45

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



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

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



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

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