class JenkinsPipelineBuilder::JobCollection

Attributes

collection[RW]
loaded[R]
loaded?[R]
remote_dependencies[RW]

Public Class Methods

new() click to toggle source
# File lib/jenkins_pipeline_builder/job_collection.rb, line 7
def initialize
  @collection = {}
  @remote_dependencies = RemoteDependencies.new self
  @loaded = false
end

Public Instance Methods

clear_remote_dependencies() click to toggle source
# File lib/jenkins_pipeline_builder/job_collection.rb, line 13
def clear_remote_dependencies
  @remote_dependencies = RemoteDependencies.new self
end
defaults() click to toggle source
# File lib/jenkins_pipeline_builder/job_collection.rb, line 33
def defaults
  collection.each_value do |item|
    return item if item[:type] == 'defaults' || item[:type] == :defaults
  end
  # This is here for historical purposes
  get_item('global')
end
get_item(name) click to toggle source
# File lib/jenkins_pipeline_builder/job_collection.rb, line 41
def get_item(name)
  collection[name.to_s]
end
jobs() click to toggle source
# File lib/jenkins_pipeline_builder/job_collection.rb, line 29
def jobs
  collect_type :job
end
load_from_path(path, remote = false) click to toggle source
# File lib/jenkins_pipeline_builder/job_collection.rb, line 45
def load_from_path(path, remote = false)
  load_extensions(path)
  path = File.expand_path(path, Dir.getwd)
  if File.directory?(path)
    logger.info "Generating from folder #{path}"
    Dir[File.join(path, '/*.{yaml,yml}')].each do |file|
      load_file(file, remote)
    end
    Dir[File.join(path, '/*.json')].each do |file|
      load_file(file, remote)
    end
  else
    load_file(path, remote)
  end
  @loaded = true
end
logger() click to toggle source
# File lib/jenkins_pipeline_builder/job_collection.rb, line 17
def logger
  JenkinsPipelineBuilder.logger
end
projects() click to toggle source
# File lib/jenkins_pipeline_builder/job_collection.rb, line 25
def projects
  collect_type :project
end
standalone_jobs() click to toggle source
# File lib/jenkins_pipeline_builder/job_collection.rb, line 21
def standalone_jobs
  jobs.map { |job| { result: job } }
end

Private Instance Methods

collect_type(type_name) click to toggle source
# File lib/jenkins_pipeline_builder/job_collection.rb, line 64
def collect_type(type_name)
  collection.values.select { |item| item if item[:type] == type_name }
end
load_extensions(path) click to toggle source
# File lib/jenkins_pipeline_builder/job_collection.rb, line 118
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").sort.each do |file|
    logger.info "Loaded #{file}"
    require file
  end
end
load_file(path, remote = false) click to toggle source
# File lib/jenkins_pipeline_builder/job_collection.rb, line 68
def load_file(path, remote = false)
  hash = if path.end_with? 'json'
           JSON.parse(IO.read(path))
         else # elsif path.end_with?("yml") || path.end_with?("yaml")
           YAML.load_file(path)
         end
  logger.info "Loading file #{path}"
  hash.each do |section|
    load_section section, remote
  end
rescue StandardError => e
  raise CustomErrors::ParseError.new e.message, path
end
load_section(section, remote) click to toggle source
# File lib/jenkins_pipeline_builder/job_collection.rb, line 82
def load_section(section, remote)
  Utils.symbolize_keys_deep!(section)
  key = section.keys.first
  value = section[key]
  if key == :dependencies
    logger.info 'Resolving Dependencies for remote project'
    remote_dependencies.load value
    return
  end

  unless value.is_a? Hash
    raise TypeError, %(Expected Hash received #{value.class}.
      Verify that the pipeline section is made up of a single {key: Hash/Object} pair
      See the definition for:
      \t#{section}).squeeze(' ')
  end

  name = value[:name]
  process_collection! name, key, value, remote
end
process_collection!(name, key, value, remote) click to toggle source

TODO: This should be cleaned up a bit. I'm sure we can get rid of the elsif and we should be more clear on the order of loading of things

# File lib/jenkins_pipeline_builder/job_collection.rb, line 105
def process_collection!(name, key, value, remote)
  if collection.key?(name)
    existing_remote = collection[name.to_s][: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
  collection[name.to_s] = { name: name.to_s, type: key, value: value, remote: remote }
end