Commit cc528b6c authored by Vitali Tatarintev's avatar Vitali Tatarintev

Merge branch 'mo-introduce-code-coverage-presenter' into 'master'

Introduce CodeCoveragePresenter to PipelineArtifact

See merge request gitlab-org/gitlab!40870
parents a7d94cf2 ce8bac4c
...@@ -8,6 +8,7 @@ module Ci ...@@ -8,6 +8,7 @@ module Ci
include UpdateProjectStatistics include UpdateProjectStatistics
include Artifactable include Artifactable
include FileStoreMounter include FileStoreMounter
include Presentable
FILE_STORE_SUPPORTED = [ FILE_STORE_SUPPORTED = [
ObjectStorage::Store::LOCAL, ObjectStorage::Store::LOCAL,
...@@ -44,5 +45,9 @@ module Ci ...@@ -44,5 +45,9 @@ module Ci
def self.find_with_code_coverage def self.find_with_code_coverage
find_by(file_type: :code_coverage) find_by(file_type: :code_coverage)
end end
def present
super(presenter_class: "Ci::PipelineArtifacts::#{self.file_type.camelize}Presenter".constantize)
end
end end
end end
# frozen_string_literal: true
module Ci
module PipelineArtifacts
class CodeCoveragePresenter < ProcessablePresenter
include Gitlab::Utils::StrongMemoize
def for_files(filenames)
coverage_files = raw_report["files"].select { |key| filenames.include?(key) }
{ files: coverage_files }
end
private
def raw_report
strong_memoize(:raw_report) do
self.each_blob do |blob|
Gitlab::Json.parse(blob)
end
end
end
end
end
end
...@@ -12,7 +12,7 @@ module Ci ...@@ -12,7 +12,7 @@ module Ci
{ {
status: :parsed, status: :parsed,
key: key(base_pipeline, head_pipeline), key: key(base_pipeline, head_pipeline),
data: Gitlab::Ci::Pipeline::Artifact::CodeCoverage.new(head_pipeline.pipeline_artifacts.find_with_code_coverage).for_files(merge_request.new_paths) data: head_pipeline.pipeline_artifacts.find_with_code_coverage.present.for_files(merge_request.new_paths)
} }
rescue => e rescue => e
Gitlab::ErrorTracking.track_exception(e, project_id: project.id) Gitlab::ErrorTracking.track_exception(e, project_id: project.id)
......
# frozen_string_literal: true
module Gitlab
module Ci
module Pipeline
module Artifact
class CodeCoverage
include Gitlab::Utils::StrongMemoize
def initialize(pipeline_artifact)
@pipeline_artifact = pipeline_artifact
end
def for_files(filenames)
coverage_files = raw_report["files"].select { |key| filenames.include?(key) }
{ files: coverage_files }
end
private
def raw_report
strong_memoize(:raw_report) do
@pipeline_artifact.each_blob do |blob|
Gitlab::Json.parse(blob)
end
end
end
end
end
end
end
end
...@@ -109,4 +109,14 @@ RSpec.describe Ci::PipelineArtifact, type: :model do ...@@ -109,4 +109,14 @@ RSpec.describe Ci::PipelineArtifact, type: :model do
end end
end end
end end
describe '#present' do
subject { coverage_report.present }
context 'when file_type is code_coverage' do
it 'uses code coverage presenter' do
expect(subject.present).to be_kind_of(Ci::PipelineArtifacts::CodeCoveragePresenter)
end
end
end
end end
...@@ -2,12 +2,13 @@ ...@@ -2,12 +2,13 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::Ci::Pipeline::Artifact::CodeCoverage do RSpec.describe Ci::PipelineArtifacts::CodeCoveragePresenter do
let(:pipeline_artifact) { create(:ci_pipeline_artifact, :with_code_coverage_with_multiple_files) } let(:pipeline_artifact) { create(:ci_pipeline_artifact, :with_code_coverage_with_multiple_files) }
let(:code_coverage) { described_class.new(pipeline_artifact) }
subject(:presenter) { described_class.new(pipeline_artifact) }
describe '#for_files' do describe '#for_files' do
subject { code_coverage.for_files(filenames) } subject { presenter.for_files(filenames) }
context 'when code coverage has data' do context 'when code coverage has data' do
context 'when filenames is empty' do context 'when filenames is empty' do
......
...@@ -16,7 +16,8 @@ RSpec.describe Ci::GenerateCoverageReportsService do ...@@ -16,7 +16,8 @@ RSpec.describe Ci::GenerateCoverageReportsService do
let!(:base_pipeline) { nil } let!(:base_pipeline) { nil }
it 'returns status and data', :aggregate_failures do it 'returns status and data', :aggregate_failures do
expect_next_instance_of(Gitlab::Ci::Pipeline::Artifact::CodeCoverage) do |instance| expect_any_instance_of(Ci::PipelineArtifact) do |instance|
expect(instance).to receive(:present)
expect(instance).to receive(:for_files).with(merge_request.new_paths).and_call_original expect(instance).to receive(:for_files).with(merge_request.new_paths).and_call_original
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment