diff options
-rw-r--r-- | app/serializers/build_details_entity.rb | 4 | ||||
-rw-r--r-- | app/serializers/job_artifact_entity.rb | 15 | ||||
-rw-r--r-- | spec/serializers/build_details_entity_spec.rb | 8 | ||||
-rw-r--r-- | spec/serializers/job_artifact_entity_spec.rb | 28 |
4 files changed, 55 insertions, 0 deletions
diff --git a/app/serializers/build_details_entity.rb b/app/serializers/build_details_entity.rb index 6928968edc0..ee5adc69660 100644 --- a/app/serializers/build_details_entity.rb +++ b/app/serializers/build_details_entity.rb @@ -42,6 +42,10 @@ class BuildDetailsEntity < JobEntity end end + expose :reports, if: -> (*) { can?(current_user, :read_build, build) }, using: JobArtifactEntity do |build| + build.job_artifacts.merge(Ci::JobArtifact.with_all_reports) + end + expose :erased_by, if: -> (*) { build.erased? }, using: UserEntity expose :erase_path, if: -> (*) { build.erasable? && can?(current_user, :erase_build, build) } do |build| erase_project_job_path(project, build) diff --git a/app/serializers/job_artifact_entity.rb b/app/serializers/job_artifact_entity.rb new file mode 100644 index 00000000000..76ded287c75 --- /dev/null +++ b/app/serializers/job_artifact_entity.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class JobArtifactEntity < Grape::Entity + include RequestAwareEntity + + expose :file_type + expose :file_format + expose :size + + expose :download_path do |artifact| + download_project_job_artifacts_path(job.project, job, file_type: artifact.file_format) + end + + alias_method :job, :object +end diff --git a/spec/serializers/build_details_entity_spec.rb b/spec/serializers/build_details_entity_spec.rb index 9c2e5c79a9d..0b8ed873bde 100644 --- a/spec/serializers/build_details_entity_spec.rb +++ b/spec/serializers/build_details_entity_spec.rb @@ -146,5 +146,13 @@ describe BuildDetailsEntity do end end end + + context 'when the build has reports' do + let!(:report) { create(:ci_job_artifact, :codequality, job: build) } + + it 'exposes the report artifacts' do + expect(subject[:reports]).not_to be_empty + end + end end end diff --git a/spec/serializers/job_artifact_entity_spec.rb b/spec/serializers/job_artifact_entity_spec.rb new file mode 100644 index 00000000000..4188be56473 --- /dev/null +++ b/spec/serializers/job_artifact_entity_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe JobArtifactEntity do + let(:report) { create(:ci_job_artifact, :codequality) } + let(:entity) { described_class.new(report, request: double) } + + describe '#as_json' do + subject { entity.as_json } + + it 'exposes file_type' do + expect(subject[:file_type]).to eq(report.file_type) + end + + it 'exposes file_format' do + expect(subject[:file_format]).to eq(report.file_format) + end + + it 'exposes size' do + expect(subject[:size]).to eq(report.size) + end + + it 'exposes download path' do + expect(subject[:download_path]).to include("jobs/#{report.job.id}/artifacts/download") + end + end +end |