Skip to content

Commit

Permalink
Merge pull request #243 from mmcrockett/_FixExceptionOnBadArtifact
Browse files Browse the repository at this point in the history
Throw more meaningful exception than NoMethodError when no artifact exists.
  • Loading branch information
katelovescode committed Nov 27, 2017
2 parents 84c7621 + c936b95 commit b65d2e7
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 7 deletions.
50 changes: 43 additions & 7 deletions lib/jenkins_api_client/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1546,17 +1546,34 @@ def delete_promote_config(job_name, process)
#A Method to find artifacts path from the Current Build
#
# @param [String] job_name
# @param [Integer] build_number
# defaults to latest build
#
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']
artifact_path = URI.escape("#{jenkins_path}artifact/#{relative_build_path}")
def find_artifact(job_name, build_number = 0)
response_json = get_build_details(job_name, build_number)
relative_build_path = artifact_path(build_details: response_json)
jenkins_path = response_json['url']
artifact_path = URI.escape("#{jenkins_path}artifact/#{relative_build_path}")

return artifact_path
end

#A Method to check artifact exists path from the Current Build
#
# @param [String] job_name
# @param [Integer] build_number
# defaults to latest build
#
def artifact_exists?(job_name, build_number = 0)
begin
artifact_path(job_name: job_name, build_number: build_number)

return true
rescue Exception => e
return false
end
end

private

# Obtains the threshold params used by jenkins in the XML file
Expand Down Expand Up @@ -1795,6 +1812,25 @@ def tree_string tree_value
return nil unless tree_value
"tree=#{tree_value}"
end

# This private method gets the artifact path or throws an exception
#
# @param [Hash] job_name, build_number or build_details object
#
def artifact_path(params)
job_name = params[:job_name]
build_number = params[:build_number] || 0
build_details = params[:build_details]

build_details = get_build_details(job_name, build_number) if build_details.nil?
artifacts = build_details['artifacts']

if ((nil != artifacts) && (false == artifacts.empty?) && (true == artifacts.first.include?('relativePath')))
return artifacts.first['relativePath']
else
raise "No artifacts found."
end
end
end
end
end
94 changes: 94 additions & 0 deletions spec/unit_tests/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
}
@sample_job_xml = File.read(
File.expand_path('../fixtures/files/job_sample.xml', __FILE__))
@sample_json_build_response = {
"url" => "https://example.com/DEFAULT-VIEW/view/VIEW-NAME/job/test_job/2/",
"artifacts" => [
{
"displayPath" => "output.json",
"fileName" => "output.json",
"relativePath" => "somepath/output.json"
}
]
}
end

describe "InstanceMethods" do
Expand Down Expand Up @@ -680,6 +690,90 @@
expect(@xml_config.at_css('scm gitTool').content).to eql('Git_NoPath')
end
end

describe "#get_build_details" do
it "accepts job name and build number" do
@client.should_receive(:api_get_request).and_return(@sample_json_build_response)
job_name = 'test_job'
response = @job.get_build_details(job_name, 1)
response.class.should == Hash
end

it "accepts job name and gets latest build number if build number is 0" do
@client.should_receive(:api_get_request).and_return(@sample_json_job_response, @sample_json_build_response)
job_name = 'test_job'
response = @job.get_build_details(job_name, 0)
response.class.should == Hash
end
end

describe "#find_artifact" do
it "accepts job name and build number and return artifact path" do
expected_path = URI.escape("https://example.com/DEFAULT-VIEW/view/VIEW-NAME/job/test_job/2/artifact/somepath/output.json")
@client.should_receive(:api_get_request).and_return(@sample_json_build_response)
expect(@job.find_artifact('test_job', 1)).to eql(expected_path)
end

it "accepts job name and uses latest build number if build number not provided and return artifact path" do
expected_path = URI.escape("https://example.com/DEFAULT-VIEW/view/VIEW-NAME/job/test_job/2/artifact/somepath/output.json")
@client.should_receive(:api_get_request).and_return(@sample_json_job_response, @sample_json_build_response)
expect(@job.find_artifact('test_job')).to eql(expected_path)
end

it "raises if artifact is missing" do
modified_response = JSON.parse(@sample_json_build_response.to_json)
modified_response.delete('artifacts')
@client.should_receive(:api_get_request).and_return(modified_response)
expect(lambda { @job.find_artifact('test_job', 1) }).to raise_error("No artifacts found.")
end

it "raises if artifact array is missing" do
modified_response = JSON.parse(@sample_json_build_response.to_json)
modified_response['artifacts'].clear()
@client.should_receive(:api_get_request).and_return(modified_response)
expect(lambda { @job.find_artifact('test_job', 1) }).to raise_error("No artifacts found.")
end

it "raises if artifact array has no relative path" do
modified_response = JSON.parse(@sample_json_build_response.to_json)
modified_response['artifacts'].first.delete('relativePath')
@client.should_receive(:api_get_request).and_return(modified_response)
expect(lambda { @job.find_artifact('test_job', 1) }).to raise_error("No artifacts found.")
end
end

describe "#artifact_exists?" do
it "accepts job name and build number and returns true when artifact exists and has path" do
@client.should_receive(:api_get_request).and_return(@sample_json_build_response)
expect(@job.artifact_exists?('test_job', 1)).to eql(true)
end

it "accepts job name and uses latest build number if build number is 0 and returns true when artifact exists and has path" do
@client.should_receive(:api_get_request).and_return(@sample_json_job_response, @sample_json_build_response)
expect(@job.artifact_exists?('test_job', 0)).to eql(true)
end

it "returns false if missing artifacts from json" do
modified_response = JSON.parse(@sample_json_build_response.to_json)
modified_response.delete('artifacts')
@client.should_receive(:api_get_request).and_return(modified_response)
expect(@job.artifact_exists?('test_job', 1)).to eql(false)
end

it "returns false if artifacts is empty array" do
modified_response = JSON.parse(@sample_json_build_response.to_json)
modified_response['artifacts'].clear()
@client.should_receive(:api_get_request).and_return(modified_response)
expect(@job.artifact_exists?('test_job', 1)).to eql(false)
end

it "returns false if no relative path is included in first artifact" do
modified_response = JSON.parse(@sample_json_build_response.to_json)
modified_response['artifacts'].first.delete('relativePath')
@client.should_receive(:api_get_request).and_return(modified_response)
expect(@job.artifact_exists?('test_job', 1)).to eql(false)
end
end
end

describe '#build_freestyle_config' do
Expand Down

0 comments on commit b65d2e7

Please sign in to comment.