class LeeroyJenkins::JobUpdater

Attributes

at_xpath[R]
jenkins_client[R]
job_names_to_update[R]
new_xml[R]
threads[R]
xpath[R]

Public Class Methods

new(job_names_to_update, new_xml, jenkins_client, xpath, at_xpath, threads) click to toggle source
# File lib/leeroy_jenkins/job_updater.rb, line 10
def initialize(job_names_to_update, new_xml, jenkins_client, xpath, at_xpath, threads)
  @job_names_to_update = job_names_to_update
  @new_xml = new_xml
  @jenkins_client = jenkins_client
  @xpath = xpath
  @at_xpath = at_xpath
  @threads = threads
end

Public Instance Methods

update_jobs(dry = true) click to toggle source
# File lib/leeroy_jenkins/job_updater.rb, line 19
def update_jobs(dry = true)
  Result.new(dry ? dry_run : update_jobs!)
end

Private Instance Methods

build_xml(job_name) click to toggle source
# File lib/leeroy_jenkins/job_updater.rb, line 42
def build_xml(job_name)
  element_to_insert = Nokogiri.XML(new_xml).root
  document_to_modify = Nokogiri.XML(current_xml(job_name), &:noblanks)
  elements_to_modify = document_to_modify.xpath(xpath).map do |node|
    node.document? ? node.root : node
  end

  elements_to_modify.each do |node|
    case at_xpath
    when :replace
      node.replace element_to_insert
    when :append
      node << element_to_insert
    when :delete
      node.remove
    end
  end

  document_to_modify.canonicalize
end
current_xml(job_name) click to toggle source
# File lib/leeroy_jenkins/job_updater.rb, line 63
def current_xml(job_name)
  jenkins_client.job.get_config(job_name)
end
dry_run() click to toggle source
# File lib/leeroy_jenkins/job_updater.rb, line 34
def dry_run
  pairs = Parallel.map(job_names_to_update, in_threads: threads) do |job_name|
    [job_name, build_xml(job_name)]
  end

  Hash[pairs]
end
update_jobs!() click to toggle source
# File lib/leeroy_jenkins/job_updater.rb, line 25
def update_jobs!
  pairs = Parallel.map(job_names_to_update, in_threads: threads) do |job_name|
    http_status_code = jenkins_client.job.post_config job_name, build_xml(job_name)
    [job_name, http_status_code]
  end

  Hash[pairs]
end