job_artifact.rb 3.27 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
module Ci
  class JobArtifact < ActiveRecord::Base
5
    include AfterCommitQueue
6
    include ObjectStorage::BackgroundMove
7
8
    extend Gitlab::Ci::Model

9
10
    NotSupportedAdapterError = Class.new(StandardError)

11
12
13
14
    TEST_REPORT_FILE_TYPES = %w[junit].freeze
    DEFAULT_FILE_NAMES = { junit: 'junit.xml' }.freeze
    TYPE_AND_FORMAT_PAIRS = { archive: :zip, metadata: :gzip, trace: :raw, junit: :gzip }.freeze

15
    belongs_to :project
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
16
    belongs_to :job, class_name: "Ci::Build", foreign_key: :job_id
17

18
19
    mount_uploader :file, JobArtifactUploader

20
21
    validates :file_format, presence: true, unless: :trace?, on: :create
    validate :valid_file_format?, unless: :trace?, on: :create
22
    before_save :set_size, if: :file_changed?
23
24
    after_save :update_project_statistics_after_save, if: :size_changed?
    after_destroy :update_project_statistics_after_destroy, unless: :project_destroyed?
25

26
    after_save :update_file_store, if: :file_changed?
27

28
    scope :with_files_stored_locally, -> { where(file_store: [nil, ::JobArtifactUploader::Store::LOCAL]) }
29

30
31
32
33
34
35
    scope :test_reports, -> do
      types = self.file_types.select { |file_type| TEST_REPORT_FILE_TYPES.include?(file_type) }.values

      where(file_type: types)
    end

36
    delegate :filename, :exists?, :open, to: :file
37

38
39
    enum file_type: {
      archive: 1,
40
      metadata: 2,
41
42
      trace: 3,
      junit: 4
43
44
    }

45
46
47
48
49
50
    enum file_format: {
      raw: 1,
      zip: 2,
      gzip: 3
    }

51
52
53
54
    FILE_FORMAT_ADAPTERS = {
      gzip: Gitlab::Ci::Build::Artifacts::GzipFileAdapter
    }.freeze

55
56
57
58
59
60
    def valid_file_format?
      unless TYPE_AND_FORMAT_PAIRS[self.file_type&.to_sym] == self.file_format&.to_sym
        errors.add(:file_format, 'Invalid file format with specified file type')
      end
    end

61
    def update_file_store
62
63
64
      # The file.object_store is set during `uploader.store!`
      # which happens after object is inserted/updated
      self.update_column(:file_store, file.object_store)
65
66
    end

67
68
69
70
    def self.artifacts_size_for(project)
      self.where(project: project).sum(:size)
    end

71
72
73
74
    def local_store?
      [nil, ::JobArtifactUploader::Store::LOCAL].include?(self.file_store)
    end

75
76
77
78
79
80
81
82
83
84
    def expire_in
      expire_at - Time.now if expire_at
    end

    def expire_in=(value)
      self.expire_at =
        if value
          ChronicDuration.parse(value)&.seconds&.from_now
        end
    end
85

86
87
88
89
90
91
92
93
94
95
    def each_blob(&blk)
      unless file_format_adapter_class
        raise NotSupportedAdapterError, 'This file format requires a dedicated adapter'
      end

      file.open do |stream|
        file_format_adapter_class.new(stream).each_blob(&blk)
      end
    end

96
97
    private

98
99
100
101
    def file_format_adapter_class
      FILE_FORMAT_ADAPTERS[file_format.to_sym]
    end

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    def set_size
      self.size = file.size
    end

    def update_project_statistics_after_save
      update_project_statistics(size.to_i - size_was.to_i)
    end

    def update_project_statistics_after_destroy
      update_project_statistics(-self.size)
    end

    def update_project_statistics(difference)
      ProjectStatistics.increment_statistic(project_id, :build_artifacts_size, difference)
    end

    def project_destroyed?
      # Use job.project to avoid extra DB query for project
      job.project.pending_delete?
    end
122
123
  end
end