Skip to content
Browse files

Make skipping of plugins in publishers possible

  • Loading branch information...
1 parent f10995a commit e30a6be211363787bdd60c7637b9516e0bf39a50 @abangratz abangratz committed
Showing with 53 additions and 20 deletions.
  1. +16 −1 lib/kraken-build/jenkins-api.rb
  2. +37 −19 spec/lib/jenkins_api_spec.rb
View
17 lib/kraken-build/jenkins-api.rb
@@ -1,6 +1,8 @@
class JenkinsApi
include HTTParty
+ attr_accessor :skip_plugins
+
def initialize(options = {})
if options[:port]
self.class.base_uri "#{options[:host]}:#{options[:port]}"
@@ -8,9 +10,14 @@ def initialize(options = {})
self.class.base_uri options[:host]
end
- if(options[:username] && options[:password])
+ 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
def get_jobs(options = {})
@@ -35,6 +42,14 @@ def create_job_configuration(repo, branch)
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
View
56 spec/lib/jenkins_api_spec.rb
@@ -27,13 +27,17 @@
api.class.default_options[:base_uri].should == 'http://host:1337'
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
context "when interacting with Jenkins" do
- before(:each) do
- @api = JenkinsApi.new
- end
+ let(:api) { JenkinsApi.new }
it "#get_jobs returns an array of jobs" do
j = []
@@ -42,9 +46,9 @@
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 "Bar"
@@ -53,15 +57,15 @@
it "#remove_job returns true if a job was deleted successfully" do
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
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
it "#get_job_configuration returns the xml configuration for a job" do
@@ -71,19 +75,33 @@
xml = double()
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
-
- it "#create_job_configuration returns a valid job configuration" do
- xml = <<-XML
+ context "#create_job_configuration" do
+ it "returns a valid job configuration" do
+ xml = <<-XML
<xml><branches><hudson.plugins.git.BranchSpec><name>master</name></hudson.plugins.git.BranchSpec></branches></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 =~ /feature/
- end
+ 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 =~ /feature/
+ 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

0 comments on commit e30a6be

Please sign in to comment.
Something went wrong with that request. Please try again.