Skip to content

Commit

Permalink
Queue item cancel feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas De los Santos committed Mar 4, 2016
1 parent d478f8a commit d8ba8df
Show file tree
Hide file tree
Showing 3 changed files with 62 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/queue_item'
require 'jenkins_api_client/build_queue'
require 'jenkins_api_client/plugin_manager'
require 'jenkins_api_client/user'
Expand Down
10 changes: 10 additions & 0 deletions lib/jenkins_api_client/build_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ def list
tasks
end

# Lists all tasks currently in the build queue as [QueueItem] objects
#
def list_tasks
@logger.info "Obtaining the tasks currently in the build queue"
response_json = @client.api_get_request("/queue")
tasks = response_json["items"].map do |item|
QueueItem.new(@client, item)
end
end

# Gets the time number of seconds the task is in the queue
#
# @param [String] task_name Name of the task/job
Expand Down
51 changes: 51 additions & 0 deletions lib/jenkins_api_client/queue_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module JenkinsApi
class Client

#
# This class represents an item in the building queue and allows interaction
# with an identified queue item.
#
class QueueItem
VALID_PARAMS = ['id', 'params'].freeze
attr_accessor *VALID_PARAMS.each {|attr| attr.to_sym}
attr_accessor :name

# Initializes a new QueueItem
#
# @param client [Cliente] the api client object
# @param json the json object with all the information of the queue item (as returned by api call "/queue")
#
# @return [QueueItem] the queue item object
#
def initialize(client, json)
@client = client
json.each do |key, value|
if VALID_PARAMS.include?(key)
instance_variable_set("@#{key}", value)
end
end if json.is_a? Hash
@name = json['task']['name']

# parseamos los parametros
@params = @params.split("\n").map do |p|
p.split('=')
end.select do |p|
not p.empty?
end
@params = Hash[@params]
end

# String representation of the queue item object
#
def to_s
self.name
end

# Removes the item from the building queue
#
def cancel
@client.api_post_request("/queue/cancelItem?id=#{self.id}")
end
end
end
end

0 comments on commit d8ba8df

Please sign in to comment.