Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

download_job_artifact_file method fails when the artifact is a non-JSON text file #621

Open
balasankarc opened this issue Jul 5, 2021 · 1 comment
Labels
bug Something isn't working help wanted Good for newcomers

Comments

@balasankarc
Copy link

balasankarc commented Jul 5, 2021

When trying to download an artifact file using the download_job_artifact_file, the response is incorrectly attempted to be JSON-parsed, and hence will fail for any non-JSON non-binary files.

Example: Try downloading artifacts from https://gitlab.com/balasankarc/test-gitlab-gem-artifact-download/-/jobs/1399004917. See example below

[1] pry(main)> valid_json_file = client.download_job_artifact_file(27906257, 1399004917, 'output/artifact.json')
=> #<Gitlab::FileResponse:46760 {filename: "artifact.json"}>

[2] pry(main)> jpg_file = client.download_job_artifact_file(27906257, 1399004917, 'output/artifact.jpg')
=> #<Gitlab::FileResponse:46780 {filename: "artifact.jpg"}>

[3] pry(main)> number_only_text_file = client.download_job_artifact_file(27906257, 1399004917, 'output/artifact.numbers.txt')
=> true

[4] pry(main)> string_text_file = client.download_job_artifact_file(27906257, 1399004917, 'output/artifact.txt')
Gitlab::Error::Parsing: The response is not a valid JSON
from /home/balasankarc/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/gitlab-4.17.0/lib/gitlab/request.rb:37:in `rescue in decode'
Caused by JSON::ParserError: 809: unexpected token at 'HelloWorld'
from /home/balasankarc/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:216:in `parse'

[5] pry(main)> invalid_json_file = client.download_job_artifact_file(27906257, 1399004917, 'output/artifact.invalid.json')
Gitlab::Error::Parsing: The response is not a valid JSON
from /home/balasankarc/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/gitlab-4.17.0/lib/gitlab/request.rb:37:in `rescue in decode'
Caused by JSON::ParserError: 809: unexpected token at 'Random String which is not valid JSOn
'
from /home/balasankarc/.asdf/installs/ruby/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.5.1/lib/json/common.rb:216:in `parse'
  1. Valid JSON file worked
  2. JPG file worked
  3. Text file with only numbers returned true because JSON load failed and the || {} gets applied.
  4. Text file with string raised an error as it was attempted to be JSON parsed
  5. Invalid JSON file (which is technically same as a regular text file) raised an error as it was attempted to be JSON parsed
@NARKOZ NARKOZ added bug Something isn't working help wanted Good for newcomers labels Dec 27, 2021
@NARKOZ
Copy link
Owner

NARKOZ commented Dec 27, 2021

Download handles only binary files:

if body.encoding == Encoding::ASCII_8BIT # binary response
::Gitlab::FileResponse.new StringIO.new(body, 'rb+')
else # error with json response
::Gitlab::Request.parse(body)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants