From d8ba8df10ebe6973ff04440b5ebc8609f3f03360 Mon Sep 17 00:00:00 2001 From: Nicolas De los Santos Date: Fri, 4 Mar 2016 08:17:29 +0100 Subject: [PATCH] Queue item cancel feature --- lib/jenkins_api_client.rb | 1 + lib/jenkins_api_client/build_queue.rb | 10 ++++++ lib/jenkins_api_client/queue_item.rb | 51 +++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 lib/jenkins_api_client/queue_item.rb diff --git a/lib/jenkins_api_client.rb b/lib/jenkins_api_client.rb index 7afdcda3..696c7021 100644 --- a/lib/jenkins_api_client.rb +++ b/lib/jenkins_api_client.rb @@ -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' diff --git a/lib/jenkins_api_client/build_queue.rb b/lib/jenkins_api_client/build_queue.rb index 09a294e6..cdb9be7e 100644 --- a/lib/jenkins_api_client/build_queue.rb +++ b/lib/jenkins_api_client/build_queue.rb @@ -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 diff --git a/lib/jenkins_api_client/queue_item.rb b/lib/jenkins_api_client/queue_item.rb new file mode 100644 index 00000000..5537a2f1 --- /dev/null +++ b/lib/jenkins_api_client/queue_item.rb @@ -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