From e313f9192e438d3e55da1e7572577ec03cca0366 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Thu, 31 Dec 2020 14:38:04 +0100 Subject: [PATCH 1/7] Make room for puppet modules specific methods --- lib/modulesync/{puppet_module.rb => source_code.rb} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename lib/modulesync/{puppet_module.rb => source_code.rb} (93%) diff --git a/lib/modulesync/puppet_module.rb b/lib/modulesync/source_code.rb similarity index 93% rename from lib/modulesync/puppet_module.rb rename to lib/modulesync/source_code.rb index d485c5e3..14012be6 100644 --- a/lib/modulesync/puppet_module.rb +++ b/lib/modulesync/source_code.rb @@ -2,8 +2,8 @@ require 'modulesync/util' module ModuleSync - # Provide methods to retrieve puppet module attributes - class PuppetModule + # Provide methods to retrieve source code attributes + class SourceCode attr_reader :given_name attr_reader :options From ed5a02d66e1b91c34270c0d7c7ebd7e471d97191 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Thu, 31 Dec 2020 14:49:17 +0100 Subject: [PATCH 2/7] Move Puppet module specific code into dedicated class --- lib/modulesync.rb | 5 +++++ lib/modulesync/puppet_module.rb | 37 +++++++++++++++++++++++++++++++++ lib/modulesync/repository.rb | 37 --------------------------------- 3 files changed, 42 insertions(+), 37 deletions(-) create mode 100644 lib/modulesync/puppet_module.rb diff --git a/lib/modulesync.rb b/lib/modulesync.rb index 2837ccfa..327d09f2 100644 --- a/lib/modulesync.rb +++ b/lib/modulesync.rb @@ -138,6 +138,11 @@ def self.manage_module(puppet_module, module_files, defaults) pr(puppet_module).manage(puppet_module.repository_namespace, puppet_module.repository_name, options) elsif !options[:offline] pushed = repository.submit_changes(files_to_manage, options) + # Only bump/tag if pushing didn't fail (i.e. there were changes) + if pushed && options[:bump] + new = puppet_module.bump(options[:message], options[:changelog]) + repository.tag(new, options[:tag_pattern]) if options[:tag] + end pushed && options[:pr] && \ pr(puppet_module).manage(puppet_module.repository_namespace, puppet_module.repository_name, options) end diff --git a/lib/modulesync/puppet_module.rb b/lib/modulesync/puppet_module.rb new file mode 100644 index 00000000..514250d9 --- /dev/null +++ b/lib/modulesync/puppet_module.rb @@ -0,0 +1,37 @@ +require 'puppet_blacksmith' + +require 'modulesync/source_code' + +module ModuleSync + # Provide methods to manipulate puppet module code + class PuppetModule < SourceCode + def update_changelog(version, message) + changelog = "#{working_directory}/CHANGELOG.md" + if File.exist?(changelog) + puts "Updating #{changelog} for version #{version}" + changes = File.readlines(changelog) + File.open(changelog, 'w') do |f| + date = Time.now.strftime('%Y-%m-%d') + f.puts "## #{date} - Release #{version}\n\n" + f.puts "#{message}\n\n" + # Add old lines again + f.puts changes + end + repository.git.add('CHANGELOG.md') + else + puts 'No CHANGELOG.md file found, not updating.' + end + end + + def bump(message, changelog = false) + m = Blacksmith::Modulefile.new("#{working_directory}/metadata.json") + new = m.bump! + puts "Bumped to version #{new}" + repository.git.add('metadata.json') + update_changelog(new, message) if changelog + repository.git.commit("Release version #{new}") + repository.git.push + new + end + end +end diff --git a/lib/modulesync/repository.rb b/lib/modulesync/repository.rb index 071b9d78..f5c1f8f9 100644 --- a/lib/modulesync/repository.rb +++ b/lib/modulesync/repository.rb @@ -1,5 +1,4 @@ require 'git' -require 'puppet_blacksmith' module ModuleSync # Wrapper for Git in ModuleSync context @@ -79,37 +78,6 @@ def prepare_workspace(branch) end end - # PuppetModule, is it used? - def update_changelog(version, message) - changelog = "#{@directory}/CHANGELOG.md" - if File.exist?(changelog) - puts "Updating #{changelog} for version #{version}" - changes = File.readlines(changelog) - File.open(changelog, 'w') do |f| - date = Time.now.strftime('%Y-%m-%d') - f.puts "## #{date} - Release #{version}\n\n" - f.puts "#{message}\n\n" - # Add old lines again - f.puts changes - end - repo.add('CHANGELOG.md') - else - puts 'No CHANGELOG.md file found, not updating.' - end - end - - # PuppetModule - def bump(message, changelog = false) - m = Blacksmith::Modulefile.new("#{@directory}/metadata.json") - new = m.bump! - puts "Bumped to version #{new}" - repo.add('metadata.json') - update_changelog(new, message) if changelog - repo.commit("Release version #{new}") - repo.push - new - end - def tag(version, tag_pattern) tag = tag_pattern % version puts "Tagging with #{tag}" @@ -151,11 +119,6 @@ def submit_changes(files, options) else repo.push('origin', branch, opts_push) end - # Only bump/tag if pushing didn't fail (i.e. there were changes) - if options[:bump] - new = bump(message, options[:changelog]) - tag(new, options[:tag_pattern]) if options[:tag] - end rescue Git::GitExecuteError => e if e.message.match?(/working (directory|tree) clean/) puts "There were no changes in '#{@directory}'. Not committing." From 9b369919348122e766b3259cb20aec6e927283f4 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Thu, 31 Dec 2020 15:04:54 +0100 Subject: [PATCH 3/7] Make SourceCode the owner of its repository --- lib/modulesync.rb | 10 ++++------ lib/modulesync/source_code.rb | 5 +++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/modulesync.rb b/lib/modulesync.rb index 327d09f2..c918fb84 100644 --- a/lib/modulesync.rb +++ b/lib/modulesync.rb @@ -3,7 +3,6 @@ require 'modulesync/cli' require 'modulesync/constants' -require 'modulesync/repository' require 'modulesync/hook' require 'modulesync/puppet_module' require 'modulesync/renderer' @@ -111,9 +110,8 @@ def self.manage_file(puppet_module, filename, settings, options) end def self.manage_module(puppet_module, module_files, defaults) - repository = Repository.new directory: puppet_module.working_directory, remote: puppet_module.repository_remote puts "Syncing '#{puppet_module.given_name}'" - repository.prepare_workspace(options[:branch]) unless options[:offline] + puppet_module.repository.prepare_workspace(options[:branch]) unless options[:offline] module_configs = Util.parse_config(module_file(puppet_module, MODULE_CONF_FILE)) settings = Settings.new(defaults[GLOBAL_DEFAULTS_KEY] || {}, @@ -133,15 +131,15 @@ def self.manage_module(puppet_module, module_files, defaults) if options[:noop] puts "Using no-op. Files in '#{puppet_module.given_name}' may be changed but will not be committed." - repository.show_changes(options) + puppet_module.repository.show_changes(options) options[:pr] && \ pr(puppet_module).manage(puppet_module.repository_namespace, puppet_module.repository_name, options) elsif !options[:offline] - pushed = repository.submit_changes(files_to_manage, options) + pushed = puppet_module.repository.submit_changes(files_to_manage, options) # Only bump/tag if pushing didn't fail (i.e. there were changes) if pushed && options[:bump] new = puppet_module.bump(options[:message], options[:changelog]) - repository.tag(new, options[:tag_pattern]) if options[:tag] + puppet_module.repository.tag(new, options[:tag_pattern]) if options[:tag] end pushed && options[:pr] && \ pr(puppet_module).manage(puppet_module.repository_namespace, puppet_module.repository_name, options) diff --git a/lib/modulesync/source_code.rb b/lib/modulesync/source_code.rb index 14012be6..445f6622 100644 --- a/lib/modulesync/source_code.rb +++ b/lib/modulesync/source_code.rb @@ -1,4 +1,5 @@ require 'modulesync' +require 'modulesync/repository' require 'modulesync/util' module ModuleSync @@ -19,6 +20,10 @@ def initialize(given_name, options) @repository_namespace = given_name.split('/')[0...-1].join('/') end + def repository + @repository ||= Repository.new directory: working_directory, remote: repository_remote + end + def repository_name @repository_name ||= given_name end From 1cda568911e1e4192cc3f1b860514c4e0966485e Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Fri, 23 Apr 2021 08:49:22 +0200 Subject: [PATCH 4/7] SourceCode: Write options initialization in one-line style --- lib/modulesync/source_code.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/modulesync/source_code.rb b/lib/modulesync/source_code.rb index 445f6622..523581ab 100644 --- a/lib/modulesync/source_code.rb +++ b/lib/modulesync/source_code.rb @@ -9,8 +9,7 @@ class SourceCode attr_reader :options def initialize(given_name, options) - options ||= {} - @options = Util.symbolize_keys(options) + @options = Util.symbolize_keys(options || {}) @given_name = given_name From 722a7449839329eb6a9964d5f69e89f7a2c2062f Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 21 Apr 2021 22:55:17 +0200 Subject: [PATCH 5/7] SourceCode: Provide #path that computes the absolute filename of a file This method replaces the old `ModuleSync::module_file`. --- lib/modulesync.rb | 8 ++------ lib/modulesync/source_code.rb | 4 ++++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/modulesync.rb b/lib/modulesync.rb index c918fb84..fb31ee2a 100644 --- a/lib/modulesync.rb +++ b/lib/modulesync.rb @@ -31,10 +31,6 @@ def self.local_file(config_path, file) File.join(config_path, MODULE_FILES_DIR, file) end - def self.module_file(puppet_module, *parts) - File.join(puppet_module.working_directory, *parts) - end - # List all template files. # # Only select *.erb files, and strip the extension. This way all the code will only have to handle bare paths, @@ -87,7 +83,7 @@ def self.manage_file(puppet_module, filename, settings, options) namespace = settings.additional_settings[:namespace] module_name = settings.additional_settings[:puppet_module] configs = settings.build_file_configs(filename) - target_file = module_file(puppet_module, filename) + target_file = puppet_module.path filename if configs['delete'] Renderer.remove(target_file) else @@ -113,7 +109,7 @@ def self.manage_module(puppet_module, module_files, defaults) puts "Syncing '#{puppet_module.given_name}'" puppet_module.repository.prepare_workspace(options[:branch]) unless options[:offline] - module_configs = Util.parse_config(module_file(puppet_module, MODULE_CONF_FILE)) + module_configs = Util.parse_config puppet_module.path(MODULE_CONF_FILE) settings = Settings.new(defaults[GLOBAL_DEFAULTS_KEY] || {}, defaults, module_configs[GLOBAL_DEFAULTS_KEY] || {}, diff --git a/lib/modulesync/source_code.rb b/lib/modulesync/source_code.rb index 523581ab..7415d86b 100644 --- a/lib/modulesync/source_code.rb +++ b/lib/modulesync/source_code.rb @@ -43,6 +43,10 @@ def working_directory @working_directory ||= File.join(ModuleSync.options[:project_root], repository_path) end + def path(*parts) + File.join(working_directory, *parts) + end + private def _repository_remote From 5813e92d080156b8cdf7ee743227a44abaf144de Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Fri, 23 Apr 2021 09:03:30 +0200 Subject: [PATCH 6/7] PuppetCode: Use SourceCode#path when relevant --- lib/modulesync.rb | 2 +- lib/modulesync/puppet_module.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/modulesync.rb b/lib/modulesync.rb index fb31ee2a..8a9abfdd 100644 --- a/lib/modulesync.rb +++ b/lib/modulesync.rb @@ -83,7 +83,7 @@ def self.manage_file(puppet_module, filename, settings, options) namespace = settings.additional_settings[:namespace] module_name = settings.additional_settings[:puppet_module] configs = settings.build_file_configs(filename) - target_file = puppet_module.path filename + target_file = puppet_module.path(filename) if configs['delete'] Renderer.remove(target_file) else diff --git a/lib/modulesync/puppet_module.rb b/lib/modulesync/puppet_module.rb index 514250d9..7cb4710c 100644 --- a/lib/modulesync/puppet_module.rb +++ b/lib/modulesync/puppet_module.rb @@ -6,7 +6,7 @@ module ModuleSync # Provide methods to manipulate puppet module code class PuppetModule < SourceCode def update_changelog(version, message) - changelog = "#{working_directory}/CHANGELOG.md" + changelog = path('CHANGELOG.md') if File.exist?(changelog) puts "Updating #{changelog} for version #{version}" changes = File.readlines(changelog) @@ -24,7 +24,7 @@ def update_changelog(version, message) end def bump(message, changelog = false) - m = Blacksmith::Modulefile.new("#{working_directory}/metadata.json") + m = Blacksmith::Modulefile.new path('metadata.json') new = m.bump! puts "Bumped to version #{new}" repository.git.add('metadata.json') From 4e9f33083dfcbef017d768ff79537be32410876d Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Thu, 22 Apr 2021 16:26:07 +0200 Subject: [PATCH 7/7] Rubocop: Update autogenerated todo file --- .rubocop_todo.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 20ac188a..b69b2939 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2021-04-21 19:04:03 +0200 using RuboCop version 0.50.0. +# on 2021-04-22 16:30:35 +0200 using RuboCop version 0.50.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 @@ -13,12 +13,12 @@ Lint/UselessAssignment: # Offense count: 10 Metrics/AbcSize: - Max: 53 + Max: 67 # Offense count: 2 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 158 + Max: 128 # Offense count: 3 Metrics/CyclomaticComplexity: @@ -27,11 +27,11 @@ Metrics/CyclomaticComplexity: # Offense count: 13 # Configuration parameters: CountComments. Metrics/MethodLength: - Max: 40 + Max: 36 # Offense count: 3 Metrics/PerceivedComplexity: - Max: 15 + Max: 13 # Offense count: 8 Style/Documentation: