Permalink
Browse files

Merge pull request #1 from aimak/master

Bug when jenkins is installed elsewhere than '/'
  • Loading branch information...
2 parents 18f76e9 + 6096fa6 commit 8c090c7d85aefd98dbe4087860ee0e37ba3efd6b @john-griffin john-griffin committed Apr 27, 2012
Showing with 159 additions and 12 deletions.
  1. +8 −0 README.md
  2. +12 −2 lib/jenkins-client/client.rb
  3. +12 −0 lib/jenkins-client/job.rb
  4. +81 −10 spec/jenkins-client/job_spec.rb
  5. +46 −0 spec/support/resp_last_build.json
View
@@ -64,6 +64,14 @@ Then you can issue the following commands in your app.
To export an existing config simply look in the jobs path inside your Jenkins server and pull back a job's `config.xml` file.
+### Start
+
+`Jenkins::Client::Job.start("job_name")` will start a build for the job whose name is passed by parameter
+
+### LastBuild
+
+`Jenkins::Client::Job.lastBuild("job_name")` will return information about the last build of the job whose name is passed by parameter
+
## Contributing
1. Fork it
@@ -12,6 +12,7 @@ def configure
setup_connection
end
+ @connection
def setup_connection
@connection = Faraday.new(:url => url) do |builder|
builder.use FaradayMiddleware::Rashify
@@ -22,17 +23,26 @@ def setup_connection
end
def get(path)
- connection.get(path)
+ normalized_path = normalize_path path
+ connection.get(normalized_path)
end
def post(path, body)
+ normalized_path = normalize_path path
resp = @connection.post do |req|
req.headers['Content-Type'] = 'application/xml'
- req.url path
+ req.url normalized_path
req.body = body
end
resp.status == 200
end
+
+ private
+ def normalize_path path
+ return @connection.path_prefix+path unless @connection.path_prefix == '/'
+ path
+ end
end
+
end
end
View
@@ -15,6 +15,18 @@ def self.find(name)
def self.create(name, config)
Jenkins::Client.post("/createItem/api/xml?name=#{CGI.escape(name)}", config)
end
+
+ def self.delete(name)
+ Jenkins::Client.post("/job/#{name}/doDelete", "")
+ end
+
+ def self.start(name)
+ Jenkins::Client.post("/job/#{name}/build", "")
+ end
+
+ def self.lastBuild(name)
+ Jenkins::Client::get("/job/#{name}/lastBuild/api/json")
+ end
end
end
end
@@ -27,15 +27,15 @@ def empty_jenkins_config
{"name":"fudge","url":"https://jenkinstest.com/job/fudge/","color":"blue"},
{"name":"amaze","url":"https://jenkinstest.com/job/amaze/","color":"blue"}]}'
stub_request(:get, "https://testuser:testpass@jenkinstest.com/api/json").
- with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
- to_return(:status => 200, :body => body, :headers => {})
+ with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
+ to_return(:status => 200, :body => body, :headers => {})
end
before(:each) do
body = '{"assignedLabels":[{}],"mode":"NORMAL","nodeDescription":"the master Jenkins node","nodeName":"","numExecutors":2,"description":null,"jobs":[]}'
stub_request(:get, "https://testuser:testpass@emptyjenkinstest.com/api/json").
- with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
- to_return(:status => 200, :body => body, :headers => {})
+ with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
+ to_return(:status => 200, :body => body, :headers => {})
end
describe ".all" do
@@ -48,10 +48,10 @@ def empty_jenkins_config
it "will contain an expected job" do
jobs.should include({
- "name"=>"foo-bar",
- "url"=>"https://testjenkins.com/job/foo-bar/",
- "color"=>"blue"
- })
+ "name"=>"foo-bar",
+ "url"=>"https://testjenkins.com/job/foo-bar/",
+ "color"=>"blue"
+ })
end
end
@@ -84,7 +84,7 @@ def empty_jenkins_config
before(:each) do
empty_jenkins_config
end
-
+
it "will be nil" do
Jenkins::Client::Job.find("woohoo").should be_nil
end
@@ -94,7 +94,7 @@ def empty_jenkins_config
describe ".create" do
before(:each) do
stub_request(:post, "https://testuser:testpass@jenkinstest.com/createItem/api/xml?name=excellent").
- to_return(:status => 200, :body => "", :headers => {})
+ to_return(:status => 200, :body => "", :headers => {})
end
context "given an xml config file" do
@@ -103,5 +103,76 @@ def empty_jenkins_config
Jenkins::Client::Job.create("excellent", config).should be_true
end
end
+
+ context "jenkins is not root app" do
+ before(:each) do
+ jenkins_config("https://jenkinstest.com/jenkins")
+ stub_request(:post, "https://testuser:testpass@jenkinstest.com/jenkins/createItem/api/xml?name=excellent").
+ to_return(:status => 200, :body => "", :headers => {})
+ end
+
+ it "should be able to create a new job" do
+ config = File.read("spec/fixtures/jenkins_config.xml")
+ Jenkins::Client::Job.create("excellent", config).should be_true
+ end
+ end
+ end
+
+ describe ".delete" do
+ context "there is a job to delete" do
+ before(:each) do
+ stub_request(:post, "https://testuser:testpass@jenkinstest.com/createItem/api/xml?name=excellent").
+ to_return(:status => 200, :body => "", :headers => {})
+
+ stub_request(:post, "https://testuser:testpass@jenkinstest.com/job/excellent/doDelete").
+ with(:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/xml', 'User-Agent'=>'Ruby'}).
+ to_return(:status => 200, :body => "", :headers => {})
+ end
+
+ it "should delete the job" do
+ Jenkins::Client::Job.delete("excellent").should be_true
+ end
+ end
+ end
+
+ describe ".start" do
+ context "there is a job to start" do
+ before(:each) do
+ stub_request(:post, "https://testuser.testpass@jenkinstest.com/createItem/api/xml?name=excellent").
+ to_return(:status => 200, :body => "", :headers => {})
+ stub_request(:post, "https://testuser:testpass@jenkinstest.com/job/excellent/build").
+ with(:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/xml', 'User-Agent'=>'Ruby'}).
+ to_return(:status => 200, :body => "", :headers => {})
+ end
+
+ it "should start the build for this job" do
+ Jenkins::Client::Job.start("excellent").should be_true
+ end
+
+ end
+ end
+
+ describe ".lastBuild" do
+ context "a build has been started" do
+ before(:each) do
+ stub_request(:post, "https://testuser.testpass@jenkinstest.com/createItem/api/xml?name=excellent").
+ to_return(:status => 200, :body => "", :headers => {})
+ stub_request(:post, "https://testuser:testpass@jenkinstest.com/job/excellent/build").
+ to_return(:status => 200, :body => "", :headers => {})
+ stub_request(:get, "https://testuser:testpass@jenkinstest.com/job/excellent/lastBuild/api/json").
+ with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
+ to_return(:status => 200, :body => resp_lastbuild, :headers => {})
+ end
+
+ it "should return 1 as buildNumber" do
+ resp = Jenkins::Client::Job.lastBuild("excellent")
+ resp.body.number.should == 1
+ end
+ end
+ end
+
+ def resp_lastbuild
+ current_dir = File.expand_path(File.dirname((__FILE__)))
+ IO.read(File.join(current_dir, "..", "support", "resp_last_build.json"))
end
end
@@ -0,0 +1,46 @@
+{
+ "actions": [{
+ "causes": [{
+ "shortDescription": "Started by an SCM change"
+ }]
+ }, {}, {
+ "buildsByBranchName": {
+ "origin/master": {
+ "buildNumber": 1,
+ "buildResult": null,
+ "revision": {
+ "SHA1": "c0262eb9f342eec9bfbc533968f2fcc66d7e80db",
+ "branch": [{
+ "SHA1": "c0262eb9f342eec9bfbc533968f2fcc66d7e80db",
+ "name": "origin/master"
+ }]
+ }
+ }
+ },
+ "lastBuiltRevision": {
+ "SHA1": "c0262eb9f342eec9bfbc533968f2fcc66d7e80db",
+ "branch": [{
+ "SHA1": "c0262eb9f342eec9bfbc533968f2fcc66d7e80db",
+ "name": "origin/master"
+ }]
+ },
+ "scmName": ""
+ }, {}, {}],
+ "artifacts": [],
+ "building": false,
+ "description": null,
+ "duration": 495,
+ "fullDisplayName": "excellent #1",
+ "id": "2012-03-19_09-01-38",
+ "keepLog": false,
+ "number": 1,
+ "result": "FAILURE",
+ "timestamp": 1332144098399,
+ "url": "https://testuser:testpass@jenkinstest.com/job/excellent/1/",
+ "builtOn": "",
+ "changeSet": {
+ "items": [],
+ "kind": null
+ },
+ "culprits": []
+}

0 comments on commit 8c090c7

Please sign in to comment.