From a78af97cb537b42661f190ecaaade58a8f6c5344 Mon Sep 17 00:00:00 2001 From: Tim Meusel Date: Thu, 14 Nov 2024 23:29:19 +0100 Subject: [PATCH] rubocop: autofix --- .rubocop_todo.yml | 21 ++++------ lib/onceover/deploy.rb | 4 +- lib/onceover/rake_tasks.rb | 2 +- lib/onceover/vendored_modules.rb | 71 +++++++++++++++----------------- 4 files changed, 43 insertions(+), 55 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 919de1f..f2724ef 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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 @@ -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. @@ -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). @@ -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' @@ -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 @@ -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 @@ -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 diff --git a/lib/onceover/deploy.rb b/lib/onceover/deploy.rb index 34c8ce6..4bc7b11 100644 --- a/lib/onceover/deploy.rb +++ b/lib/onceover/deploy.rb @@ -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? @@ -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 diff --git a/lib/onceover/rake_tasks.rb b/lib/onceover/rake_tasks.rb index adb2250..ea00e35 100644 --- a/lib/onceover/rake_tasks.rb +++ b/lib/onceover/rake_tasks.rb @@ -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 diff --git a/lib/onceover/vendored_modules.rb b/lib/onceover/vendored_modules.rb index c974a61..ac2caa0 100644 --- a/lib/onceover/vendored_modules.rb +++ b/lib/onceover/vendored_modules.rb @@ -22,7 +22,6 @@ class Onceover class VendoredModules - attr_reader :vendored_references, :missing_vendored def initialize(opts = {}) @@ -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-_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| @@ -64,7 +63,7 @@ def initialize(opts = {}) query_or_cache( component['url'], nil, - component_cache(mod_name) + component_cache(mod_name), ) end end @@ -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 '-puppet_agent-.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 '-puppet_agent-.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 @@ -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('git@github.com:', '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" @@ -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