lib/modulesync.rb in modulesync-1.0.0 vs lib/modulesync.rb in modulesync-1.1.0
- old
+ new
@@ -14,11 +14,11 @@
Octokit.configure do |c|
c.api_endpoint = ENV.fetch('GITHUB_BASE_URL', 'https://api.github.com')
end
-module ModuleSync
+module ModuleSync # rubocop:disable Metrics/ModuleLength
include Constants
def self.config_defaults
{
:project_root => 'modules/',
@@ -30,12 +30,12 @@
def self.local_file(config_path, file)
File.join(config_path, MODULE_FILES_DIR, file)
end
- def self.module_file(project_root, namespace, puppet_module, file)
- File.join(project_root, namespace, puppet_module, file)
+ def self.module_file(project_root, namespace, puppet_module, *parts)
+ File.join(project_root, namespace, puppet_module, *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,
@@ -44,11 +44,11 @@
if File.exist?(local_template_dir)
Find.find(local_template_dir).find_all { |p| p =~ /.erb$/ && !File.directory?(p) }
.collect { |p| p.chomp('.erb') }
.to_a
else
- puts "#{local_template_dir} does not exist." \
+ $stdout.puts "#{local_template_dir} does not exist." \
' Check that you are working in your module configs directory or' \
' that you have passed in the correct directory with -c.'
exit
end
end
@@ -58,11 +58,11 @@
end
def self.managed_modules(config_file, filter, negative_filter)
managed_modules = Util.parse_config(config_file)
if managed_modules.empty?
- puts "No modules found in #{config_file}." \
+ $stdout.puts "No modules found in #{config_file}." \
' Check that you specified the right :configs directory and :managed_modules_conf file.'
exit
end
managed_modules.select! { |m| m =~ Regexp.new(filter) } unless filter.nil?
managed_modules.reject! { |m| m =~ Regexp.new(negative_filter) } unless negative_filter.nil?
@@ -87,31 +87,33 @@
def self.manage_file(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(options[:project_root], namespace, module_name, filename)
if configs['delete']
- Renderer.remove(module_file(options[:project_root], namespace, module_name, filename))
+ Renderer.remove(target_file)
else
templatename = local_file(options[:configs], filename)
begin
erb = Renderer.build(templatename)
- template = Renderer.render(erb, configs)
- Renderer.sync(template, module_file(options[:project_root], namespace, module_name, filename))
+ # Meta data passed to the template as @metadata[:name]
+ metadata = {
+ :module_name => module_name,
+ :workdir => module_file(options[:project_root], namespace, module_name),
+ :target_file => target_file,
+ }
+ template = Renderer.render(erb, configs, metadata)
+ Renderer.sync(template, target_file)
rescue # rubocop:disable Lint/RescueWithoutErrorClass
- STDERR.puts "Error while rendering #{filename}"
+ $stderr.puts "Error while rendering #{filename}"
raise
end
end
end
def self.manage_module(puppet_module, module_files, module_options, defaults, options)
- if options[:pr] && !GITHUB_TOKEN
- STDERR.puts 'Environment variable GITHUB_TOKEN must be set to use --pr!'
- raise unless options[:skip_broken]
- end
-
namespace, module_name = module_name(puppet_module, options[:namespace])
git_repo = File.join(namespace, module_name)
unless options[:offline]
Git.pull(options[:git_base], git_repo, options[:branch], options[:project_root], module_options || {})
end
@@ -123,11 +125,11 @@
module_configs,
:puppet_module => module_name,
:git_base => options[:git_base],
:namespace => namespace)
settings.unmanaged_files(module_files).each do |filename|
- puts "Not managing #{filename} in #{module_name}"
+ $stdout.puts "Not managing #{filename} in #{module_name}"
end
files_to_manage = settings.managed_files(module_files)
files_to_manage.each { |filename| manage_file(filename, settings, options) }
@@ -136,28 +138,43 @@
elsif !options[:offline]
# Git.update() returns a boolean: true if files were pushed, false if not.
pushed = Git.update(git_repo, files_to_manage, options)
return nil unless pushed && options[:pr]
- # We only do GitHub PR work if the GITHUB_TOKEN variable is set in the environment.
- repo_path = File.join(namespace, module_name)
- puts "Submitting PR '#{options[:pr_title]}' on GitHub to #{repo_path} - merges #{options[:branch]} into master"
- github = Octokit::Client.new(:access_token => GITHUB_TOKEN)
- pr = github.create_pull_request(repo_path, 'master', options[:branch], options[:pr_title], options[:message])
- puts "PR created at #{pr['html_url']}"
+ manage_pr(namespace, module_name, options)
+ end
+ end
- # PR labels can either be a list in the YAML file or they can pass in a comma
- # separated list via the command line argument.
- pr_labels = Util.parse_list(options[:pr_labels])
+ def self.manage_pr(namespace, module_name, options)
+ if options[:pr] && GITHUB_TOKEN.empty?
+ $stderr.puts 'Environment variable GITHUB_TOKEN must be set to use --pr!'
+ raise unless options[:skip_broken]
+ end
- # We only assign labels to the PR if we've discovered a list > 1. The labels MUST
- # already exist. We DO NOT create missing labels.
- unless pr_labels.empty?
- puts "Attaching the following labels to PR #{pr['number']}: #{pr_labels.join(', ')}"
- github.add_labels_to_an_issue(repo_path, pr['number'], pr_labels)
- end
+ # We only do GitHub PR work if the GITHUB_TOKEN variable is set in the environment.
+ repo_path = File.join(namespace, module_name)
+ github = Octokit::Client.new(:access_token => GITHUB_TOKEN)
+
+ # Skip creating the PR if it exists already.
+ head = "#{namespace}:#{options[:branch]}"
+ pull_requests = github.pull_requests(repo_path, :state => 'open', :base => 'master', :head => head)
+ if pull_requests.empty?
+ pr = github.create_pull_request(repo_path, 'master', options[:branch], options[:pr_title], options[:message])
+ $stdout.puts "Submitted PR '#{options[:pr_title]}' to #{repo_path} - merges #{options[:branch]} into master"
+ else
+ $stdout.puts "Skipped! #{pull_requests.length} PRs found for branch #{options[:branch]}"
end
+
+ # PR labels can either be a list in the YAML file or they can pass in a comma
+ # separated list via the command line argument.
+ pr_labels = Util.parse_list(options[:pr_labels])
+
+ # We only assign labels to the PR if we've discovered a list > 1. The labels MUST
+ # already exist. We DO NOT create missing labels.
+ return if pr_labels.empty?
+ $stdout.puts "Attaching the following labels to PR #{pr['number']}: #{pr_labels.join(', ')}"
+ github.add_labels_to_an_issue(repo_path, pr['number'], pr_labels)
end
def self.update(options)
options = config_defaults.merge(options)
defaults = Util.parse_config(File.join(options[:configs], CONF_FILE))
@@ -174,13 +191,13 @@
# managed_modules is either an array or a hash
managed_modules.each do |puppet_module, module_options|
begin
manage_module(puppet_module, module_files, module_options, defaults, options)
rescue # rubocop:disable Lint/RescueWithoutErrorClass
- STDERR.puts "Error while updating #{puppet_module}"
+ $stderr.puts "Error while updating #{puppet_module}"
raise unless options[:skip_broken]
errors = true
- puts "Skipping #{puppet_module} as update process failed"
+ $stdout.puts "Skipping #{puppet_module} as update process failed"
end
end
exit 1 if errors && options[:fail_on_warnings]
end
end