Skip to content
This repository has been archived by the owner on May 10, 2021. It is now read-only.

Commit

Permalink
CD-3202 Updating code to appease rubocop.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Johnston committed Sep 13, 2018
1 parent c465dc9 commit ebf407f
Show file tree
Hide file tree
Showing 45 changed files with 101 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AllCops:
TargetRubyVersion: 2.1.5
TargetRubyVersion: 2.3.3

inherit_from: .rubocop_todo.yml

Expand Down
6 changes: 0 additions & 6 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ Metrics/BlockLength:
# Offense count: 5
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect.
Performance/HashEachMethods:
Exclude:
- 'lib/jenkins_pipeline_builder/cli/list.rb'
- 'lib/jenkins_pipeline_builder/compiler.rb'
- 'lib/jenkins_pipeline_builder/extensions.rb'
- 'lib/jenkins_pipeline_builder/utils.rb'

# Offense count: 1
# Configuration parameters: MinBodyLength.
Expand Down
3 changes: 1 addition & 2 deletions jenkins_pipeline_builder.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'jenkins_pipeline_builder/version'

Expand Down
2 changes: 1 addition & 1 deletion lib/jenkins_pipeline_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class << self
attr_reader :client, :credentials, :debug, :file_mode
attr_writer :logger
def generator
@_generator ||= Generator.new
@generator ||= Generator.new
end

def file_mode!
Expand Down
5 changes: 3 additions & 2 deletions lib/jenkins_pipeline_builder/cli/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def self.valid_cli_creds?(options)
def self.process_creds_file(file)
return load File.expand_path(file) if file.end_with? 'rb'
return self.jenkins_api_creds = JSON.parse(IO.read(File.expand_path(file))) if file.end_with? 'json'

self.jenkins_api_creds = YAML.load_file(File.expand_path(file))
end

Expand All @@ -97,14 +98,13 @@ def self.process_cli_creds(options)
end
end

private_class_method

def self.find_default_file
default_file_name = "#{ENV['HOME']}/.jenkins_api_client/login"

found_suffix = nil
DEFAULT_FILE_FORMATS.each do |suffix|
next unless File.exist?("#{default_file_name}.#{suffix}")

if !found_suffix
found_suffix = suffix
else
Expand All @@ -118,6 +118,7 @@ def self.find_default_file
def self.logger
JenkinsPipelineBuilder.logger
end
private_class_method :find_default_file, :logger
end
end
end
1 change: 1 addition & 0 deletions lib/jenkins_pipeline_builder/cli/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def job_attributes
entries = JenkinsPipelineBuilder.registry.registry[:job]
entries.each do |name, set|
next unless set.is_a? ExtensionSet

ext = set.extensions.first
display_module(name, ext)
end
Expand Down
15 changes: 14 additions & 1 deletion lib/jenkins_pipeline_builder/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ def get_settings_bag(item_bag, settings_bag = {})
item = item_bag[:value]
bag = {}
return unless item.is_a?(Hash)

item.keys.each do |k|
val = item[k]
next unless val.is_a? String

new_value = resolve_value(val, settings_bag)
return nil if new_value.nil?

bag[k] = new_value
end
my_settings_bag = settings_bag.clone
Expand All @@ -48,7 +51,7 @@ def compile_job(item, settings = {})
new_item = compile(item, settings)
[true, new_item]
rescue StandardError => e
return [false, [e.message]]
[false, [e.message]]
end

def compile(item, settings = {})
Expand All @@ -67,10 +70,12 @@ def compile(item, settings = {})

def handle_enable(item, settings)
return item unless item.is_a? Hash

if enable_block_present? item
enabled_switch = resolve_value(item[:enabled], settings)
return {} if enabled_switch == 'false'
raise "Invalid value for #{item[:enabled]}: #{enabled_switch}" if enabled_switch != 'true'

if item[:parameters].is_a? Hash
item = item.merge item[:parameters]
item.delete :parameters
Expand Down Expand Up @@ -105,17 +110,21 @@ def compile_array(array, settings)

def compile_array_item(item, settings, array)
raise "Found a nil value when processing following array:\n #{array.inspect}" if item.nil?

payload = compile(item, settings)
raise "Failed to resolve:\n===>item #{item}\n\n===>of list: #{array.inspect}" if payload.nil?

payload
end

def compile_item(key, value, settings)
if value.nil?
raise "key: #{key} has a nil value, this is often a yaml syntax error. Skipping children and siblings"
end

payload = compile(value, settings)
raise "Failed to resolve:\n===>key: #{key}\n\n===>value: #{value} payload" if payload.nil?

payload
end

Expand All @@ -135,6 +144,7 @@ def resolve_value(value, settings)
pull_job = value.to_s.match(/{{pull@(.*)}}/)
if pull_job
return pull_job[1] unless settings[:pull_request_number]

value = pull_job[1]
end

