jenkins-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+
6
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jenkins-client.gemspec
4
+ gemspec
@@ -0,0 +1,19 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+ # Capybara request specs
17
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
18
+ end
19
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 John Griffin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,73 @@
1
+ # Jenkins::Client
2
+
3
+ This is a small gem used for listing, finding and creating Jenkins jobs on a Jenkins CI server.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'jenkins-client'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install jenkins-client
18
+
19
+ ## Usage
20
+
21
+ In rails add an initialiser like this
22
+
23
+ ``` ruby
24
+ Jenkins::Client.configure do |c|
25
+ c.username = "user"
26
+ c.password = "pass"
27
+ c.url = "http://jenkinsurl.com"
28
+ end
29
+ ```
30
+
31
+ Then you can issue the following commands in your app.
32
+
33
+ ### All
34
+
35
+ `Jenkins::Client::Job.all` pulls back a list of objects that represent all the Jenkins jobs on the server.
36
+
37
+ ### Find
38
+
39
+ `Jenkins::Client::Job.find("job_name")` pulls back a single Jenkins job based on the job name.
40
+
41
+ ### Create
42
+
43
+ `Jenkins::Client::Job.create("job_name", config).should be_true` will create a new Jenkins job on the server based on the config you pass in. Jenkins uses XML config files on the server and this is what you should send as the config. Example
44
+
45
+ ``` xml
46
+ <?xml version='1.0' encoding='UTF-8'?>
47
+ <project>
48
+ <actions/>
49
+ <description></description>
50
+ <keepDependencies>false</keepDependencies>
51
+ <properties/>
52
+ <scm class="hudson.scm.NullSCM"/>
53
+ <canRoam>true</canRoam>
54
+ <disabled>false</disabled>
55
+ <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
56
+ <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
57
+ <triggers class="vector"/>
58
+ <concurrentBuild>false</concurrentBuild>
59
+ <builders/>
60
+ <publishers/>
61
+ <buildWrappers/>
62
+ </project>
63
+ ```
64
+
65
+ To export an existing config simply look in the jobs path inside your Jenkins server and pull back a job's `config.xml` file.
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/jenkins-client/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["John Griffin"]
6
+ gem.email = ["johnog@gmail.com"]
7
+ gem.description = "List, find and create Jenkins jobs on a Jenkins CI server"
8
+ gem.summary = "List, find and create Jenkins jobs on a Jenkins CI server"
9
+ gem.homepage = "https://github.com/john-griffin/jenkins-client"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "jenkins-client"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Jenkins::Client::VERSION
17
+
18
+ gem.add_dependency "faraday", "~> 0.7.6"
19
+ gem.add_dependency "faraday_middleware", "~> 0.8.4"
20
+ gem.add_dependency 'rash', "~> 0.3.2"
21
+
22
+ gem.add_development_dependency "rspec", "~> 2.8.0"
23
+ gem.add_development_dependency "rake"
24
+ gem.add_development_dependency "webmock", "~> 1.7.10"
25
+ gem.add_development_dependency "guard-rspec", "~> 0.6.0"
26
+ end
@@ -0,0 +1,2 @@
1
+ require "jenkins-client/version"
2
+ require "jenkins-client/job"
@@ -0,0 +1,38 @@
1
+ require "faraday"
2
+ require "faraday_middleware"
3
+ require 'rash'
4
+
5
+ module Jenkins
6
+ class Client
7
+ class << self
8
+ attr_accessor :username, :password, :url, :connection
9
+
10
+ def configure
11
+ yield self
12
+ setup_connection
13
+ end
14
+
15
+ def setup_connection
16
+ @connection = Faraday.new(:url => url) do |builder|
17
+ builder.use FaradayMiddleware::Rashify
18
+ builder.use FaradayMiddleware::ParseJson
19
+ builder.adapter :net_http
20
+ end
21
+ @connection.basic_auth username, password
22
+ end
23
+
24
+ def get(path)
25
+ connection.get(path)
26
+ end
27
+
28
+ def post(path, body)
29
+ resp = @connection.post do |req|
30
+ req.headers['Content-Type'] = 'application/xml'
31
+ req.url path
32
+ req.body = body
33
+ end
34
+ resp.status == 200
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,20 @@
1
+ require "jenkins-client/client"
2
+
3
+ module Jenkins
4
+ class Client
5
+ class Job
6
+ def self.all
7
+ resp = Jenkins::Client.get "/api/json"
8
+ resp.body.jobs
9
+ end
10
+
11
+ def self.find(name)
12
+ all.select{|j| j.name == name}.first
13
+ end
14
+
15
+ def self.create(name, config)
16
+ Jenkins::Client.post("/createItem/api/xml?name=#{CGI.escape(name)}", config)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module Jenkins
2
+ class Client
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ <?xml version='1.0' encoding='UTF-8'?>
2
+ <project>
3
+ <actions/>
4
+ <description></description>
5
+ <keepDependencies>false</keepDependencies>
6
+ <properties/>
7
+ <scm class="hudson.scm.NullSCM"/>
8
+ <canRoam>true</canRoam>
9
+ <disabled>false</disabled>
10
+ <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
11
+ <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
12
+ <triggers class="vector"/>
13
+ <concurrentBuild>false</concurrentBuild>
14
+ <builders/>
15
+ <publishers/>
16
+ <buildWrappers/>
17
+ </project>
@@ -0,0 +1,107 @@
1
+ require "spec_helper"
2
+ require "jenkins-client"
3
+ require "jenkins-client/job"
4
+
5
+ describe Jenkins::Client::Job do
6
+ def jenkins_config(url)
7
+ Jenkins::Client.configure do |c|
8
+ c.username = "testuser"
9
+ c.password = "testpass"
10
+ c.url = url
11
+ end
12
+ end
13
+
14
+ def empty_jenkins_config
15
+ jenkins_config("https://emptyjenkinstest.com")
16
+ end
17
+
18
+ before(:each) do
19
+ jenkins_config("https://jenkinstest.com")
20
+ end
21
+
22
+ before(:each) do
23
+ body = '{"assignedLabels":[{}],"mode":"NORMAL","nodeDescription":"the master Jenkins node","nodeName":"","numExecutors":2,"description":null,"jobs":[
24
+ {"name":"foo-bar","url":"https://testjenkins.com/job/foo-bar/","color":"blue"},
25
+ {"name":"woohoo","url":"https://jenkinstest.com/job/woohoo/","color":"red"},
26
+ {"name":"wat","url":"https://jenkinstest.com/job/wat/","color":"red"},
27
+ {"name":"fudge","url":"https://jenkinstest.com/job/fudge/","color":"blue"},
28
+ {"name":"amaze","url":"https://jenkinstest.com/job/amaze/","color":"blue"}]}'
29
+ stub_request(:get, "https://testuser:testpass@jenkinstest.com/api/json").
30
+ with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
31
+ to_return(:status => 200, :body => body, :headers => {})
32
+ end
33
+
34
+ before(:each) do
35
+ body = '{"assignedLabels":[{}],"mode":"NORMAL","nodeDescription":"the master Jenkins node","nodeName":"","numExecutors":2,"description":null,"jobs":[]}'
36
+ stub_request(:get, "https://testuser:testpass@emptyjenkinstest.com/api/json").
37
+ with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
38
+ to_return(:status => 200, :body => body, :headers => {})
39
+ end
40
+
41
+ describe ".all" do
42
+ context "given some jobs are available" do
43
+ let(:jobs){Jenkins::Client::Job.all}
44
+
45
+ it "will return a list of jobs" do
46
+ jobs.should have(5).items
47
+ end
48
+
49
+ it "will contain an expected job" do
50
+ jobs.should include({
51
+ "name"=>"foo-bar",
52
+ "url"=>"https://testjenkins.com/job/foo-bar/",
53
+ "color"=>"blue"
54
+ })
55
+ end
56
+ end
57
+
58
+ context "given no jobs" do
59
+ before(:each) do
60
+ empty_jenkins_config
61
+ end
62
+
63
+ it "will have no jobs" do
64
+ Jenkins::Client::Job.all.should be_empty
65
+ end
66
+ end
67
+ end
68
+
69
+ describe ".find" do
70
+ context "given some jobs are available" do
71
+ it "will find the specified job" do
72
+ job = Jenkins::Client::Job.find("woohoo")
73
+ job.url.should eq("https://jenkinstest.com/job/woohoo/")
74
+ end
75
+ end
76
+
77
+ context "given a match isn't found" do
78
+ it "will be nil" do
79
+ Jenkins::Client::Job.find("badname").should be_nil
80
+ end
81
+ end
82
+
83
+ context "given no jobs" do
84
+ before(:each) do
85
+ empty_jenkins_config
86
+ end
87
+
88
+ it "will be nil" do
89
+ Jenkins::Client::Job.find("woohoo").should be_nil
90
+ end
91
+ end
92
+ end
93
+
94
+ describe ".create" do
95
+ before(:each) do
96
+ stub_request(:post, "https://testuser:testpass@jenkinstest.com/createItem/api/xml?name=excellent").
97
+ to_return(:status => 200, :body => "", :headers => {})
98
+ end
99
+
100
+ context "given an xml config file" do
101
+ it "will be able to create a new jenkins job" do
102
+ config = File.open("spec/fixtures/jenkins_config.xml").read
103
+ Jenkins::Client::Job.create("excellent", config).should be_true
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,6 @@
1
+ require 'webmock/rspec'
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jenkins-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Griffin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: &70266235683860 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.7.6
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70266235683860
25
+ - !ruby/object:Gem::Dependency
26
+ name: faraday_middleware
27
+ requirement: &70266235683100 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.8.4
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70266235683100
36
+ - !ruby/object:Gem::Dependency
37
+ name: rash
38
+ requirement: &70266235682080 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.3.2
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70266235682080
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70266235681340 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 2.8.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70266235681340
58
+ - !ruby/object:Gem::Dependency
59
+ name: rake
60
+ requirement: &70266235680480 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70266235680480
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: &70266235679620 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 1.7.10
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70266235679620
80
+ - !ruby/object:Gem::Dependency
81
+ name: guard-rspec
82
+ requirement: &70266235672500 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: 0.6.0
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70266235672500
91
+ description: List, find and create Jenkins jobs on a Jenkins CI server
92
+ email:
93
+ - johnog@gmail.com
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files: []
97
+ files:
98
+ - .gitignore
99
+ - .rspec
100
+ - .travis.yml
101
+ - Gemfile
102
+ - Guardfile
103
+ - LICENSE
104
+ - README.md
105
+ - Rakefile
106
+ - jenkins-client.gemspec
107
+ - lib/jenkins-client.rb
108
+ - lib/jenkins-client/client.rb
109
+ - lib/jenkins-client/job.rb
110
+ - lib/jenkins-client/version.rb
111
+ - spec/fixtures/jenkins_config.xml
112
+ - spec/jenkins-client/job_spec.rb
113
+ - spec/spec_helper.rb
114
+ homepage: https://github.com/john-griffin/jenkins-client
115
+ licenses: []
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ segments:
127
+ - 0
128
+ hash: 2674017292482177410
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ segments:
136
+ - 0
137
+ hash: 2674017292482177410
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 1.8.11
141
+ signing_key:
142
+ specification_version: 3
143
+ summary: List, find and create Jenkins jobs on a Jenkins CI server
144
+ test_files:
145
+ - spec/fixtures/jenkins_config.xml
146
+ - spec/jenkins-client/job_spec.rb
147
+ - spec/spec_helper.rb