Skip to content

Commit

Permalink
rubocop: autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
bastelfreak committed Nov 14, 2024
1 parent c44f64b commit 77d92c1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 41 deletions.
4 changes: 2 additions & 2 deletions lib/onceover/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def deploy_local(repo = Onceover::Controlrepo.new, opts = {})
if auto_vendored
tmp_puppetfile = File.join(temp_controlrepo, 'Puppetfile')
tmp_puppetfile_contents = File.read(tmp_puppetfile)
vm = Onceover::VendoredModules.new({repo: repo})
vm = Onceover::VendoredModules.new({ repo: repo })
puppetfile = R10K::ModuleLoader::Puppetfile.new(basedir: temp_controlrepo)
vm.puppetfile_missing_vendored(puppetfile)
unless vm.missing_vendored.empty?
Expand All @@ -112,7 +112,7 @@ def deploy_local(repo = Onceover::Controlrepo.new, opts = {})
mod_slug = missing_mod.keys[0]
"mod '#{mod_slug}',\n git: '#{missing_mod[mod_slug][:git]}',\n ref: '#{missing_mod[mod_slug][:ref]}'"
end.join("\n")
File.write(tmp_puppetfile, tmp_puppetfile_contents + "\n# Onceover Managed Vendored Modules\n" + modlines)
File.write(tmp_puppetfile, "#{tmp_puppetfile_contents}\n# Onceover Managed Vendored Modules\n#{modlines}")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/onceover/rake_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,5 @@
require 'onceover/vendored_modules'

repo = Onceover::Controlrepo.new(debug: true)
Onceover::VendoredModules.new({repo: repo, cachedir: File.join(repo.spec_dir, 'vendored_modules'), force_update: true})
Onceover::VendoredModules.new({ repo: repo, cachedir: File.join(repo.spec_dir, 'vendored_modules'), force_update: true })
end
71 changes: 33 additions & 38 deletions lib/onceover/vendored_modules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

class Onceover
class VendoredModules

attr_reader :vendored_references, :missing_vendored

def initialize(opts = {})
Expand Down Expand Up @@ -51,11 +50,11 @@ def initialize(opts = {})
# https://docs.github.com/en/rest/git/trees?apiVersion=2022-11-28#get-a-tree
puppet_agent_tree = query_or_cache(
"https://api.github.com/repos/puppetlabs/puppet-agent/git/trees/#{@puppet_version}",
{ :recursive => true },
component_cache('repo_tree')
{ recursive: true },
component_cache('repo_tree'),
)
# Get only the module-puppetlabs-<something>_core.json component files
vendored_components = puppet_agent_tree['tree'].select { |file| /configs\/components\/module-puppetlabs-\w+\.json/.match(file['path']) }
vendored_components = puppet_agent_tree['tree'].select { |file| %r{configs/components/module-puppetlabs-\w+\.json}.match(file['path']) }
# Get the contents of each component file
# https://docs.github.com/en/rest/git/blobs?apiVersion=2022-11-28#get-a-blob
@vendored_references = vendored_components.map do |component|
Expand All @@ -64,7 +63,7 @@ def initialize(opts = {})
query_or_cache(
component['url'],
nil,
component_cache(mod_name)
component_cache(mod_name),
)
end
end
Expand All @@ -75,42 +74,38 @@ def component_cache(component)
# By default look for any caches created during previous runs
cache_file = File.join(@cachedir, desired_name)