Expand All @@ -151,16 +161,19 @@ def resolve_value(value, settings)
vars.select! do |var|
var_val = settings[var]
raise "Could not find defined substitution variable: #{var}" if var_val.nil?

value_s.gsub!("{{#{var}}}", var_val.to_s)
var_val.nil?
end
return nil if vars.count != 0

value_s
end

def correct_job_names!(value)
vars = value.scan(/{{job@(.*)}}/).flatten
return unless vars.count > 0

vars.select! do |var|
var_val = job_collection[var.to_s]
value.gsub!("{{job@#{var}}}", var_val[:value][:name]) unless var_val.nil?
Expand Down
5 changes: 5 additions & 0 deletions lib/jenkins_pipeline_builder/extension_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ExtensionSet
SET_METHODS.each do |method_name|
define_method method_name do |value = nil|
return settings[method_name] if value.nil?

settings[method_name] = value
end
end
Expand Down Expand Up @@ -53,9 +54,11 @@ def add_extension(type, version, settings, path = nil)

def installed_version
return @version if @version

reg = JenkinsPipelineBuilder.registry
version = reg.versions[settings[:plugin_id]]
raise "Plugin #{settings[:name]} is not installed (plugin_id: #{settings[:plugin_id]})" if version.nil?

self.installed_version = version
@version
end
Expand Down Expand Up @@ -103,6 +106,7 @@ def xml(path: false, version: '0', &block)
end
unless block
raise "no block found for version #{version}" unless blocks.key version

return blocks[version][:block]
end
store_xml version, block, path
Expand All @@ -122,6 +126,7 @@ def version(ver, &block)
end

return instance_variable_get(method_name) unless block

blocks[version] = {} unless blocks[version]
blocks[version][method_name] = block
end
Expand Down
8 changes: 6 additions & 2 deletions lib/jenkins_pipeline_builder/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Extension
EXT_METHODS.keys.each do |method_name|
define_method method_name do |value = nil|
return instance_variable_get("@#{method_name}") if value.nil?

instance_variable_set("@#{method_name}", value)
end
end
Expand All @@ -59,6 +60,7 @@ def valid?
def execute(value, n_xml)
errors = check_parameters value
raise ArgumentError, errors.join("\n") if errors.any?

