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 a78af97
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 55 deletions.
21 changes: 7 additions & 14 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-11-14 21:32:23 UTC using RuboCop version 1.67.0.
# on 2024-11-14 22:37:24 UTC using RuboCop version 1.67.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -22,13 +22,6 @@ Gemspec/AddRuntimeDependency:
Exclude:
- 'onceover.gemspec'

# Offense count: 1
# Configuration parameters: Severity, Include.
# Include: **/*.gemspec
Gemspec/RequiredRubyVersion:
Exclude:
- 'onceover.gemspec'

# Offense count: 2
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyleAlignWith.
Expand Down Expand Up @@ -146,12 +139,13 @@ Layout/HeredocIndentation:
- 'lib/onceover/cli/show.rb'
- 'lib/onceover/cli/update.rb'

# Offense count: 7
# Offense count: 8
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: Width, AllowedPatterns.
Layout/IndentationWidth:
Exclude:
- 'lib/onceover/controlrepo.rb'
- 'lib/onceover/vendored_modules.rb'

# Offense count: 12
# This cop supports safe autocorrection (--autocorrect).
Expand Down Expand Up @@ -287,14 +281,13 @@ Layout/TrailingEmptyLines:
- 'features/step_definitions/formatter.rb'
- 'features/support/env.rb'

# Offense count: 9
# Offense count: 8
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowInHeredoc.
Layout/TrailingWhitespace:
Exclude:
- 'features/step_definitions/common.rb'
- 'features/step_definitions/formatter.rb'
- 'lib/onceover/deploy.rb'
- 'lib/onceover/node.rb'
- 'lib/onceover/rspec/formatters.rb'
- 'lib/onceover/testconfig.rb'
Expand Down Expand Up @@ -544,7 +537,7 @@ Style/ConditionalAssignment:
- 'lib/onceover/test.rb'
- 'lib/onceover/testconfig.rb'

# Offense count: 26
# Offense count: 27
# Configuration parameters: AllowedConstants.
Style/Documentation:
Enabled: false
Expand Down Expand Up @@ -578,7 +571,7 @@ Style/Encoding:
Exclude:
- 'onceover.gemspec'

# Offense count: 33
# Offense count: 34
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Expand Down Expand Up @@ -943,7 +936,7 @@ Style/WhileUntilDo:
Exclude:
- 'lib/onceover/controlrepo.rb'

# Offense count: 24
# Offense count: 32
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
# URISchemes: http, https
Expand Down
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 a78af97

Please sign in to comment.