Welcome to mirror list, hosted at ThFree Co, Russian Federation.

coverage_report_service.rb « pipeline_artifacts « ci « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 99877603554c8e56ab2e725da96712435b695d95 (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
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
module Ci
  module PipelineArtifacts
    class CoverageReportService
      include Gitlab::Utils::StrongMemoize

      def initialize(pipeline)
        @pipeline = pipeline
      end

      def execute
        return if report.empty?

        Ci::PipelineArtifact.create_or_replace_for_pipeline!(**pipeline_artifact_params).tap do |pipeline_artifact|
          Gitlab::AppLogger.info(log_params(pipeline_artifact))
        end
      end

      private

      attr_reader :pipeline

      def report
        strong_memoize(:report) do
          Gitlab::Ci::Reports::CoverageReportGenerator.new(pipeline).report
        end
      end

      def pipeline_artifact_params
        attributes = {
          pipeline: pipeline,
          file_type: :code_coverage,
          file: carrierwave_file,
          size: carrierwave_file['tempfile'].size
        }

        if ::Feature.enabled?(:ci_update_unlocked_pipeline_artifacts, pipeline.project)
          attributes[:locked] = pipeline.locked
        end

        attributes
      end

      def carrierwave_file
        strong_memoize(:carrier_wave_file) do
          CarrierWaveStringFile.new_file(
            file_content: report.to_json,
            filename: Ci::PipelineArtifact::DEFAULT_FILE_NAMES.fetch(:code_coverage),
            content_type: 'application/json'
          )
        end
      end

      def log_params(pipeline_artifact)
        {
          project_id: pipeline.project_id,
          pipeline_id: pipeline.id,
          pipeline_artifact_id: pipeline_artifact.id,
          message: "Created code coverage for pipeline."
        }
      end
    end
  end
end