Skip to content

Commit

Permalink
Merge pull request #4 from abangratz/master
Browse files Browse the repository at this point in the history
Create options for skipping plugins in <publishers> tag
  • Loading branch information
pxlpnk committed Dec 17, 2012
2 parents aaf6bef + e30a6be commit 18935bb
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 20 deletions.
17 changes: 16 additions & 1 deletion lib/kraken-build/jenkins-api.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,16 +1,23 @@
class JenkinsApi class JenkinsApi
include HTTParty include HTTParty


attr_accessor :skip_plugins

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


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


def get_jobs(options = {}) def get_jobs(options = {})
Expand All @@ -35,6 +42,14 @@ def create_job_configuration(repo, branch)
doc = REXML::Document.new(draft) doc = REXML::Document.new(draft)
REXML::XPath.first(doc, '//branches//hudson.plugins.git.BranchSpec//name').text = branch 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 doc.to_s
end end


Expand Down
56 changes: 37 additions & 19 deletions spec/lib/jenkins_api_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
api.class.default_options[:base_uri].should == 'http://host:1337' api.class.default_options[:base_uri].should == 'http://host:1337'
end end


it "sets the plugins to skip at initialization" do
options = {:skip_plugins => ['test'] }
jenkins_api = JenkinsApi.new(options)
jenkins_api.skip_plugins.should eq(['test'])
end

end end




context "when interacting with Jenkins" do context "when interacting with Jenkins" do
before(:each) do let(:api) { JenkinsApi.new }
@api = JenkinsApi.new
end


it "#get_jobs returns an array of jobs" do it "#get_jobs returns an array of jobs" do
j = [] j = []
Expand All @@ -42,9 +46,9 @@


results = {"jobs" => j} results = {"jobs" => j}


@api.class.should_receive(:get).and_return(results) api.class.should_receive(:get).and_return(results)


jobs = @api.get_jobs jobs = api.get_jobs


jobs.should include "Foo" jobs.should include "Foo"
jobs.should include "Bar" jobs.should include "Bar"
Expand All @@ -53,15 +57,15 @@
it "#remove_job returns true if a job was deleted successfully" do it "#remove_job returns true if a job was deleted successfully" do
job_name = "foo.test_branch" job_name = "foo.test_branch"


@api.class.should_receive(:post).with("/job/#{CGI.escape(job_name)}/doDelete").and_return(true) api.class.should_receive(:post).with("/job/#{CGI.escape(job_name)}/doDelete").and_return(true)


@api.remove_job(job_name) api.remove_job(job_name)
end end


it "#build_job returns true and triggers a build for a job" do it "#build_job returns true and triggers a build for a job" do
@api.class.should_receive(:get).with("/job/FooJob/build").and_return(true) api.class.should_receive(:get).with("/job/FooJob/build").and_return(true)


@api.build_job("FooJob") api.build_job("FooJob")
end end


it "#get_job_configuration returns the xml configuration for a job" do it "#get_job_configuration returns the xml configuration for a job" do
Expand All @@ -71,19 +75,33 @@
xml = double() xml = double()
xml.should_receive(:body).and_return(a) xml.should_receive(:body).and_return(a)


@api.class.should_receive(:get).with("/job/FooJob/config.xml", {}).and_return(xml) api.class.should_receive(:get).with("/job/FooJob/config.xml", {}).and_return(xml)


@api.get_job_configuration("FooJob").should be(a) api.get_job_configuration("FooJob").should be(a)
end end

context "#create_job_configuration" do
it "#create_job_configuration returns a valid job configuration" do it "returns a valid job configuration" do
xml = <<-XML xml = <<-XML
<xml><branches><hudson.plugins.git.BranchSpec><name>master</name></hudson.plugins.git.BranchSpec></branches></xml> <xml><branches><hudson.plugins.git.BranchSpec><name>master</name></hudson.plugins.git.BranchSpec></branches></xml>
XML XML
xml.should_receive(:body).and_return(xml) xml.should_receive(:body).and_return(xml)
@api.class.should_receive(:get).with('/job/foobar.master/config.xml', {}).and_return(xml) api.class.should_receive(:get).with('/job/foobar.master/config.xml', {}).and_return(xml)
@api.create_job_configuration('foobar', 'feature').should =~ /feature/ api.create_job_configuration('foobar', 'feature').should =~ /feature/
end end
it "removes all plugins that have been listed in 'skip_plugins'" do
api.skip_plugins = ['test']
xml_stripped = <<-XML_S
<xml><project><branches><hudson.plugins.git.BranchSpec><name>master</name></hudson.plugins.git.BranchSpec></branches><publishers></publishers></project></xml>
XML_S
xml = <<-XML
<xml><project><branches><hudson.plugins.git.BranchSpec><name>master</name></hudson.plugins.git.BranchSpec></branches><publishers><hudson.plugins.test>test</hudson.plugins.test></publishers></project></xml>
XML
xml.should_receive(:body).and_return(xml)
api.class.should_receive(:get).with('/job/foobar.master/config.xml', {}).and_return(xml)
api.create_job_configuration('foobar', 'feature').should_not =~ /hudson\.plugins\.test/
end
end



end end


Expand Down
38 changes: 38 additions & 0 deletions spec/lib/kraken-build_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'spec_helper'

describe KrakenBuild do
context "Setting config" do
let(:options) { mock(Hash).as_null_object }

it "sets the config options at @config" do
kraken = KrakenBuild.set_config(options)
KrakenBuild.instance_variable_get(:@config).should eq(options)
end

it "sets the repository option as @repository" do
repository = mock(String)
options.should_receive(:[]).with(:repository).and_return(repository)
kraken = KrakenBuild.set_config(options)
KrakenBuild.instance_variable_get(:@repository).should eq(repository)
end

it "sets the github api from instance call" do
github_api = mock(GithubApi).as_null_object
GithubApi.should_receive(:new).and_return(github_api)
kraken = KrakenBuild.set_config(options)
KrakenBuild.instance_variable_get(:@github).should eq(github_api)
end
it "sets the jenkins api from instance call" do
jenkins_api = mock(GithubApi).as_null_object
JenkinsApi.should_receive(:new).and_return(jenkins_api)
kraken = KrakenBuild.set_config(options)
KrakenBuild.instance_variable_get(:@jenkins).should eq(jenkins_api)
end
it "returns the config" do
kraken = KrakenBuild.set_config(options)
KrakenBuild.instance_variable_get(:@config).should eq(options)
kraken.should eq(options)
end

end
end

0 comments on commit 18935bb

Please sign in to comment.