NARKOZ / gitlab

Ruby wrapper and CLI for the GitLab REST API
https://narkoz.github.io/gitlab
BSD 2-Clause "Simplified" License
1.06k stars 396 forks source link

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

Open balasankarc opened 3 years ago

balasankarc commented 3 years ago

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 commented 2 years ago

Download handles only binary files: https://github.com/NARKOZ/gitlab/blob/891fc6020868ee39eb0f98c144fba3eef82d3f97/lib/gitlab/client/jobs.rb#L120-L124