summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/reports/coverage_report_generator.rb
blob: fd73ed6fd259c54666cbf376fa5618b813547b02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# frozen_string_literal: true

module Gitlab
  module Ci
    module Reports
      class CoverageReportGenerator
        include Gitlab::Utils::StrongMemoize

        def initialize(pipeline)
          @pipeline = pipeline
        end

        def report
          coverage_report = Gitlab::Ci::Reports::CoverageReport.new

          # Return an empty report if the pipeline is a child pipeline.
          # Since the coverage report is used in a merge request report,
          # we are only interested in the coverage report from the root pipeline.
          return coverage_report if @pipeline.child?

          coverage_report.tap do |coverage_report|
            report_builds.find_each do |build|
              build.each_report(::Ci::JobArtifact::COVERAGE_REPORT_FILE_TYPES) do |file_type, blob|
                Gitlab::Ci::Parsers.fabricate!(file_type).parse!(
                  blob,
                  coverage_report,
                  project_path: @pipeline.project.full_path,
                  worktree_paths: @pipeline.all_worktree_paths
                )
              end
            end
          end
        end

        private

        def report_builds
          if child_pipeline_feature_enabled?
            @pipeline.latest_report_builds_in_self_and_descendants(::Ci::JobArtifact.coverage_reports)
          else
            @pipeline.latest_report_builds(::Ci::JobArtifact.coverage_reports)
          end
        end

        def child_pipeline_feature_enabled?
          strong_memoize(:feature_enabled) do
            Feature.enabled?(:ci_child_pipeline_coverage_reports, @pipeline.project)
          end
        end
      end
    end
  end
end