jenkins_api_client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "nokogiri"
4
+ gem "activesupport"
5
+
6
+ group :development do
7
+ gem "bundler", "~> 1.2.1"
8
+ gem "jeweler", "~> 1.6.4"
9
+ gem "builder", "~> 3.1.3"
10
+ end
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ jenkins_api_client
2
+ ==================
3
+
4
+ Client libraries for communicating with a Jenkins CI server
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../lib/jenkins_api_client', __FILE__)
2
+ require File.expand_path('../lib/jenkins_api_client/version', __FILE__)
3
+ require 'rake'
4
+ require 'jeweler'
5
+
6
+ Jeweler::Tasks.new do |gemspec|
7
+ gemspec.name = 'jenkins_api_client'
8
+ gemspec.version = JenkinsApi::Client::VERSION
9
+ gemspec.platform = Gem::Platform::RUBY
10
+ gemspec.date = Time.now.utc.strftime("%Y-%m-%d")
11
+ gemspec.require_path = 'lib'
12
+ gemspec.authors = [ 'Kannan Manickam' ]
13
+ gemspec.email = [ 'arangamani.kannan@gmail.com' ]
14
+ gemspec.homepage = 'https://github.com/arangamani/jenkins_api_client'
15
+ gemspec.summary = 'Jenkins JSON API Client'
16
+ gemspec.description = %{
17
+ This is a simple and easy-to-use Jenkins Api client with features focused on
18
+ automating Job configuration programaticaly and so forth}
19
+ gemspec.rubygems_version = '1.8.17'
20
+ gemspec.add_runtime_dependency 'json'
21
+ gemspec.add_development_dependency 'rake', '0.8.7'
22
+ gemspec.add_development_dependency 'activesupport'
23
+ gemspec.add_development_dependency 'bundler'
24
+ end
@@ -0,0 +1 @@
1
+ require File.expand_path('../jenkins_api_client/client', __FILE__)
@@ -0,0 +1,71 @@
1
+ require 'rubygems'
2
+ require 'json'
3
+ require 'net/http'
4
+ require 'nokogiri'
5
+ require 'active_support/core_ext'
6
+ require 'active_support/builder'
7
+
8
+ require File.expand_path('../version', __FILE__)
9
+ require File.expand_path('../exceptions', __FILE__)
10
+ require File.expand_path('../job', __FILE__)
11
+
12
+ module JenkinsApi
13
+ class Client
14
+
15
+ attr_accessor :server_ip, :server_port, :username, :password
16
+ DEFAULT_SERVER_PORT = 8080
17
+ VALID_PARAMS = %w(server_ip server_port username password)
18
+
19
+ # @param [Hash] args
20
+ def initialize(args)
21
+ args.each { |key, value|
22
+ instance_variable_set("@#{key}", value) if value
23
+ } if args.is_a? Hash
24
+ raise "Server IP is required to connect to Jenkins Server" unless @server_ip
25
+ raise "Credentials are required to connect to te Jenkins Server" unless @username && @password
26
+ @server_port = DEFAULT_SERVER_PORT unless @server_port
27
+ end
28
+
29
+ def job
30
+ JenkinsApi::Client::Job.new(self)
31
+ end
32
+
33
+ def to_s
34
+ "#<JenkinsApi::Client>"
35
+ end
36
+
37
+ def api_get_request(url_prefix)
38
+ http = Net::HTTP.start(@server_ip, @server_port)
39
+ request = Net::HTTP::Get.new("#{url_prefix}/api/json")
40
+ request.basic_auth @username, @password
41
+ response = http.request(request)
42
+ JSON.parse(response.body)
43
+ end
44
+
45
+ def api_post_request(url_prefix)
46
+ http = Net::HTTP.start(@server_ip, @server_port)
47
+ request = Net::HTTP::Post.new("#{url_prefix}")
48
+ request.basic_auth @username, @password
49
+ response = http.request(request)
50
+ end
51
+
52
+
53
+ def get_config(url_prefix)
54
+ http = Net::HTTP.start(@server_ip, @server_port)
55
+ request = Net::HTTP::Get.new("#{url_prefix}/config.xml")
56
+ request.basic_auth @username, @password
57
+ response = http.request(request)
58
+ response.body
59
+ end
60
+
61
+ def post_config(url_prefix, xml)
62
+ http = Net::HTTP.start(@server_ip, @server_port)
63
+ request = Net::HTTP::Post.new("#{url_prefix}/config.xml")
64
+ request.basic_auth @username, @password
65
+ request.body = xml
66
+ response = http.request(request)
67
+ response.code
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,11 @@
1
+ module JenkinsApi
2
+ module Exceptions
3
+ class ApiException < RuntimeError
4
+
5
+ def initialize(message="")
6
+ super("Error: #{message}")
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,151 @@
1
+ module JenkinsApi
2
+ class Client
3
+ class Job
4
+
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ def test_method
10
+ puts "Username is: #{@client}"
11
+ end
12
+
13
+ def list_all
14
+ response_json = @client.api_get_request("")
15
+ jobs = []
16
+ response_json["jobs"].each { |job|
17
+ jobs << job["name"]
18
+ }
19
+ jobs.sort!
20
+ end
21
+
22
+ def list(filter)
23
+ response_json = @client.api_get_request("")
24
+ jobs = []
25
+ response_json["jobs"].each { |job|
26
+ jobs << job["name"] if job["name"] =~ /#{filter}/i
27
+ }
28
+ jobs
29
+ end
30
+
31
+ def list_all_with_details
32
+ response_json = @client.api_get_request("")
33
+ response_json["jobs"]
34
+ end
35
+
36
+ def list_details(job_name)
37
+ @client.api_get_request("/job/#{job_name}")
38
+ end
39
+
40
+ def get_upstream_projects(job_name)
41
+ response_json = @client.api_get_request("/job/#{job_name}")
42
+ response_json["upstreamProjects"]
43
+ end
44
+
45
+ def get_downstream_projects(job_name)
46
+ response_json = @client.api_get_request("/job/#{job_name}")
47
+ response_json["downstreamProjects"]
48
+ end
49
+
50
+ def get_builds(job_name)
51
+ response_json = @client.api_get_request("/job/#{job_name}")
52
+ response_json["builds"]
53
+ end
54
+
55
+ def build(job_name)
56
+ @client.api_post_request("/job/#{job_name}/build")
57
+ end
58
+
59
+ def get_config(job_name)
60
+ @client.get_config("/job/#{job_name}")
61
+ end
62
+
63
+ def post_config(job_name, xml)
64
+ @client.post_config("/job/#{job_name}", xml)
65
+ end
66
+
67
+ def change_description(job_name)
68
+ xml = get_config(job_name)
69
+ n_xml = Nokogiri::XML(xml)
70
+ desc = n_xml.xpath("//description").first
71
+ desc.content = "Some description"
72
+ xml_modified = n_xml.to_xml
73
+ post_config(job_name, xml_modified)
74
+ end
75
+
76
+ def add_downstream_projects(job_name, downstream_project, threshold = 'success')
77
+ case threshold
78
+ when 'success'
79
+ name = 'SUCCESS'
80
+ ordinal = 0
81
+ color = 'BLUE'
82
+ when 'unstable'
83
+ name = 'UNSTABLE'
84
+ ordinal = 1
85
+ color = 'YELLOW'
86
+ when 'failure'
87
+ name = 'FAILURE'
88
+ ordinal = 2
89
+ color = 'RED'
90
+ end
91
+ xml = get_config(job_name)
92
+ n_xml = Nokogiri::XML(xml)
93
+ child_projects_node = n_xml.xpath("//childProjects").first
94
+ if child_projects_node
95
+ child_projects_node.content = child_projects_node.content + ", #{downstream_project}"
96
+ else
97
+ publisher_node = n_xml.xpath("//publishers").first
98
+ build_trigger_node = publisher_node.add_child("<hudson.tasks.BuildTrigger/>")
99
+ child_project_node = build_trigger_node.first.add_child("<childProjects>#{downstream_project}</childProjects>")
100
+ threshold_node = child_project_node.first.add_next_sibling("<threshold/>")
101
+ threshold_node.first.add_child("<name>#{name}</name><ordinal>#{ordinal}</ordinal><color>#{color}</color>")
102
+ end
103
+ xml_modified = n_xml.to_xml
104
+ post_config(job_name, xml_modified)
105
+ end
106
+
107
+ def remove_downstream_projects(job_name)
108
+ xml = get_config(job_name)
109
+ n_xml = Nokogiri::XML(xml)
110
+ n_xml.search("//hudson.tasks.BuildTrigger").each{ |node|
111
+ child_project_trigger = false
112
+ node.search("//childProjects").each { |child_node|
113
+ child_project_trigger = true
114
+ child_node.search("//threshold").each { |threshold_node|
115
+ threshold_node.children.each { |threshold_value_node|
116
+ threshold_value_node.content = nil
117
+ threshold_value_node.remove
118
+ }
119
+ threshold_node.content = nil
120
+ threshold_node.remove
121
+ }
122
+ child_node.content = nil
123
+ child_node.remove
124
+ }
125
+ node.content = nil
126
+ node.remove
127
+ }
128
+ publisher_node = n_xml.search("//publishers").first
129
+ publisher_node.content = nil if publisher_node.children.empty?
130
+ xml_modified = n_xml.to_xml
131
+ post_config(job_name, xml_modified)
132
+ end
133
+
134
+ def restrict_to_node(job_name, node_name)
135
+ xml = get_config(job_name)
136
+ n_xml = Nokogiri::XML(xml)
137
+ if (node = n_xml.xpath("//assignedNode").first)
138
+ node.content = node_name
139
+ else
140
+ project = n_xml.xpath("//scm").first
141
+ child_node = project.add_next_sibling("<assignedNode>#{node_name}</assignedNode>")
142
+ roam_node = n_xml.xpath("//canRoam").first
143
+ roam_node.content = "false"
144
+ end
145
+ xml_modified = n_xml.to_xml
146
+ post_config(job_name, xml_modified)
147
+ end
148
+
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,6 @@
1
+
2
+ module JenkinsApi
3
+ class Client
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,205 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jenkins_api_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kannan Manickam
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.2.1
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.2.1
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.6.4
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.6.4
78
+ - !ruby/object:Gem::Dependency
79
+ name: builder
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 3.1.3
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 3.1.3
94
+ - !ruby/object:Gem::Dependency
95
+ name: json
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rake
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - '='
116
+ - !ruby/object:Gem::Version
117
+ version: 0.8.7
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - '='
124
+ - !ruby/object:Gem::Version
125
+ version: 0.8.7
126
+ - !ruby/object:Gem::Dependency
127
+ name: activesupport
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: bundler
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ description: ! '
159
+
160
+ This is a simple and easy-to-use Jenkins Api client with features focused on
161
+
162
+ automating Job configuration programaticaly and so forth'
163
+ email:
164
+ - arangamani.kannan@gmail.com
165
+ executables: []
166
+ extensions: []
167
+ extra_rdoc_files:
168
+ - README.md
169
+ files:
170
+ - Gemfile
171
+ - README.md
172
+ - Rakefile
173
+ - lib/jenkins_api_client.rb
174
+ - lib/jenkins_api_client/client.rb
175
+ - lib/jenkins_api_client/exceptions.rb
176
+ - lib/jenkins_api_client/job.rb
177
+ - lib/jenkins_api_client/version.rb
178
+ homepage: https://github.com/arangamani/jenkins_api_client
179
+ licenses: []
180
+ post_install_message:
181
+ rdoc_options: []
182
+ require_paths:
183
+ - lib
184
+ required_ruby_version: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ! '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ segments:
191
+ - 0
192
+ hash: -29913673152348123
193
+ required_rubygems_version: !ruby/object:Gem::Requirement
194
+ none: false
195
+ requirements:
196
+ - - ! '>='
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
199
+ requirements: []
200
+ rubyforge_project:
201
+ rubygems_version: 1.8.24
202
+ signing_key:
203
+ specification_version: 3
204
+ summary: Jenkins JSON API Client
205
+ test_files: []