summaryrefslogtreecommitdiff
path: root/app/models/ci/pipeline_artifact.rb
blob: 6d22a875aab64d8c5e6cf9a94e74449d4a48b04a (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true

# This class is being used to persist additional artifacts after a pipeline completes, which is a great place to cache a computed result in object storage

module Ci
  class PipelineArtifact < Ci::ApplicationRecord
    include UpdateProjectStatistics
    include Artifactable
    include FileStoreMounter
    include Lockable
    include Presentable

    FILE_SIZE_LIMIT = 10.megabytes.freeze
    EXPIRATION_DATE = 1.week.freeze

    DEFAULT_FILE_NAMES = {
      code_coverage: 'code_coverage.json',
      code_quality_mr_diff: 'code_quality_mr_diff.json'
    }.freeze

    REPORT_TYPES = {
      code_coverage: :raw,
      code_quality_mr_diff: :raw
    }.freeze

    belongs_to :project, class_name: "Project", inverse_of: :pipeline_artifacts
    belongs_to :pipeline, class_name: "Ci::Pipeline", inverse_of: :pipeline_artifacts

    validates :pipeline, :project, :file_format, :file, presence: true
    validates :file_store, presence: true, inclusion: { in: ObjectStorage::SUPPORTED_STORES }
    validates :size, presence: true, numericality: { less_than_or_equal_to: FILE_SIZE_LIMIT }
    validates :file_type, presence: true

    mount_file_store_uploader Ci::PipelineArtifactUploader

    update_project_statistics project_statistics_name: :pipeline_artifacts_size

    enum file_type: {
      code_coverage: 1,
      code_quality_mr_diff: 2
    }

    scope :unlocked, -> { joins(:pipeline).merge(::Ci::Pipeline.unlocked) }

    class << self
      def report_exists?(file_type)
        return false unless REPORT_TYPES.key?(file_type)

        where(file_type: file_type).exists?
      end

      def find_by_file_type(file_type)
        find_by(file_type: file_type)
      end

      def create_or_replace_for_pipeline!(pipeline:, file_type:, file:, size:, locked: :unknown)
        transaction do
          pipeline.pipeline_artifacts.find_by_file_type(file_type)&.destroy!

          pipeline.pipeline_artifacts.create!(
            file_type: file_type,
            project_id: pipeline.project_id,
            size: size,
            file: file,
            file_format: REPORT_TYPES[file_type],
            expire_at: EXPIRATION_DATE.from_now,
            locked: locked
          )
        end
      rescue ActiveRecord::ActiveRecordError => err
        Gitlab::ErrorTracking.track_and_raise_exception(err, { pipeline_id: pipeline.id, file_type: file_type })
      end
    end

    def present
      super(presenter_class: "Ci::PipelineArtifacts::#{self.file_type.camelize}Presenter".constantize)
    end
  end
end

Ci::PipelineArtifact.prepend_mod