jenkins_api_client 0.3.2 → 0.4.0

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/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,12 @@
1
1
  = CHANGELOG
2
2
 
3
+ ===== v0.4.0 [07-DEC-2012]
4
+ * Added some methods for handling jobs.
5
+ * The status 'not run' is not returned as 'not_run' from job status.
6
+
7
+ ===== v0.3.2 [17-NOV-2012]
8
+ * Added some new methods for Job class
9
+
3
10
  ===== v0.3.1 [11-NOV-2012]
4
11
  * Removed unnecessary debug statements
5
12
 
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Jenkins API Client
2
2
  ==================
3
3
 
4
- Copyright © 2012, Kannan Manickam.
4
+ Copyright © 2012, Kannan Manickam [![endorse](http://api.coderwall.com/arangamani/endorsecount.png)](http://coderwall.com/arangamani)
5
5
 
6
6
  Client libraries for communicating with a Jenkins CI server and programatically managing jobs.
7
7
 
@@ -1,2 +1,6 @@
1
- Dir[File.dirname(__FILE__) + '/jenkins_api_client/*.rb'].each {|file| require file }
2
- Dir[File.dirname(__FILE__) + '/jenkins_api_client/cli/*.rb'].each {|file| require file }
1
+ Dir[File.dirname(__FILE__) + '/jenkins_api_client/*.rb'].each do |file|
2
+ require file
3
+ end
4
+ Dir[File.dirname(__FILE__) + '/jenkins_api_client/cli/*.rb'].each do |file|
5
+ require file
6
+ end
@@ -53,6 +53,21 @@ module JenkinsApi
53
53
  @client.api_post_request("/job/#{job_name}/doDelete")
54
54
  end
55
55
 
56
+ # Stops a running build of a job
57
+ # This method will stop the current/most recent build if no build number
58
+ # is specified. The build will be stopped only if it was in 'running' state.
59
+ #
60
+ # @param [String] job_name
61
+ # @param [Number] build_number
62
+ #
63
+ def stop_build(job_name, build_number = 0)
64
+ build_number = get_current_build_number(job_name) if build_number == 0
65
+ raise "No builds for #{job_name}" unless build_number
66
+ # Check and see if the build is running
67
+ is_building = @client.api_get_request("/job/#{job_name}/#{build_number}")["building"]
68
+ @client.api_post_request("/job/#{job_name}/#{build_number}/stop") if is_building
69
+ end
70
+
56
71
  # Re-create the same job
57
72
  # This is a hack to clear any existing builds
58
73
  #
@@ -75,6 +90,14 @@ module JenkinsApi
75
90
  jobs.sort!
76
91
  end
77
92
 
93
+ # Checks if the given job exists in Jenkins
94
+ #
95
+ # @param [String] job_name
96
+ #
97
+ def exists?(job_name)
98
+ list(job_name).include?(job_name) ? true : false
99
+ end
100
+
78
101
  # List all Jobs matching the given status
79
102
  # You can optionally pass in jobs list to filter the status from
80
103
  #
@@ -166,7 +189,7 @@ module JenkinsApi
166
189
  when "grey_anime", "blue_anime", "red_anime"
167
190
  "running"
168
191
  when "grey"
169
- "not run"
192
+ "not_run"
170
193
  when "aborted"
171
194
  "aborted"
172
195
  else
@@ -185,6 +208,16 @@ module JenkinsApi
185
208
  color_to_status(response_json["color"])
186
209
  end
187
210
 
211
+ # Obtain the current build number of the given job
212
+ # This function returns nil if there were no builds for the given job name.
213
+ #
214
+ # @param [String] job_name
215
+ #
216
+ def get_current_build_number(job_name)
217
+ builds = get_builds(job_name)
218
+ builds.length > 0 ? builds.first["number"] : nil
219
+ end
220
+
188
221
  # This functions lists all jobs that are currently running on the Jenkins CI server
189
222
  #
190
223
  def list_running
@@ -23,8 +23,8 @@
23
23
  module JenkinsApi
24
24
  class Client
25
25
  MAJOR = 0
26
- MINOR = 3
27
- TINY = 2
26
+ MINOR = 4
27
+ TINY = 0
28
28
  PRE = nil
29
29
  VERSION = [MAJOR, MINOR, TINY, PRE].compact.join('.')
30
30
  end
data/spec/client_spec.rb CHANGED
@@ -47,6 +47,6 @@ describe JenkinsApi::Client do
47
47
  client3 = JenkinsApi::Client.new(YAML.load_file(File.expand_path(@creds_file, __FILE__)))
48
48
  client3.class.should == JenkinsApi::Client
49
49
  end
50
-
50
+
51
51
  end
52
52
  end
data/spec/job_spec.rb CHANGED
@@ -96,7 +96,7 @@ describe JenkinsApi::Client::Job do
96
96
  it "Should obtain the current build status for the specified job" do
97
97
  build_status = @client.job.get_current_build_status(@job_name)
98
98
  build_status.class.should == String
99
- valid_build_status = ["not run", "aborted", "success", "failure", "unstable", "running"]
99
+ valid_build_status = ["not_run", "aborted", "success", "failure", "unstable", "running"]
100
100
  valid_build_status.include?(build_status).should be_true
101
101
  end
102
102
 
@@ -108,6 +108,24 @@ describe JenkinsApi::Client::Job do
108
108
  @client.job.get_current_build_status(@job_name).should_not == "running"
109
109
  response = @client.job.build(@job_name)
110
110
  response.to_i.should == 302
111
+ # Sleep for 20 seconds so we don't hit the Jenkins quiet period
112
+ sleep 20
113
+ @client.job.get_current_build_status(@job_name).should == "running"
114
+ while @client.job.get_current_build_status(@job_name) == "running" do
115
+ # Waiting for this job to finish so it doesn't affect other tests
116
+ sleep 10
117
+ end
118
+ end
119
+
120
+ it "Should be able to abort a recent build of a running job" do
121
+ @client.job.get_current_build_status(@job_name).should_not == "running"
122
+ @client.job.build(@job_name)
123
+ sleep 20
124
+ @client.job.get_current_build_status(@job_name).should == "running"
125
+ sleep 20
126
+ @client.job.stop_build(@job_name).should == 302
127
+ sleep 20
128
+ @client.job.get_current_build_status(@job_name).should == "aborted"
111
129
  end
112
130
 
113
131
  it "Should be able to restrict a job to a node" do
@@ -124,9 +142,7 @@ describe JenkinsApi::Client::Job do
124
142
  start_jobs.class.should == Array
125
143
  start_jobs.length.should == 1
126
144
 
127
- #
128
- #
129
- start_jobs = @client.job.chain(jobs, 'failure', ["not run", "aborted", 'failure'], 3)
145
+ start_jobs = @client.job.chain(jobs, 'failure', ["not_run", "aborted", 'failure'], 3)
130
146
  start_jobs.class.should == Array
131
147
  start_jobs.length.should == 3
132
148
  end
data/spec/spec_helper.rb CHANGED
@@ -33,7 +33,7 @@ module JenkinsApiSpecHelper
33
33
  xml.concurrentBuild "false"
34
34
  xml.builders {
35
35
  xml.send("hudson.tasks.Shell") {
36
- xml.command "\necho 'done'\necho 'done again'"
36
+ xml.command "\necho 'going to take a nice nap'\nsleep 120\necho 'took a nice nap'"
37
37
  }
38
38
  }
39
39
  xml.publishers
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jenkins_api_client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 2
10
- version: 0.3.2
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kannan Manickam
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-11-18 00:00:00 Z
18
+ date: 2012-12-08 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  prerelease: false