lib/jenkins/api.rb in jenkins-0.6.4 vs lib/jenkins/api.rb in jenkins-0.6.5

- old
+ new

@@ -11,22 +11,24 @@ include HTTParty headers 'content-type' => 'application/json' format :json # http_proxy 'localhost', '8888' - + JobAlreadyExistsError = Class.new(Exception) def self.setup_base_url(options) options = options.with_clean_keys # Thor's HashWithIndifferentAccess is based on string keys which URI::HTTP.build ignores options = options.inject({}) { |mem, (key, val)| mem[key.to_sym] = val; mem } + options = setup_authentication(options) options[:host] ||= ENV['JENKINS_HOST'] options[:port] ||= ENV['JENKINS_PORT'] options[:port] &&= options[:port].to_i return false unless options[:host] || Jenkins::Config.config["base_uri"] - uri = options[:host] ? URI::HTTP.build(options) : Jenkins::Config.config["base_uri"] + uri_class = options.delete(:ssl) ? URI::HTTPS : URI::HTTP + uri = options[:host] ? uri_class.build(options) : Jenkins::Config.config["base_uri"] base_uri uri.to_s uri end # returns true if successfully create a new job on Jenkins @@ -43,11 +45,11 @@ begin res = post "/createItem/api/xml?name=#{CGI.escape(name)}", { :body => job_config.to_xml, :format => :xml, :headers => { 'content-type' => 'application/xml' } } if res.code.to_i == 200 - cache_base_uri + cache_configuration! true else require "hpricot" doc = Hpricot(res.body) error_msg = doc.search("td#main-panel p") @@ -64,57 +66,57 @@ rescue REXML::ParseException => e # For some reason, if the job exists we get back half a page of HTML raise JobAlreadyExistsError.new(name) end end - + # Attempts to delete a job +name+ def self.delete_job(name) res = post_plain "#{job_url name}/doDelete" res.code.to_i == 302 end - + def self.build_job(name) res = get_plain "/job/#{name}/build" res.code.to_i == 302 end def self.summary json = get "/api/json" - cache_base_uri + cache_configuration! json end - + def self.job_names summary["jobs"].map {|job| job["name"]} end # Return hash of job statuses def self.job(name) begin json = get "/job/#{name}/api/json" - cache_base_uri + cache_configuration! json rescue Crack::ParseError false end end # Return a hash of information about a build. def self.build_details(job_name, build_number) begin json = get "/job/#{job_name}/#{build_number}/api/json" - cache_base_uri + cache_configuration! json rescue Crack::ParseError false end end def self.nodes json = get "/computer/api/json" - cache_base_uri + cache_configuration! json end # Adds SSH nodes only, for now def self.add_node(options = {}) @@ -140,11 +142,11 @@ :executors => 2, :exclusive => true ) end options = default_options.merge(options) - + slave_host = options[:slave_host] name = options[:name] || slave_host labels = options[:labels].split(/\s*,\s*/).join(' ') if options[:labels] type = "hudson.slaves.DumbSlave$DescriptorImpl" @@ -196,40 +198,52 @@ puts response.body # so we can find other errors end false end end - + def self.delete_node(name) post_plain("#{base_uri}/computer/#{CGI::escape(name).gsub('+', '%20')}/doDelete/api/json") end # Helper for POST that don't barf at Jenkins's crappy API responses def self.post_plain(path, data = "", options = {}) options = options.with_clean_keys uri = URI.parse base_uri - res = Net::HTTP.start(uri.host, uri.port) do |http| + res = Net::HTTP.start(uri.host, uri.port) do |http| if RUBY_VERSION =~ /1.8/ http.post(path, options) else http.post(path, data, options) end end end - + # Helper for GET that don't barf at Jenkins's crappy API responses def self.get_plain(path, options = {}) options = options.with_clean_keys uri = URI.parse base_uri res = Net::HTTP.start(uri.host, uri.port) { |http| http.get(path, options) } end - - private - def self.cache_base_uri + + def self.cache_configuration! Jenkins::Config.config["base_uri"] = base_uri + Jenkins::Config.config["basic_auth"] = default_options[:basic_auth] Jenkins::Config.store! end - + + private + def self.setup_authentication(options) + username, password = options.delete(:username), options.delete(:password) + if username && password + basic_auth username, password + elsif Jenkins::Config.config["basic_auth"] + basic_auth Jenkins::Config.config["basic_auth"]["username"], + Jenkins::Config.config["basic_auth"]["password"] + end + options + end + def self.job_url(name) "#{base_uri}/job/#{name}" end end end