unless path
raise ArgumentError, %(Extension #{name} has no valid path
Check ModuleRegistry#entries and the definition of the extension
Expand All @@ -74,11 +76,13 @@ def execute(value, n_xml)
end

def check_parameters(value)
return [] if parameters && parameters.empty?
return [] if parameters&.empty?
return [] unless value.is_a? Hash

errors = []
value.each_key do |key|
next if parameters && parameters.include?(key)
next if parameters&.include?(key)

errors << "Extension #{name} does not support parameter #{key}"
end
errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class CoberturaReportHelper < ExtensionHelper
def thresholds
@thresholds ||= params[:metric_targets]
return @thresholds if @thresholds

@thresholds = {
failing: [
{ type: 'type', value: 0 },
Expand Down
1 change: 1 addition & 0 deletions lib/jenkins_pipeline_builder/extensions/job_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
if params[:changelog_to_branch]
opts = params[:changelog_to_branch]
raise 'remote and branch are required for changelog_to_branch' unless opts[:remote] && opts[:branch]

send('hudson.plugins.git.extensions.impl.ChangelogToBranch') do
options do
compareRemote opts[:remote]
Expand Down
6 changes: 3 additions & 3 deletions lib/jenkins_pipeline_builder/extensions/publishers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
groovyScript params[:groovy_script]
behavior params[:behavior] || '0'
runFormMatrixParent 'false'
params[:additional_classpaths] && params[:additional_classpaths].each do |path|
params[:additional_classpaths]&.each do |path|
send('org.jvnet.hudson.plugins.groovypostbuild.GroovyScriptPath') do
path path[:path] || '/'
end
Expand Down Expand Up @@ -541,7 +541,7 @@
xml do |params|
send('htmlpublisher.HtmlPublisher', 'plugin' => 'htmlpublisher') do
send('reportTargets') do
params[:report_targets] && params[:report_targets].each do |target|
params[:report_targets]&.each do |target|
send('htmlpublisher.HtmlPublisherTarget') do
reportName target[:report_title] || 'HTML Report'
reportDir target[:report_dir] || ''
Expand Down Expand Up @@ -588,7 +588,7 @@
xml do |params|
send('xunit', 'plugin' => 'xunit') do
send('types') do
params[:types] && params[:types].each do |type|
params[:types]&.each do |type|
send(type[:type]) do
pattern type[:pattern]
skipNoTestFiles type[:skip_no_test_files] || false
Expand Down
1 change: 1 addition & 0 deletions lib/jenkins_pipeline_builder/extensions/wrappers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
passwords = wrapper
end
break unless passwords

passwordEntries do
passwords.each do |password|
EnvInjectPasswordEntry do
Expand Down
4 changes: 4 additions & 0 deletions lib/jenkins_pipeline_builder/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def pull_request(path, project_name, base_branch_only = false)
errors = []
pr_generator.open_prs.each do |pr|
next if base_branch_only && defaults[:git_branch] != pr[:base]

pr_generator.convert! job_collection, pr[:number]
error = publish(project_name)
errors << error unless error.empty?
Expand Down Expand Up @@ -105,6 +106,7 @@ def resolve_project(project)
def resolve_job_by_name(name, settings = {})
job = job_collection.get_item(name)
raise "Failed to locate job by name '#{name}'" if job.nil?

job_value = job[:value]
logger.debug "Compiling job #{name}"
compiler = JenkinsPipelineBuilder::Compiler.new self
Expand Down Expand Up @@ -214,6 +216,7 @@ def publish_promotions(promotions, jobs)
# A hash of promoted_builds names => associated job names
promotion_job_pairs = jobs.each_with_object({}) do |j, acc|
next unless j[:result][:promoted_builds]

j[:result][:promoted_builds].each do |promotion_name|
acc[promotion_name] = j[:result][:name]
end
Expand All @@ -238,6 +241,7 @@ def publish_jobs(jobs, errors = {})
logger.info "Processing #{i}"
job = i[:result]
raise "Result is empty for #{i}" if job.nil?

job = Job.new job
success, payload = job.create_or_update
errors[job.name] = payload unless success
Expand Down
3 changes: 3 additions & 0 deletions lib/jenkins_pipeline_builder/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def logger
def create_or_update
success, payload = to_xml
return success, payload unless success

xml = payload
return local_output(xml) if JenkinsPipelineBuilder.debug || JenkinsPipelineBuilder.file_mode

Expand All @@ -31,6 +32,7 @@ def to_xml
job[:job_type] = 'free_style' unless job[:job_type]
type = job[:job_type]
return false, "Job type: #{type} is not one of #{job_methods.join(', ')}" unless known_type? type

@xml = setup_freestyle_base(job)
payload = send("update_#{type}")

Expand Down Expand Up @@ -109,6 +111,7 @@ def setup_freestyle_base(params)
if params.key?(:template)
template_name = params[:template]
raise "Job template '#{template_name}' can't be resolved." unless @job_templates.key?(template_name)

params.delete(:template)
template = @job_templates[template_name]
params = template.deep_merge(params)
Expand Down
2 changes: 2 additions & 0 deletions lib/jenkins_pipeline_builder/job_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def process_collection!(name, key, value, remote)
# skip if the existing item is local and the new item is remote
return if remote && !existing_remote
raise "Duplicate item with name '#{name}' was detected." unless existing_remote && !remote

# override if the existing item is remote and the new is local
logger.info "Duplicate item with name '#{name}' was detected from the remote folder."
end
Expand All @@ -118,6 +119,7 @@ def load_extensions(path)
path = "#{path}/extensions"
path = File.expand_path(path, Dir.getwd)
return unless File.directory?(path)

logger.info "Loading extensions from folder #{path}"
logger.info Dir.glob("#{path}/*.rb").inspect
Dir.glob("#{path}/**/*.rb").each do |file|
Expand Down
4 changes: 4 additions & 0 deletions lib/jenkins_pipeline_builder/module_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def initialize
def versions
# Return a hash with a default of 1000 so that we'll get the newest in debug
return Hash.new { |_| '1000.0' } if JenkinsPipelineBuilder.debug

@versions ||= JenkinsPipelineBuilder.client.plugin.list_installed
end

Expand Down Expand Up @@ -77,6 +78,7 @@ def get(path)
def get_by_path_collection(path, registry)
item = registry[path.shift.to_sym]
return item if path.count == 0

get_by_path_collection(path, item)
end

Expand All @@ -88,8 +90,10 @@ def traverse_registry_path(path, params, n_xml)
def traverse_registry(registry, params, n_xml, strict = false)
params.each do |key, value|
next unless registry.is_a? Hash

unless registry.key? key
raise TypeError, "!!!! could not find key #{key} !!!!" if strict

next
end
reg_value = registry[key]
Expand Down
1 change: 1 addition & 0 deletions lib/jenkins_pipeline_builder/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def initialize(generator)
def create(params, job_name)
success, payload = prom_to_xml(params)
return success, payload unless success

xml = payload
return local_output(xml) if JenkinsPipelineBuilder.debug || JenkinsPipelineBuilder.file_mode

Expand Down
1 change: 1 addition & 0 deletions lib/jenkins_pipeline_builder/pull_request_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def convert!(job_collection, pr_number)

def delete_closed_prs
return if JenkinsPipelineBuilder.debug

jobs_to_delete = JenkinsPipelineBuilder.client.job.list "^#{application_name}-PR(\\d+)-(.*)$"
open_prs.each do |pr|
jobs_to_delete.reject! { |j| j.start_with? "#{application_name}-PR#{pr[:number]}" }
Expand Down
Loading

0 comments on commit ebf407f

Please sign in to comment.