Permalink
Browse files

Refactored models to avoid job or client assignment.

  • Loading branch information...
1 parent b1f9c16 commit 1e57cbea223e84ac64e7827d7bdd1e5047dd0143 @dblock committed Apr 30, 2012
View
@@ -1,2 +1,2 @@
---color
---format progress
+--colour
+
View
@@ -31,18 +31,22 @@ Then you can issue the following commands in your app.
### Jobs
-`client.jobs` returns a hash of jobs with the job name as key.
+Retrieve a hash of jobs with the job name as key.
+
+``` ruby
+client.jobs
+```
#### Create
Create a new Jenkins job on the server with a given configuration.
``` ruby
-job = Jenkins::Client::Job.new({name: "job_name"})
+job = Jenkins::Client::Job.new({ :name => "job_name" })
job.create!(config)
```
-Jenkins uses XML config files on the server and this is what you should send as the config. Example:
+Jenkins uses XML config files on the server and this is what you should send as the config.
``` xml
<?xml version='1.0' encoding='UTF-8'?>
@@ -70,27 +74,37 @@ To export an existing config simply look in the jobs path inside your Jenkins se
Starts a job.
-job = Jenkins::Client::Job.new({name: "job_name"})
+``` ruby
+job = Jenkins::Client::Job.new({ :name => "job_name" })
job.start!
+```
#### Delete
Delete a Jenkins job on the server.
``` ruby
-job = Jenkins::Client::Job.new({name: "job_name"})
+job = Jenkins::Client::Job.new({ :name => "job_name" })
job.delete!
```
#### Last Build
-`job.last_build` will return the last build.
-`job.last_successful_build` will return the last successful build.
-`job.last_failed_build` will return the last failed build.
+Retrieve the last build.
+
+``` ruby
+job.last_build # last build
+job.last_successful_build # last successful build
+job.last_failed_build # last failed build
+```
### Build
-`build.console_text` will return the build's console text.
+Retrieve the console text of a build.
+
+``` ruby
+job.last_failed_build.console_text
+```
## Changelog/History
@@ -1,10 +1,8 @@
module Jenkins
class Client
class Build < Hashie::Rash
- attr_accessor :job
-
def console_text
- job.client.get("#{url}consoleText", job.client.raw_connection).body
+ job.client.get("#{url}consoleText", { :json => false }).body
end
end
end
@@ -5,57 +5,50 @@ class Client
def configure
yield self
end
-
- def connection
- @connection ||= begin
- c = Faraday.new(:url => url) do |builder|
- builder.use FaradayMiddleware::Rashify
- builder.use FaradayMiddleware::ParseJson
- builder.adapter :net_http
- end
- c.basic_auth username, password
- c
- end
- end
- def raw_connection
- @raw_connection ||= begin
- c = Faraday.new(:url => url) do |builder|
- builder.adapter :net_http
- end
- c.basic_auth username, password
- c
- end
- end
-
- def get(path, use_connection = connection)
- normalized_path = normalize_path path
- use_connection.get(normalized_path)
+ def get(path, options = default_connection_options)
+ connection(options).get(normalize_path(path, options))
end
- def post(path, body, use_connection = connection)
- normalized_path = normalize_path path
- resp = use_connection.post do |req|
+ def post(path, body, options = default_connection_options)
+ resp = connection(options).post do |req|
req.headers['Content-Type'] = 'application/xml'
- req.url normalized_path
+ req.url normalize_path(path, options)
req.body = body
end
resp.status == 200
end
def jobs
Hash[get("/api/json").body.jobs.map do |data|
- job = Jenkins::Client::Job.new(data)
- job.client = self
+ job = Jenkins::Client::Job.new(data.merge({ :client => self }))
[ job.name, job ]
end]
end
private
- def normalize_path path
- return @connection.path_prefix+path unless @connection.path_prefix == '/'
- path
+ def default_connection_options
+ { :json => true }
+ end
+
+ def connection(options = default_connection_options)
+ @connections ||= {}
+ @connections[options.map { |k, v| "#{k}=v" }.join("|")] ||= begin
+ c = Faraday.new(:url => url) do |builder|
+ if options[:json]
+ builder.use FaradayMiddleware::Rashify
+ builder.use FaradayMiddleware::ParseJson
+ end
+ builder.adapter :net_http
+ end
+ c.basic_auth username, password
+ c
+ end
+ end
+
+ def normalize_path(path, options = default_connection_options)
+ connection(options).path_prefix == '/' ? path : connection.path_prefix + path
end
end
@@ -1,7 +1,6 @@
module Jenkins
class Client
class Job < Hashie::Rash
- attr_accessor :client
def create!(config)
client.post("/createItem/api/xml?name=#{CGI.escape(name)}", config)
@@ -16,9 +15,8 @@ def start!
end
def last_build(status = "")
- build = Jenkins::Client::Build.new(client.get("/job/#{name}/last#{status.capitalize}Build/api/json").body)
- build.job = self
- build
+ resp = client.get("/job/#{name}/last#{status.capitalize}Build/api/json").body
+ Jenkins::Client::Build.new(resp.merge({ :job => self }))
end
def last_failed_build
@@ -0,0 +1,9 @@
+{
+ "assignedLabels" : [{}],
+ "mode" : "NORMAL",
+ "nodeDescription" : "the master Jenkins node",
+ "nodeName" : "",
+ "numExecutors" : 2,
+ "description" : null,
+ "jobs" : []
+}
@@ -0,0 +1,17 @@
+{
+ "assignedLabels" : [{}],
+ "mode" : "NORMAL",
+ "nodeDescription" : "the master Jenkins node",
+ "nodeName" : "",
+ "numExecutors" : 2,
+ "description" : null,
+ "jobs":
+ [
+ { "name" : "foo-bar", "url" : "https://testjenkins.com/job/foo-bar/", "color" : "blue"},
+ { "name" : "woohoo", "url" : "https://jenkinstest.com/job/woohoo/", "color" : "red"},
+ { "name" : "wat", "url" : "https://jenkinstest.com/job/wat/", "color" : "red"},
+ { "name" : "fudge", "url" : "https://jenkinstest.com/job/fudge/", "color" : "blue"},
+ { "name" : "amaze", "url" : "https://jenkinstest.com/job/amaze/", "color" : "blue"}
+ ]
+}
+
@@ -0,0 +1,26 @@
+require "spec_helper"
+
+describe Jenkins::Client::Build do
+
+ before :each do
+ jenkins_config "https://jenkinstest.com"
+ end
+
+ let(:client) { @client }
+ let(:job) { Jenkins::Client::Job.new({ :name => "test_job", :client => @client }) }
+
+ describe ".console_text" do
+ before :each do
+ stub_request(:get, "https://testuser:testpass@jenkinstest.com/job/test_job/lastBuild/consoleText").
+ 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 => "console\ntext", :headers => {})
+ end
+ let(:build) { Jenkins::Client::Build.new({ :url => "#{client.url}/job/#{job.name}/lastBuild/", :job => job }) }
+
+ it "returns console text" do
+ build.console_text.should == "console\ntext"
+ end
+ end
+
+
+end
@@ -2,54 +2,50 @@
describe Jenkins::Client do
- before(:each) do
- jenkins_config("https://jenkinstest.com")
+ before :each do
+ jenkins_config "https://jenkinstest.com"
end
+
+ let(:client) { @client }
- before(:each) do
- body = '{"assignedLabels":[{}],"mode":"NORMAL","nodeDescription":"the master Jenkins node","nodeName":"","numExecutors":2,"description":null,"jobs":[
- {"name":"foo-bar","url":"https://testjenkins.com/job/foo-bar/","color":"blue"},
- {"name":"woohoo","url":"https://jenkinstest.com/job/woohoo/","color":"red"},
- {"name":"wat","url":"https://jenkinstest.com/job/wat/","color":"red"},
- {"name":"fudge","url":"https://jenkinstest.com/job/fudge/","color":"blue"},
- {"name":"amaze","url":"https://jenkinstest.com/job/amaze/","color":"blue"}]}'
+ before :each do
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 => {})
+ to_return(:status => 200, :body => resp_json(:jenkinstest), :headers => {})
end
- before(:each) do
- body = '{"assignedLabels":[{}],"mode":"NORMAL","nodeDescription":"the master Jenkins node","nodeName":"","numExecutors":2,"description":null,"jobs":[]}'
+ before :each do
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 => {})
+ to_return(:status => 200, :body => resp_json(:emptyjenkinstest), :headers => {})
end
describe ".jobs" do
context "given some jobs are available" do
- let(:jobs) { @client.jobs }
+ let(:jobs) { client.jobs }
it "will return a list of jobs" do
jobs.should have(5).items
end
it "will contain an expected job" do
- jobs.should include(
- "foo-bar" => {
- "name"=>"foo-bar",
- "url"=>"https://testjenkins.com/job/foo-bar/",
- "color"=>"blue"
- })
+ jobs.should include \
+ "foo-bar" => {
+ "client" => client,
+ "name"=>"foo-bar",
+ "url"=>"https://testjenkins.com/job/foo-bar/",
+ "color"=>"blue"
+ }
end
end
context "given no jobs" do
- before(:each) do
+ before :each do
empty_jenkins_config
end
it "will have no jobs" do
- @client.jobs.should be_empty
+ client.jobs.should be_empty
end
end
end
Oops, something went wrong.

0 comments on commit 1e57cbe

Please sign in to comment.