From b1c8fe842b7e1a351a4b1930b7ac5b80ea525638 Mon Sep 17 00:00:00 2001 From: Joel Neubert Date: Wed, 10 Sep 2014 10:27:10 -0600 Subject: [PATCH 1/6] this new method is working but requires you to pass in login credentials that the Jenkins API Client already knows about --- lib/jenkins_api_client/job.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/jenkins_api_client/job.rb b/lib/jenkins_api_client/job.rb index faeebd56..e056df7c 100644 --- a/lib/jenkins_api_client/job.rb +++ b/lib/jenkins_api_client/job.rb @@ -21,6 +21,8 @@ # require 'jenkins_api_client/urihelper' +require 'net/https' +require 'pry' module JenkinsApi class Client @@ -1425,6 +1427,22 @@ def get_promotions(job_name) result end + #A Method to find and download artifacts from the Current Build + # + # @param [String] job_name + # @param [String] save_path location to save artifact + + def curl_current_build(job_name,save_path, user, password) + current_build_number = get_current_build_number(job_name) + job_path = "job/#{path_encode job_name}/" + response_json = @client.api_get_request("/#{job_path}#{current_build_number}") + relative_build_path = response_json['artifacts'][0]['relativePath'] + jenkins_path = response_json['url'] + curl_path = URI.escape("#{jenkins_path}artifact/#{relative_build_path}") + binding.pry + exec %{`curl -o #{save_path} -k "#{curl_path}" --ssl --user #{user}:#{password}`} + end + private # Obtains the threshold params used by jenkins in the XML file From 10d898604192918ea34dd922d22f95e8b39c0c1a Mon Sep 17 00:00:00 2001 From: Joel Neubert Date: Wed, 10 Sep 2014 10:33:20 -0600 Subject: [PATCH 2/6] removed pry --- lib/jenkins_api_client/job.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/jenkins_api_client/job.rb b/lib/jenkins_api_client/job.rb index e056df7c..d16cdd5e 100644 --- a/lib/jenkins_api_client/job.rb +++ b/lib/jenkins_api_client/job.rb @@ -22,7 +22,6 @@ require 'jenkins_api_client/urihelper' require 'net/https' -require 'pry' module JenkinsApi class Client @@ -1439,7 +1438,6 @@ def curl_current_build(job_name,save_path, user, password) relative_build_path = response_json['artifacts'][0]['relativePath'] jenkins_path = response_json['url'] curl_path = URI.escape("#{jenkins_path}artifact/#{relative_build_path}") - binding.pry exec %{`curl -o #{save_path} -k "#{curl_path}" --ssl --user #{user}:#{password}`} end From 8687e49f806436a49f7927a8779858348a062118 Mon Sep 17 00:00:00 2001 From: Joel Neubert Date: Wed, 10 Sep 2014 14:10:17 -0600 Subject: [PATCH 3/6] update to use Net::HTTP instead of curl --- lib/jenkins_api_client/client.rb | 12 ++++++++++++ lib/jenkins_api_client/job.rb | 9 +++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/jenkins_api_client/client.rb b/lib/jenkins_api_client/client.rb index fb7cfa38..50413823 100644 --- a/lib/jenkins_api_client/client.rb +++ b/lib/jenkins_api_client/client.rb @@ -249,6 +249,18 @@ def inspect " @http_read_timeout=#{@http_read_timeout.inspect}>" end + def get_artifact(job_name,filename) + @artifact = job.find_artifact(job_name) + uri = URI.parse(@artifact) + http = Net::HTTP.new(uri.host, uri.port) + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + http.use_ssl = true + request = Net::HTTP::Get.new(uri.request_uri) + request.basic_auth(@username, @password) + response = http.request(request) + File.write(filename, response.body) + end + # Connects to the Jenkins server, sends the specified request and returns # the response. # diff --git a/lib/jenkins_api_client/job.rb b/lib/jenkins_api_client/job.rb index d16cdd5e..64792f63 100644 --- a/lib/jenkins_api_client/job.rb +++ b/lib/jenkins_api_client/job.rb @@ -22,6 +22,7 @@ require 'jenkins_api_client/urihelper' require 'net/https' +require 'pry' module JenkinsApi class Client @@ -1426,19 +1427,19 @@ def get_promotions(job_name) result end - #A Method to find and download artifacts from the Current Build + #A Method to find artifacts path from the Current Build # # @param [String] job_name # @param [String] save_path location to save artifact - def curl_current_build(job_name,save_path, user, password) + def find_artifact(job_name) current_build_number = get_current_build_number(job_name) job_path = "job/#{path_encode job_name}/" response_json = @client.api_get_request("/#{job_path}#{current_build_number}") relative_build_path = response_json['artifacts'][0]['relativePath'] jenkins_path = response_json['url'] - curl_path = URI.escape("#{jenkins_path}artifact/#{relative_build_path}") - exec %{`curl -o #{save_path} -k "#{curl_path}" --ssl --user #{user}:#{password}`} + artifact_path = URI.escape("#{jenkins_path}artifact/#{relative_build_path}") + return artifact_path end private From b7bd3032226c973a933e7970a331546d0c7aba05 Mon Sep 17 00:00:00 2001 From: Joel Neubert Date: Wed, 10 Sep 2014 14:14:54 -0600 Subject: [PATCH 4/6] added notes to method --- lib/jenkins_api_client/client.rb | 5 +++++ lib/jenkins_api_client/job.rb | 3 +-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/jenkins_api_client/client.rb b/lib/jenkins_api_client/client.rb index 50413823..3ec8e2b6 100644 --- a/lib/jenkins_api_client/client.rb +++ b/lib/jenkins_api_client/client.rb @@ -249,6 +249,11 @@ def inspect " @http_read_timeout=#{@http_read_timeout.inspect}>" end + #Connects to the server and downloads artifacts to a specified location + # + # @param [String] job_name + # @param [String] filename location to save artifact + # def get_artifact(job_name,filename) @artifact = job.find_artifact(job_name) uri = URI.parse(@artifact) diff --git a/lib/jenkins_api_client/job.rb b/lib/jenkins_api_client/job.rb index 64792f63..a95fdb67 100644 --- a/lib/jenkins_api_client/job.rb +++ b/lib/jenkins_api_client/job.rb @@ -1430,8 +1430,7 @@ def get_promotions(job_name) #A Method to find artifacts path from the Current Build # # @param [String] job_name - # @param [String] save_path location to save artifact - + # def find_artifact(job_name) current_build_number = get_current_build_number(job_name) job_path = "job/#{path_encode job_name}/" From a68eeee5c9c176e5f0b6e97d5cb3bb6b42fe5f2f Mon Sep 17 00:00:00 2001 From: Joel Neubert Date: Wed, 10 Sep 2014 15:16:19 -0600 Subject: [PATCH 5/6] check for response.code, fix to use File.expand_path, and removed pry --- lib/jenkins_api_client/client.rb | 4 ++-- lib/jenkins_api_client/job.rb | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/jenkins_api_client/client.rb b/lib/jenkins_api_client/client.rb index 3ec8e2b6..6634cec4 100644 --- a/lib/jenkins_api_client/client.rb +++ b/lib/jenkins_api_client/client.rb @@ -249,7 +249,7 @@ def inspect " @http_read_timeout=#{@http_read_timeout.inspect}>" end - #Connects to the server and downloads artifacts to a specified location + # Connects to the server and downloads artifacts to a specified location # # @param [String] job_name # @param [String] filename location to save artifact @@ -263,7 +263,7 @@ def get_artifact(job_name,filename) request = Net::HTTP::Get.new(uri.request_uri) request.basic_auth(@username, @password) response = http.request(request) - File.write(filename, response.body) + File.write(File.expand_path(filename), response.body) if response.code == 200 end # Connects to the Jenkins server, sends the specified request and returns diff --git a/lib/jenkins_api_client/job.rb b/lib/jenkins_api_client/job.rb index a95fdb67..c14f2b77 100644 --- a/lib/jenkins_api_client/job.rb +++ b/lib/jenkins_api_client/job.rb @@ -22,7 +22,6 @@ require 'jenkins_api_client/urihelper' require 'net/https' -require 'pry' module JenkinsApi class Client From 08e5dc76da5e2ce9c21d3ab927f1a13434a85b06 Mon Sep 17 00:00:00 2001 From: Joel Neubert Date: Fri, 3 Oct 2014 18:33:32 -0600 Subject: [PATCH 6/6] requested changes to jenkins api client, (sorry for the delay) --- lib/jenkins_api_client/client.rb | 4 +++- lib/jenkins_api_client/job.rb | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/jenkins_api_client/client.rb b/lib/jenkins_api_client/client.rb index 6634cec4..1d8ca17e 100644 --- a/lib/jenkins_api_client/client.rb +++ b/lib/jenkins_api_client/client.rb @@ -263,7 +263,9 @@ def get_artifact(job_name,filename) request = Net::HTTP::Get.new(uri.request_uri) request.basic_auth(@username, @password) response = http.request(request) - File.write(File.expand_path(filename), response.body) if response.code == 200 + File.write(File.expand_path(filename), response.body) if response.code == "200" + rescue + raise "Response was not 200" end # Connects to the Jenkins server, sends the specified request and returns diff --git a/lib/jenkins_api_client/job.rb b/lib/jenkins_api_client/job.rb index c14f2b77..b8cab15b 100644 --- a/lib/jenkins_api_client/job.rb +++ b/lib/jenkins_api_client/job.rb @@ -21,7 +21,6 @@ # require 'jenkins_api_client/urihelper' -require 'net/https' module JenkinsApi class Client