`download_job_artifact_file` method fails when the artifact is a non-JSON text file
balasankarc opened this issue · comments
Balasankar "Balu" C commented
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'
- Valid JSON file worked
- JPG file worked
- Text file with only numbers returned
true
because JSON load failed and the|| {}
gets applied. - Text file with string raised an error as it was attempted to be JSON parsed
- Invalid JSON file (which is technically same as a regular text file) raised an error as it was attempted to be JSON parsed
Nihad Abbasov commented
Download handles only binary files:
gitlab/lib/gitlab/client/jobs.rb
Lines 120 to 124 in 891fc60