NARKOZ / gitlab

Ruby wrapper and CLI for the GitLab REST API

Home Page:https://narkoz.github.io/gitlab

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

balasankarc opened this issue · comments

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

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