Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions lib/gitlab/client/jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ def job_artifacts_download(project_id, ref_name, job_name)
parser: proc { |body, _|
if body.encoding == Encoding::ASCII_8BIT # binary response
::Gitlab::FileResponse.new StringIO.new(body, 'rb+')
else # error with json response
::Gitlab::Request.parse(body)
else
begin
::Gitlab::Request.parse(body) # JSON error envelope
rescue ::Gitlab::Error::Parsing
::Gitlab::FileResponse.new StringIO.new(body) # text artifact
end
end
})
end
Expand All @@ -119,8 +123,12 @@ def download_job_artifact_file(project_id, job_id, artifact_path)
parser: proc { |body, _|
if body.encoding == Encoding::ASCII_8BIT # binary response
::Gitlab::FileResponse.new StringIO.new(body, 'rb+')
else # error with json response
::Gitlab::Request.parse(body)
else
begin
::Gitlab::Request.parse(body) # JSON error envelope
rescue ::Gitlab::Error::Parsing
::Gitlab::FileResponse.new StringIO.new(body) # text artifact
end
end
})
end
Expand All @@ -143,8 +151,12 @@ def download_branch_artifact_file(project_id, ref_name, artifact_path, job)
parser: proc { |body, _|
if body.encoding == Encoding::ASCII_8BIT # binary response
::Gitlab::FileResponse.new StringIO.new(body, 'rb+')
else # error with json response
::Gitlab::Request.parse(body)
else
begin
::Gitlab::Request.parse(body) # JSON error envelope
rescue ::Gitlab::Error::Parsing
::Gitlab::FileResponse.new StringIO.new(body) # text artifact
end
end
})
end
Expand Down
69 changes: 69 additions & 0 deletions spec/gitlab/client/jobs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,29 @@
end
end

context 'when successful text response' do
before do
fixture = load_fixture('raw_file.txt')
@body = fixture.read
stub_request(:get, "#{Gitlab.endpoint}/projects/3/jobs/artifacts/master/download")
.with(query: { job: 'test' }, headers: { 'PRIVATE-TOKEN' => Gitlab.private_token })
.to_return(body: @body, headers: { 'Content-Disposition' => 'attachment; filename=raw_file.txt' })
@job_artifacts = Gitlab.job_artifacts_download(3, 'master', 'test')
end

it 'returns a FileResponse' do
expect(@job_artifacts).to be_a Gitlab::FileResponse
end

it 'returns a file with filename' do
expect(@job_artifacts.filename).to eq 'raw_file.txt'
end

it 'returns the body content' do
expect(@job_artifacts.read).to eq @body
end
end

context 'when bad request' do
it 'throws an exception' do
stub_get('/projects/3/jobs/artifacts/master/download', 'error_project_not_found', 404)
Expand Down Expand Up @@ -149,6 +172,29 @@
end
end

context 'when successful text response' do
before do
fixture = load_fixture('raw_file.txt')
@body = fixture.read
stub_request(:get, "#{Gitlab.endpoint}/projects/3/jobs/5/artifacts/raw_file.txt")
.with(headers: { 'PRIVATE-TOKEN' => Gitlab.private_token })
.to_return(body: @body, headers: { 'Content-Disposition' => 'attachment; filename=raw_file.txt' })
@job_artifact_file = Gitlab.download_job_artifact_file(3, 5, 'raw_file.txt')
end

it 'returns a FileResponse' do
expect(@job_artifact_file).to be_a Gitlab::FileResponse
end

it 'returns a file with filename' do
expect(@job_artifact_file.filename).to eq 'raw_file.txt'
end

it 'returns the body content' do
expect(@job_artifact_file.read).to eq @body
end
end

context 'when bad request' do
it 'throws an exception' do
stub_get('/projects/3/jobs/5/artifacts/raw_file.txt', 'error_project_not_found', 404)
Expand Down Expand Up @@ -182,6 +228,29 @@
end
end

context 'when successful text response' do
before do
fixture = load_fixture('raw_file.txt')
@body = fixture.read
stub_request(:get, "#{Gitlab.endpoint}/projects/1/jobs/artifacts/master/raw/raw_file.txt")
.with(query: { job: 'txt' }, headers: { 'PRIVATE-TOKEN' => Gitlab.private_token })
.to_return(body: @body, headers: { 'Content-Disposition' => 'attachment; filename=raw_file.txt' })
@branch_artifact_file = Gitlab.download_branch_artifact_file(1, 'master', 'raw_file.txt', 'txt')
end

it 'returns a FileResponse' do
expect(@branch_artifact_file).to be_a Gitlab::FileResponse
end

it 'returns a file with filename' do
expect(@branch_artifact_file.filename).to eq 'raw_file.txt'
end

it 'returns the body content' do
expect(@branch_artifact_file.read).to eq @body
end
end

context 'when bad request' do
it 'throws an exception' do
stub_get('/projects/1/jobs/artifacts/master/raw/raw_file.txt', 'error_project_not_found', 404)
Expand Down
Loading