Skip to content

Commit

Permalink
Merge pull request #16 from arangamani/adding_buildqueue_class
Browse files Browse the repository at this point in the history
Adding buildqueue class
  • Loading branch information
arangamani committed Jan 27, 2013
2 parents 268906e + 6cdc8c7 commit 71f7dee
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/jenkins_api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
require 'jenkins_api_client/node'
require 'jenkins_api_client/system'
require 'jenkins_api_client/view'
require 'jenkins_api_client/build_queue'

require 'jenkins_api_client/cli/helper'
require 'jenkins_api_client/cli/base'
Expand Down
213 changes: 213 additions & 0 deletions lib/jenkins_api_client/build_queue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
#
# Copyright (c) 2013 Kannan Manickam <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

module JenkinsApi
class Client
class BuildQueue

# Initializes a new BuildQueue object.
#
# @param [Object] client a reference to Client
#
def initialize(client)
@client = client
end

# Returns a string representation of BuildQueue class.
#
def to_s
"#<JenkinsApi::Client::BuildQueue>"
end

# Gives the number of jobs currently in the build queue
#
def size
response_json = @client.api_get_request("/queue")
response_json["items"].size
end

# Lists all tasks currently in the build queue
#
def list
response_json = @client.api_get_request("/queue")
tasks = []
response_json["items"].each do |item|
tasks << item["task"]["name"]
end
tasks
end

# Gets the time number of seconds the task is in the queue
#
# @param [String] task_name Name of the task/job
#
# @return [FixNum] age in seconds
#
def get_age(task_name)
age = nil
details = get_details(task_name)
unless details.empty?
age = Time.now - Time.at(details["inQueueSince"].to_i/1000)
end
age
end

# Obtains the detail Hash from the API response
#
# @param [String] task_name Name of the task/job
#
# @return [Hash] Queue details of the specified task/job
#
def get_details(task_name)
response_json = @client.api_get_request("/queue")
details = {}
response_json["items"].each do |item|
details = item if item["task"]["name"]
end
details
end

# Obtains the causes from the build queue for the specified task
#
# @param [String] task_name
#
# @return [Array] causes for the task to be in queue
#
def get_causes(task_name)
causes = nil
details = get_details(task_name)
unless details.empty?
causes = details["actions"]["causes"]
end
causes
end

# Obtains the reason why the task is in queue
#
# @param [String] task_name name of the task
#
# @return [String] reason for being in queue, nil if no task found
#
def get_reason(task_name)
reason = nil
details = get_details(task_name)
unless details.empty?
reason = details["why"]
end
reason
end

# Obtains the ETA given by Jenkins if any
#
# @param[String] task_name name of the task
#
# @return [String] ETA for the task, nil if no task found or ETA is
# not available
#
def get_eta(task_name)
eta = nil
details = get_details(task_name)
unless details.empty?
matched = details["why"].match(/.*\(ETA:(.*)\)/)
eta = matched[1].strip unless matched.nil?
end
eta
end

# Obtains the ID of the task from the queue
#
# @param [String] task_name name of the task
#
# @return [String] ID of the task, nil of no task is found
#
def get_id(task_name)
id = nil
details = get_details(task_name)
unless details.empty?
id = details["id"]
end
id
end

# Obtains the params from the build queue
#
# @param [String] task_name name of the task
#
# @return [String] params, nil if the no task is found
#
def get_params(task_name)
params = nil
details = get_details(task_name)
unless details.empty?
params = details["params"]
end
params
end

# Obtains whether the task is buildable
#
# @param [String] task_name name of the task
#
# @return [TrueClass|FalseClass] buildable or not
#
def is_buildable?(task_name)
buildable = nil
details = get_details(task_name)
unless details.empty?
buildable = details["buildable"] == "true" ? true : false
end
buildable
end

# Obtains whether the task is blocked
#
# @param [String] task_name name of the task
#
# @return [TrueClass|FalseClass] blocked or not
#
def is_blocked?(task_name)
blocked = nil
details = get_details(task_name)
unless details.empty?
blocked = details["blocked"] == "true" ? true : false
end
blocked
end

# Obtains whether the task is stuck
#
# @param [String] task_name name of the task
#
# @return [TrueClass|FalseClass] stuck or not
#
def is_stuck?(task_name)
stuck = nil
details = get_details(task_name)
unless details.empty?
stuck = details["stuck"] == "true" ? true : false
end
stuck
end

end
end
end
6 changes: 6 additions & 0 deletions lib/jenkins_api_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ def view
JenkinsApi::Client::View.new(self)
end

# Creates an instance to the BuildQueue by passing a reference to self
#
def queue
JenkinsApi::Client::BuildQueue.new(self)
end

# Returns a string representing the class name
#
def to_s
Expand Down
12 changes: 12 additions & 0 deletions spec/unit_tests/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@
client.system.class.should == JenkinsApi::Client::System
end
end

describe "#queue" do
it "Should return a Client::BuildQueue object" do
client = JenkinsApi::Client.new(
:server_ip => '127.0.0.1',
:server_port => 8080,
:username => 'username',
:password => 'password'
)
client.queue.class.should == JenkinsApi::Client::BuildQueue
end
end
end

describe "InstanceMethods" do
Expand Down

0 comments on commit 71f7dee

Please sign in to comment.