Commit ce8bac4c authored by Maxime Orefice's avatar Maxime Orefice Committed by Vitali Tatarintev

Introduce CodeCoveragePresenter to PipelineArtifact

This MR introduces a new presenter class which will be used
to present data code coverage data to gitlab frontend. This
new architecture will allow us to scale to more file_type
easily in the near future.
parent facdfab1
......@@ -8,6 +8,7 @@ module Ci
include UpdateProjectStatistics
include Artifactable
include FileStoreMounter
include Presentable
FILE_STORE_SUPPORTED = [
ObjectStorage::Store::LOCAL,
......@@ -44,5 +45,9 @@ module Ci
def self.find_with_code_coverage
find_by(file_type: :code_coverage)
end
def present
super(presenter_class: "Ci::PipelineArtifacts::#{self.file_type.camelize}Presenter".constantize)
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
{
status: :parsed,
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
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
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
......@@ -2,12 +2,13 @@
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(:code_coverage) { described_class.new(pipeline_artifact) }
subject(:presenter) { described_class.new(pipeline_artifact) }
describe '#for_files' do
subject { code_coverage.for_files(filenames) }
subject { presenter.for_files(filenames) }
context 'when code coverage has data' do
context 'when filenames is empty' do
......
......@@ -16,7 +16,8 @@ RSpec.describe Ci::GenerateCoverageReportsService do
let!(:base_pipeline) { nil }
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
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