2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
module Ci
|
|
|
|
class JobArtifact < ActiveRecord::Base
|
2018-05-09 12:01:36 +05:30
|
|
|
include AfterCommitQueue
|
|
|
|
include ObjectStorage::BackgroundMove
|
2018-03-17 18:26:18 +05:30
|
|
|
extend Gitlab::Ci::Model
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
NotSupportedAdapterError = Class.new(StandardError)
|
|
|
|
|
|
|
|
TEST_REPORT_FILE_TYPES = %w[junit].freeze
|
2018-12-05 23:21:45 +05:30
|
|
|
NON_ERASABLE_FILE_TYPES = %w[trace].freeze
|
|
|
|
DEFAULT_FILE_NAMES = {
|
|
|
|
archive: nil,
|
|
|
|
metadata: nil,
|
|
|
|
trace: nil,
|
|
|
|
junit: 'junit.xml',
|
2018-12-13 13:39:08 +05:30
|
|
|
codequality: 'gl-code-quality-report.json',
|
2018-12-05 23:21:45 +05:30
|
|
|
sast: 'gl-sast-report.json',
|
|
|
|
dependency_scanning: 'gl-dependency-scanning-report.json',
|
|
|
|
container_scanning: 'gl-container-scanning-report.json',
|
2018-12-13 13:39:08 +05:30
|
|
|
dast: 'gl-dast-report.json',
|
|
|
|
license_management: 'gl-license-management-report.json',
|
|
|
|
performance: 'performance.json'
|
2018-12-05 23:21:45 +05:30
|
|
|
}.freeze
|
|
|
|
|
|
|
|
TYPE_AND_FORMAT_PAIRS = {
|
|
|
|
archive: :zip,
|
|
|
|
metadata: :gzip,
|
|
|
|
trace: :raw,
|
|
|
|
junit: :gzip,
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
# All these file formats use `raw` as we need to store them uncompressed
|
|
|
|
# for Frontend to fetch the files and do analysis
|
|
|
|
# When they will be only used by backend, they can be `gzipped`.
|
|
|
|
codequality: :raw,
|
|
|
|
sast: :raw,
|
|
|
|
dependency_scanning: :raw,
|
|
|
|
container_scanning: :raw,
|
|
|
|
dast: :raw,
|
|
|
|
license_management: :raw,
|
|
|
|
performance: :raw
|
2018-12-05 23:21:45 +05:30
|
|
|
}.freeze
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
belongs_to :project
|
|
|
|
belongs_to :job, class_name: "Ci::Build", foreign_key: :job_id
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
mount_uploader :file, JobArtifactUploader
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
validates :file_format, presence: true, unless: :trace?, on: :create
|
|
|
|
validate :valid_file_format?, unless: :trace?, on: :create
|
2018-03-17 18:26:18 +05:30
|
|
|
before_save :set_size, if: :file_changed?
|
2018-10-15 14:42:47 +05:30
|
|
|
after_save :update_project_statistics_after_save, if: :size_changed?
|
|
|
|
after_destroy :update_project_statistics_after_destroy, unless: :project_destroyed?
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
after_save :update_file_store, if: :file_changed?
|
|
|
|
|
|
|
|
scope :with_files_stored_locally, -> { where(file_store: [nil, ::JobArtifactUploader::Store::LOCAL]) }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
scope :with_file_types, -> (file_types) do
|
|
|
|
types = self.file_types.select { |file_type| file_types.include?(file_type) }.values
|
|
|
|
|
|
|
|
where(file_type: types)
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
scope :test_reports, -> do
|
2018-12-05 23:21:45 +05:30
|
|
|
with_file_types(TEST_REPORT_FILE_TYPES)
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :erasable, -> do
|
|
|
|
types = self.file_types.reject { |file_type| NON_ERASABLE_FILE_TYPES.include?(file_type) }.values
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
where(file_type: types)
|
|
|
|
end
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
delegate :filename, :exists?, :open, to: :file
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
enum file_type: {
|
|
|
|
archive: 1,
|
|
|
|
metadata: 2,
|
2018-11-18 11:00:15 +05:30
|
|
|
trace: 3,
|
2018-12-05 23:21:45 +05:30
|
|
|
junit: 4,
|
|
|
|
sast: 5, ## EE-specific
|
|
|
|
dependency_scanning: 6, ## EE-specific
|
|
|
|
container_scanning: 7, ## EE-specific
|
|
|
|
dast: 8, ## EE-specific
|
2018-12-13 13:39:08 +05:30
|
|
|
codequality: 9, ## EE-specific
|
|
|
|
license_management: 10, ## EE-specific
|
|
|
|
performance: 11 ## EE-specific
|
2018-11-18 11:00:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
enum file_format: {
|
|
|
|
raw: 1,
|
|
|
|
zip: 2,
|
|
|
|
gzip: 3
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
# `file_location` indicates where actual files are stored.
|
|
|
|
# Ideally, actual files should be stored in the same directory, and use the same
|
|
|
|
# convention to generate its path. However, sometimes we can't do so due to backward-compatibility.
|
|
|
|
#
|
|
|
|
# legacy_path ... The actual file is stored at a path consists of a timestamp
|
|
|
|
# and raw project/model IDs. Those rows were migrated from
|
|
|
|
# `ci_builds.artifacts_file` and `ci_builds.artifacts_metadata`
|
|
|
|
# hashed_path ... The actual file is stored at a path consists of a SHA2 based on the project ID.
|
|
|
|
# This is the default value.
|
|
|
|
enum file_location: {
|
|
|
|
legacy_path: 1,
|
|
|
|
hashed_path: 2
|
|
|
|
}
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
FILE_FORMAT_ADAPTERS = {
|
2018-12-13 13:39:08 +05:30
|
|
|
gzip: Gitlab::Ci::Build::Artifacts::Adapters::GzipStream,
|
|
|
|
raw: Gitlab::Ci::Build::Artifacts::Adapters::RawStream
|
2018-11-18 11:00:15 +05:30
|
|
|
}.freeze
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def update_file_store
|
|
|
|
# The file.object_store is set during `uploader.store!`
|
|
|
|
# which happens after object is inserted/updated
|
|
|
|
self.update_column(:file_store, file.object_store)
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def self.artifacts_size_for(project)
|
|
|
|
self.where(project: project).sum(:size)
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def local_store?
|
|
|
|
[nil, ::JobArtifactUploader::Store::LOCAL].include?(self.file_store)
|
|
|
|
end
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
def hashed_path?
|
|
|
|
return true if trace? # ArchiveLegacyTraces background migration might not have `file_location` column
|
|
|
|
|
|
|
|
super || self.file_location.nil?
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
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
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
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
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
private
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def file_format_adapter_class
|
|
|
|
FILE_FORMAT_ADAPTERS[file_format.to_sym]
|
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
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
|
2018-11-20 20:47:30 +05:30
|
|
|
update_project_statistics(-self.size.to_i)
|
2018-10-15 14:42:47 +05:30
|
|
|
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
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|