unless @force_update
# If the user provides their own cache
if File.directory?(@manual_vendored_dir)
# Check for any '<component>-puppet_agent-<puppet version>.json' files
dg = Dir.glob(File.join(@manual_vendored_dir, "#{component}-puppet_agent*"))
# Check if there are multiple versions of the component cache
if dg.size > 1
# If there is the same version supplied as whats being tested against use that
if dg.any? { |s| s[desired_name] }
cache_file = File.join(@manual_vendored_dir, desired_name)
# If there are any with the same major version, use the latest supplied
elsif dg.any? { |s| s["#{component}-puppet_agent-#{@puppet_major_version}"] }
maj_match = dg.select { |f| /#{component}-puppet_agent-#{@puppet_major_version}.\d+\.\d+\.json/.match(f) }
maj_match.each do |f|
if (version_from_file(cache_file) == version_from_file(desired_name)) || (version_from_file(f) >= version_from_file(cache_file))
# if the current cache version matches the desired version, use the first matching major version in user cache
# if there are multiple major version matches in user cache, use the latest
cache_file = f
# If the user provides their own cache
if !@force_update && File.directory?(@manual_vendored_dir)
# Check for any '<component>-puppet_agent-<puppet version>.json' files
dg = Dir.glob(File.join(@manual_vendored_dir, "#{component}-puppet_agent*"))
# Check if there are multiple versions of the component cache
if dg.size > 1
# If there is the same version supplied as whats being tested against use that
if dg.any? { |s| s[desired_name] }
cache_file = File.join(@manual_vendored_dir, desired_name)
# If there are any with the same major version, use the latest supplied
elsif dg.any? { |s| s["#{component}-puppet_agent-#{@puppet_major_version}"] }
maj_match = dg.select { |f| /#{component}-puppet_agent-#{@puppet_major_version}.\d+\.\d+\.json/.match(f) }
maj_match.each do |f|
next unless (version_from_file(cache_file) == version_from_file(desired_name)) || (version_from_file(f) >= version_from_file(cache_file))

# if the current cache version matches the desired version, use the first matching major version in user cache
# if there are multiple major version matches in user cache, use the latest
cache_file = f
end
# Otherwise just use the latest supplied
else
dg.each { |f| cache_file = f if version_from_file(f) >= version_from_file(cache_file) }
end
# If there is only one use that
elsif dg.size == 1
cache_file = dg[0]
end
end
# Otherwise just use the latest supplied
else
dg.each { |f| cache_file = f if version_from_file(f) >= version_from_file(cache_file) }
end
# If there is only one use that
elsif dg.size == 1
cache_file = dg[0]
end
end
end

# Warn the user if cached version does not match whats being used to test
cache_version = version_from_file(cache_file)
if cache_version != @puppet_version
logger.warn "Cache for #{component} is for puppet_agent #{cache_version}, while you are testing against puppet_agent #{@puppet_version}. Consider updating your cache to ensure consistent behavior in your tests"
end
logger.warn "Cache for #{component} is for puppet_agent #{cache_version}, while you are testing against puppet_agent #{@puppet_version}. Consider updating your cache to ensure consistent behavior in your tests" if cache_version != @puppet_version

cache_file
end
Expand All @@ -134,7 +129,7 @@ def puppetfile_missing_vendored(puppetfile)
# Change url to https instead of ssh to allow anonymous git clones
# so that users do not need to have an ssh keypair associated with a Github account
url = mod['url'].gsub('[email protected]:', 'https://github.com/')
@missing_vendored << {mod_slug => {git: url, ref: mod['ref']}}
@missing_vendored << { mod_slug => { git: url, ref: mod['ref'] } }
logger.debug "#{mod_name} found to be missing in Puppetfile"
else
logger.debug "#{mod_name} found in Puppetfile. Using the specified version"
Expand Down Expand Up @@ -173,7 +168,7 @@ def github_get(url, params)
else
# Expose the ratelimit response headers
# https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#checking-the-status-of-your-rate-limit
ratelimit_headers = response.to_hash.select { |k, v| k =~ /x-ratelimit.*/ }
ratelimit_headers = response.to_hash.select { |k, _v| k =~ /x-ratelimit.*/ }
raise "#{response.code} #{response.message} #{ratelimit_headers}"
end
end
Expand Down

0 comments on commit 77d92c1

Please sign in to comment.