Class: JenkinsApi

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/kraken-build/jenkins-api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ JenkinsApi

Returns a new instance of JenkinsApi.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kraken-build/jenkins-api.rb', line 6

def initialize(options = {})
  if options[:port]
    self.class.base_uri "#{options[:host]}:#{options[:port]}"
  else
    self.class.base_uri  options[:host]
  end

  if (options[:username] && options[:password])
    self.class.basic_auth options[:username] , options[:password]
  end
  if (options[:skip_plugins])
    self.skip_plugins = options[:skip_plugins]
  else
    self.skip_plugins = []
  end
end

Instance Attribute Details

#skip_pluginsObject

Returns the value of attribute skip_plugins.



4
5
6
# File 'lib/kraken-build/jenkins-api.rb', line 4

def skip_plugins
  @skip_plugins
end

Instance Method Details

#build_job(job_name, options = {}) ⇒ Object



66
67
68
# File 'lib/kraken-build/jenkins-api.rb', line 66

def build_job(job_name, options={})
  self.class.get("/job/#{CGI.escape(job_name)}/build")
end

#create_job(job, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/kraken-build/jenkins-api.rb', line 30

def create_job(job, options = {})
  repo, branch_name = job.split('.', 2)
  job_config = create_job_configuration(repo, branch_name)
  options.merge!(
    :body => job_config,
    :format => :xml, :headers => { 'content-type' => 'application/xml' })

  self.class.post("/createItem/api/xml?name=#{CGI.escape(job)}", options)
end

#create_job_configuration(repo, branch) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kraken-build/jenkins-api.rb', line 40

def create_job_configuration(repo, branch)
  draft = get_job_configuration("#{repo}.master")
  doc = REXML::Document.new(draft)
  REXML::XPath.first(doc, '//branches//hudson.plugins.git.BranchSpec//name').text = branch

  plugin_names = self.skip_plugins.map { |n| "hudson.plugins.#{n}" }
  publishers = REXML::XPath.first(doc, '//project/publishers')
  if publishers && publishers.has_elements? && self.skip_plugins && !(self.skip_plugins.empty?)
    publishers.children.select { |child| child.xpath.match %r[/hudson\.plugins] }.each do |plugin|
      doc.delete_element(plugin.xpath) if plugin_names.any? { |name| plugin.xpath[name]}
    end
  end

  doc.to_s
end

#get_job_configuration(job, options = {}) ⇒ Object



56
57
58
59
60
# File 'lib/kraken-build/jenkins-api.rb', line 56

def get_job_configuration(job, options = {})
  response = self.class.get("/job/#{job}/config.xml", options)

  response.body
end

#get_jobs(options = {}) ⇒ Object



23
24
25
26
27
28
# File 'lib/kraken-build/jenkins-api.rb', line 23

def get_jobs(options = {})
  response = self.class.get("/api/json/", options)
  jobs = response["jobs"]

  jobs.map { |job| job["name"] }
end

#remove_job(job_name, options = {}) ⇒ Object



62
63
64
# File 'lib/kraken-build/jenkins-api.rb', line 62

def remove_job(job_name, options={})
  self.class.post("/job/#{CGI.escape(job_name)}/doDelete")
end