jenkins_api_client 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +14 -0
- data/lib/jenkins_api_client/client.rb +28 -2
- data/lib/jenkins_api_client/job.rb +100 -9
- data/lib/jenkins_api_client/version.rb +1 -1
- metadata +5 -5
- data/README.md +0 -4
data/README.rdoc
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
= jenkins_api_client
|
2
|
+
|
3
|
+
Client libraries for communicating with a Jenkins CI server
|
4
|
+
|
5
|
+
== Sample Usage:
|
6
|
+
|
7
|
+
Install jenkins_api_client by <tt>sudo gem install jenkins_api_client</tt>
|
8
|
+
|
9
|
+
require 'jenkins_api_client'
|
10
|
+
|
11
|
+
client = JenkinsApi::Client.new(:server_ip => '0.0.0.0',
|
12
|
+
:username => 'somename', :password => 'secret password')
|
13
|
+
# The following call will return all jobs matching 'Testjob'
|
14
|
+
puts client.job.list("^Testjob")
|
@@ -12,11 +12,17 @@ require File.expand_path('../job', __FILE__)
|
|
12
12
|
module JenkinsApi
|
13
13
|
class Client
|
14
14
|
|
15
|
-
attr_accessor :server_ip, :server_port, :username, :password
|
16
15
|
DEFAULT_SERVER_PORT = 8080
|
17
16
|
VALID_PARAMS = %w(server_ip server_port username password)
|
18
17
|
|
18
|
+
# Initialize a Client object with Jenkins CI server information and credentials
|
19
|
+
#
|
19
20
|
# @param [Hash] args
|
21
|
+
# * the +:server_ip+ param is the IP address of the Jenkins CI server
|
22
|
+
# * the +:server_port+ param is the port on which the Jenkins server listens
|
23
|
+
# * the +:username+ param is the username used for connecting to the CI server
|
24
|
+
# * the +:password+ param is the password for connecting to the CI server
|
25
|
+
#
|
20
26
|
def initialize(args)
|
21
27
|
args.each { |key, value|
|
22
28
|
instance_variable_set("@#{key}", value) if value
|
@@ -26,14 +32,22 @@ module JenkinsApi
|
|
26
32
|
@server_port = DEFAULT_SERVER_PORT unless @server_port
|
27
33
|
end
|
28
34
|
|
35
|
+
# Creates an instance to the Job object by passing a reference to self
|
36
|
+
#
|
29
37
|
def job
|
30
38
|
JenkinsApi::Client::Job.new(self)
|
31
39
|
end
|
32
40
|
|
41
|
+
# Returns a string representing the class name
|
42
|
+
#
|
33
43
|
def to_s
|
34
44
|
"#<JenkinsApi::Client>"
|
35
45
|
end
|
36
46
|
|
47
|
+
# Sends a GET request to the Jenkins CI server with the specified URL
|
48
|
+
#
|
49
|
+
# @param [String] url_prefix
|
50
|
+
#
|
37
51
|
def api_get_request(url_prefix)
|
38
52
|
http = Net::HTTP.start(@server_ip, @server_port)
|
39
53
|
request = Net::HTTP::Get.new("#{url_prefix}/api/json")
|
@@ -42,6 +56,10 @@ module JenkinsApi
|
|
42
56
|
JSON.parse(response.body)
|
43
57
|
end
|
44
58
|
|
59
|
+
# Sends a POST message to the Jenkins CI server with the specified URL
|
60
|
+
#
|
61
|
+
# @param [String] url_prefix
|
62
|
+
#
|
45
63
|
def api_post_request(url_prefix)
|
46
64
|
http = Net::HTTP.start(@server_ip, @server_port)
|
47
65
|
request = Net::HTTP::Post.new("#{url_prefix}")
|
@@ -49,7 +67,10 @@ module JenkinsApi
|
|
49
67
|
response = http.request(request)
|
50
68
|
end
|
51
69
|
|
52
|
-
|
70
|
+
# Obtains the configuration of a component from the Jenkins CI server
|
71
|
+
#
|
72
|
+
# @param [String] url_prefix
|
73
|
+
#
|
53
74
|
def get_config(url_prefix)
|
54
75
|
http = Net::HTTP.start(@server_ip, @server_port)
|
55
76
|
request = Net::HTTP::Get.new("#{url_prefix}/config.xml")
|
@@ -58,6 +79,11 @@ module JenkinsApi
|
|
58
79
|
response.body
|
59
80
|
end
|
60
81
|
|
82
|
+
# Posts the given xml configuration to the url given
|
83
|
+
#
|
84
|
+
# @param [String] url_prefix
|
85
|
+
# @param [String] xml
|
86
|
+
#
|
61
87
|
def post_config(url_prefix, xml)
|
62
88
|
http = Net::HTTP.start(@server_ip, @server_port)
|
63
89
|
request = Net::HTTP::Post.new("#{url_prefix}/config.xml")
|
@@ -2,14 +2,14 @@ module JenkinsApi
|
|
2
2
|
class Client
|
3
3
|
class Job
|
4
4
|
|
5
|
+
# Initialize the Job object and store the reference to Client object
|
6
|
+
#
|
5
7
|
def initialize(client)
|
6
8
|
@client = client
|
7
9
|
end
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
-
|
11
|
+
# List all jobs on the Jenkins CI server
|
12
|
+
#
|
13
13
|
def list_all
|
14
14
|
response_json = @client.api_get_request("")
|
15
15
|
jobs = []
|
@@ -19,6 +19,10 @@ module JenkinsApi
|
|
19
19
|
jobs.sort!
|
20
20
|
end
|
21
21
|
|
22
|
+
# List all jobs that match the given regex
|
23
|
+
#
|
24
|
+
# @param [String] filter - a regex
|
25
|
+
#
|
22
26
|
def list(filter)
|
23
27
|
response_json = @client.api_get_request("")
|
24
28
|
jobs = []
|
@@ -28,52 +32,130 @@ module JenkinsApi
|
|
28
32
|
jobs
|
29
33
|
end
|
30
34
|
|
35
|
+
# List all jobs on the Jenkins CI server along with their details
|
36
|
+
#
|
31
37
|
def list_all_with_details
|
32
38
|
response_json = @client.api_get_request("")
|
33
39
|
response_json["jobs"]
|
34
40
|
end
|
35
41
|
|
42
|
+
# List details of a specific job
|
43
|
+
#
|
44
|
+
# @param [String] job_name
|
45
|
+
#
|
36
46
|
def list_details(job_name)
|
37
47
|
@client.api_get_request("/job/#{job_name}")
|
38
48
|
end
|
39
49
|
|
50
|
+
# List upstream projects of a specific job
|
51
|
+
#
|
52
|
+
# @param [String] job_name
|
53
|
+
#
|
40
54
|
def get_upstream_projects(job_name)
|
41
55
|
response_json = @client.api_get_request("/job/#{job_name}")
|
42
56
|
response_json["upstreamProjects"]
|
43
57
|
end
|
44
58
|
|
59
|
+
# List downstream projects of a specific job
|
60
|
+
#
|
61
|
+
# @param [String] job_name
|
62
|
+
#
|
45
63
|
def get_downstream_projects(job_name)
|
46
64
|
response_json = @client.api_get_request("/job/#{job_name}")
|
47
65
|
response_json["downstreamProjects"]
|
48
66
|
end
|
49
67
|
|
68
|
+
# Obtain build details of a specific job
|
69
|
+
#
|
70
|
+
# @param [String] job_name
|
71
|
+
#
|
50
72
|
def get_builds(job_name)
|
51
73
|
response_json = @client.api_get_request("/job/#{job_name}")
|
52
74
|
response_json["builds"]
|
53
75
|
end
|
54
76
|
|
77
|
+
# Obtain the current build status of the job
|
78
|
+
# By defaule Jenkins returns the color of the job status icon
|
79
|
+
# This function translates the color into a meaningful status
|
80
|
+
#
|
81
|
+
# @param [String] job_name
|
82
|
+
#
|
83
|
+
def get_current_build_status(job_name)
|
84
|
+
response_json = @client.api_get_request("/job/#{job_name}")
|
85
|
+
case response_json["color"]
|
86
|
+
when "blue"
|
87
|
+
"success"
|
88
|
+
when "red"
|
89
|
+
"failure"
|
90
|
+
when "yellow"
|
91
|
+
"unstable"
|
92
|
+
when "grey_anime", "blue_anime", "red_anime"
|
93
|
+
"running"
|
94
|
+
when "grey"
|
95
|
+
"not run"
|
96
|
+
when "aborted"
|
97
|
+
"aborted"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# This functions lists all jobs that are currently running on the Jenkins CI server
|
102
|
+
#
|
103
|
+
def list_running
|
104
|
+
jobs = list_all
|
105
|
+
running_jobs = []
|
106
|
+
jobs.each { |job|
|
107
|
+
running_jobs << job if get_current_build_status(job) == "running"
|
108
|
+
}
|
109
|
+
running_jobs
|
110
|
+
end
|
111
|
+
|
112
|
+
# Build a job given the name of the job
|
113
|
+
#
|
114
|
+
# @param [String] job_name
|
115
|
+
#
|
55
116
|
def build(job_name)
|
56
117
|
@client.api_post_request("/job/#{job_name}/build")
|
57
118
|
end
|
58
119
|
|
120
|
+
# Obtain the configuration stored in config.xml of a specific job
|
121
|
+
#
|
122
|
+
# @param [String] job_name
|
123
|
+
#
|
59
124
|
def get_config(job_name)
|
60
125
|
@client.get_config("/job/#{job_name}")
|
61
126
|
end
|
62
127
|
|
128
|
+
# Post the configuration of a job given the job name and the config.xml
|
129
|
+
#
|
130
|
+
# @param [String] job_name
|
131
|
+
# @param [String] xml
|
132
|
+
#
|
63
133
|
def post_config(job_name, xml)
|
64
134
|
@client.post_config("/job/#{job_name}", xml)
|
65
135
|
end
|
66
136
|
|
67
|
-
|
137
|
+
# Change the description of a specific job
|
138
|
+
#
|
139
|
+
# @param [String] job_name
|
140
|
+
# @param [String] description
|
141
|
+
#
|
142
|
+
def change_description(job_name, description)
|
68
143
|
xml = get_config(job_name)
|
69
144
|
n_xml = Nokogiri::XML(xml)
|
70
145
|
desc = n_xml.xpath("//description").first
|
71
|
-
desc.content = "
|
146
|
+
desc.content = "#{description}"
|
72
147
|
xml_modified = n_xml.to_xml
|
73
148
|
post_config(job_name, xml_modified)
|
74
149
|
end
|
75
150
|
|
76
|
-
|
151
|
+
# Add downstream projects to a specific job given the job name, projects to be
|
152
|
+
# added as downstream projects, and the threshold
|
153
|
+
#
|
154
|
+
# @param [String] job_name
|
155
|
+
# @param [String] downstream_projects
|
156
|
+
# @param [String] threshold - failure, success, or unstable
|
157
|
+
#
|
158
|
+
def add_downstream_projects(job_name, downstream_projects, threshold = 'success')
|
77
159
|
case threshold
|
78
160
|
when 'success'
|
79
161
|
name = 'SUCCESS'
|
@@ -92,11 +174,11 @@ module JenkinsApi
|
|
92
174
|
n_xml = Nokogiri::XML(xml)
|
93
175
|
child_projects_node = n_xml.xpath("//childProjects").first
|
94
176
|
if child_projects_node
|
95
|
-
child_projects_node.content = child_projects_node.content + ", #{
|
177
|
+
child_projects_node.content = child_projects_node.content + ", #{downstream_projects}"
|
96
178
|
else
|
97
179
|
publisher_node = n_xml.xpath("//publishers").first
|
98
180
|
build_trigger_node = publisher_node.add_child("<hudson.tasks.BuildTrigger/>")
|
99
|
-
child_project_node = build_trigger_node.first.add_child("<childProjects>#{
|
181
|
+
child_project_node = build_trigger_node.first.add_child("<childProjects>#{downstream_projects}</childProjects>")
|
100
182
|
threshold_node = child_project_node.first.add_next_sibling("<threshold/>")
|
101
183
|
threshold_node.first.add_child("<name>#{name}</name><ordinal>#{ordinal}</ordinal><color>#{color}</color>")
|
102
184
|
end
|
@@ -104,6 +186,10 @@ module JenkinsApi
|
|
104
186
|
post_config(job_name, xml_modified)
|
105
187
|
end
|
106
188
|
|
189
|
+
# Remove all downstream projects of a specific job
|
190
|
+
#
|
191
|
+
# @param [String] job_name
|
192
|
+
#
|
107
193
|
def remove_downstream_projects(job_name)
|
108
194
|
xml = get_config(job_name)
|
109
195
|
n_xml = Nokogiri::XML(xml)
|
@@ -131,6 +217,11 @@ module JenkinsApi
|
|
131
217
|
post_config(job_name, xml_modified)
|
132
218
|
end
|
133
219
|
|
220
|
+
# Resctrict the given job to a specific node
|
221
|
+
#
|
222
|
+
# @param [String] job_name
|
223
|
+
# @param [String] node_name
|
224
|
+
#
|
134
225
|
def restrict_to_node(job_name, node_name)
|
135
226
|
xml = get_config(job_name)
|
136
227
|
n_xml = Nokogiri::XML(xml)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jenkins_api_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
@@ -165,10 +165,10 @@ email:
|
|
165
165
|
executables: []
|
166
166
|
extensions: []
|
167
167
|
extra_rdoc_files:
|
168
|
-
- README.
|
168
|
+
- README.rdoc
|
169
169
|
files:
|
170
170
|
- Gemfile
|
171
|
-
- README.
|
171
|
+
- README.rdoc
|
172
172
|
- Rakefile
|
173
173
|
- lib/jenkins_api_client.rb
|
174
174
|
- lib/jenkins_api_client/client.rb
|
@@ -189,7 +189,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
189
189
|
version: '0'
|
190
190
|
segments:
|
191
191
|
- 0
|
192
|
-
hash: -
|
192
|
+
hash: -746284255980224518
|
193
193
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
194
194
|
none: false
|
195
195
|
requirements:
|
data/README.md
DELETED