debian-mirror-gitlab/app/models/ci/job_artifact.rb

406 lines
12 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module Ci
2021-10-27 15:23:28 +05:30
class JobArtifact < Ci::ApplicationRecord
2022-10-11 01:57:18 +05:30
include Ci::Partitionable
2022-01-26 12:08:38 +05:30
include IgnorableColumns
2018-05-09 12:01:36 +05:30
include AfterCommitQueue
2019-07-31 22:56:46 +05:30
include UpdateProjectStatistics
2020-06-23 00:09:42 +05:30
include UsageStatistics
2019-12-21 20:55:43 +05:30
include Sortable
2020-10-24 23:57:45 +05:30
include Artifactable
2022-10-11 01:57:18 +05:30
include Lockable
2020-10-24 23:57:45 +05:30
include FileStoreMounter
2021-03-08 18:12:59 +05:30
include EachBatch
2021-12-11 22:18:48 +05:30
include Gitlab::Utils::StrongMemoize
2018-03-17 18:26:18 +05:30
2023-06-20 00:43:36 +05:30
# NOTE: Temporarily ignore. This will will be used in https://gitlab.com/gitlab-org/gitlab/-/merge_requests/106740
ignore_column :file_final_path, remove_with: '16.1', remove_after: '2023-05-23'
2023-03-17 16:20:25 +05:30
enum accessibility: { public: 0, private: 1 }, _suffix: true
2018-12-05 23:21:45 +05:30
NON_ERASABLE_FILE_TYPES = %w[trace].freeze
2022-08-27 11:52:29 +05:30
REPORT_FILE_TYPES = {
sast: %w[sast],
secret_detection: %w[secret_detection],
test: %w[junit],
accessibility: %w[accessibility],
coverage: %w[cobertura],
codequality: %w[codequality],
2022-10-11 01:57:18 +05:30
terraform: %w[terraform]
2022-08-27 11:52:29 +05:30
}.freeze
2018-12-05 23:21:45 +05:30
DEFAULT_FILE_NAMES = {
archive: nil,
metadata: nil,
trace: nil,
2020-03-13 15:44:24 +05:30
metrics_referee: nil,
network_referee: nil,
2018-12-05 23:21:45 +05:30
junit: 'junit.xml',
2020-05-24 23:13:21 +05:30
accessibility: 'gl-accessibility.json',
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',
2020-06-23 00:09:42 +05:30
secret_detection: 'gl-secret-detection-report.json',
2018-12-05 23:21:45 +05:30
dependency_scanning: 'gl-dependency-scanning-report.json',
container_scanning: 'gl-container-scanning-report.json',
2021-09-30 23:02:18 +05:30
cluster_image_scanning: 'gl-cluster-image-scanning-report.json',
2018-12-13 13:39:08 +05:30
dast: 'gl-dast-report.json',
2020-03-13 15:44:24 +05:30
license_scanning: 'gl-license-scanning-report.json',
2019-07-07 11:18:12 +05:30
performance: 'performance.json',
2020-07-28 23:09:34 +05:30
browser_performance: 'browser-performance.json',
load_performance: 'load-performance.json',
2020-03-13 15:44:24 +05:30
metrics: 'metrics.txt',
2020-04-08 14:13:33 +05:30
lsif: 'lsif.json',
dotenv: '.env',
2020-04-22 19:07:51 +05:30
cobertura: 'cobertura-coverage.xml',
2020-05-24 23:13:21 +05:30
terraform: 'tfplan.json',
2022-07-16 23:28:13 +05:30
cluster_applications: 'gl-cluster-applications.json', # DEPRECATED: https://gitlab.com/gitlab-org/gitlab/-/issues/361094
2023-03-04 22:38:38 +05:30
requirements: 'requirements.json', # Will be DEPRECATED soon: https://gitlab.com/groups/gitlab-org/-/epics/9203
requirements_v2: 'requirements_v2.json',
2021-01-03 14:25:43 +05:30
coverage_fuzzing: 'gl-coverage-fuzzing.json',
2022-08-27 11:52:29 +05:30
api_fuzzing: 'gl-api-fuzzing-report.json',
2022-10-11 01:57:18 +05:30
cyclonedx: 'gl-sbom.cdx.json'
2018-12-05 23:21:45 +05:30
}.freeze
2019-09-04 21:01:54 +05:30
INTERNAL_TYPES = {
2018-12-05 23:21:45 +05:30
archive: :zip,
metadata: :gzip,
2019-09-04 21:01:54 +05:30
trace: :raw
}.freeze
REPORT_TYPES = {
2018-12-05 23:21:45 +05:30
junit: :gzip,
2019-07-07 11:18:12 +05:30
metrics: :gzip,
2020-03-13 15:44:24 +05:30
metrics_referee: :gzip,
network_referee: :gzip,
2020-04-08 14:13:33 +05:30
dotenv: :gzip,
cobertura: :gzip,
2022-07-16 23:28:13 +05:30
cluster_applications: :gzip, # DEPRECATED: https://gitlab.com/gitlab-org/gitlab/-/issues/361094
2020-05-24 23:13:21 +05:30
lsif: :zip,
2022-10-11 01:57:18 +05:30
cyclonedx: :gzip,
2018-12-13 13:39:08 +05:30
2021-01-03 14:25:43 +05:30
# Security reports and license scanning reports are raw artifacts
# because they used to be fetched by the frontend, but this is not the case anymore.
2018-12-13 13:39:08 +05:30
sast: :raw,
2020-06-23 00:09:42 +05:30
secret_detection: :raw,
2018-12-13 13:39:08 +05:30
dependency_scanning: :raw,
container_scanning: :raw,
2021-09-30 23:02:18 +05:30
cluster_image_scanning: :raw,
2018-12-13 13:39:08 +05:30
dast: :raw,
2020-03-13 15:44:24 +05:30
license_scanning: :raw,
2021-01-03 14:25:43 +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`.
accessibility: :raw,
codequality: :raw,
2020-04-22 19:07:51 +05:30
performance: :raw,
2020-07-28 23:09:34 +05:30
browser_performance: :raw,
load_performance: :raw,
2020-06-23 00:09:42 +05:30
terraform: :raw,
2020-07-28 23:09:34 +05:30
requirements: :raw,
2023-03-04 22:38:38 +05:30
requirements_v2: :raw,
2021-01-03 14:25:43 +05:30
coverage_fuzzing: :raw,
2022-10-11 01:57:18 +05:30
api_fuzzing: :raw
2018-12-05 23:21:45 +05:30
}.freeze
2018-11-18 11:00:15 +05:30
2020-05-24 23:13:21 +05:30
DOWNLOADABLE_TYPES = %w[
accessibility
2021-01-03 14:25:43 +05:30
api_fuzzing
2020-05-24 23:13:21 +05:30
archive
cobertura
codequality
container_scanning
dast
dependency_scanning
dotenv
junit
license_scanning
lsif
metrics
performance
2020-07-28 23:09:34 +05:30
browser_performance
load_performance
2020-05-24 23:13:21 +05:30
sast
2020-06-23 00:09:42 +05:30
secret_detection
requirements
2023-03-04 22:38:38 +05:30
requirements_v2
2021-09-30 23:02:18 +05:30
cluster_image_scanning
2022-08-27 11:52:29 +05:30
cyclonedx
2020-05-24 23:13:21 +05:30
].freeze
2019-09-04 21:01:54 +05:30
TYPE_AND_FORMAT_PAIRS = INTERNAL_TYPES.merge(REPORT_TYPES).freeze
2020-07-28 23:09:34 +05:30
PLAN_LIMIT_PREFIX = 'ci_max_artifact_size_'
2020-05-24 23:13:21 +05:30
2018-03-17 18:26:18 +05:30
belongs_to :project
2023-05-27 22:25:52 +05:30
belongs_to :job, class_name: "Ci::Build", foreign_key: :job_id, inverse_of: :job_artifacts
2018-03-17 18:26:18 +05:30
2022-07-23 23:45:48 +05:30
mount_file_store_uploader JobArtifactUploader, skip_store_file: true
2018-05-09 12:01:36 +05:30
2022-10-11 01:57:18 +05:30
before_save :set_size, if: :file_changed?
2022-07-23 23:45:48 +05:30
after_save :store_file_in_transaction!, unless: :store_after_commit?
2023-04-23 21:23:45 +05:30
after_create_commit :log_create
2022-07-23 23:45:48 +05:30
after_commit :store_file_after_transaction!, on: [:create, :update], if: :store_after_commit?
2021-12-11 22:18:48 +05:30
2023-04-23 21:23:45 +05:30
after_destroy_commit :log_destroy
2022-10-11 01:57:18 +05:30
validates :job, presence: true
2018-11-18 11:00:15 +05:30
validates :file_format, presence: true, unless: :trace?, on: :create
2020-05-24 23:13:21 +05:30
validate :validate_file_format!, unless: :trace?, on: :create
2018-03-17 18:26:18 +05:30
2019-09-04 21:01:54 +05:30
update_project_statistics project_statistics_name: :build_artifacts_size
2022-10-11 01:57:18 +05:30
partitionable scope: :job
2019-07-31 22:56:46 +05:30
2020-06-23 00:09:42 +05:30
scope :not_expired, -> { where('expire_at IS NULL OR expire_at > ?', Time.current) }
2020-04-08 14:13:33 +05:30
scope :for_sha, ->(sha, project_id) { joins(job: :pipeline).where(ci_pipelines: { sha: sha, project_id: project_id }) }
2022-01-26 12:08:38 +05:30
scope :for_job_ids, ->(job_ids) { where(job_id: job_ids) }
2020-04-22 19:07:51 +05:30
scope :for_job_name, ->(name) { joins(:job).where(ci_builds: { name: name }) }
2022-07-23 23:45:48 +05:30
scope :created_at_before, ->(time) { where(arel_table[:created_at].lteq(time)) }
scope :id_before, ->(id) { where(arel_table[:id].lteq(id)) }
scope :id_after, ->(id) { where(arel_table[:id].gt(id)) }
scope :ordered_by_id, -> { order(:id) }
2018-03-17 18:26:18 +05:30
2021-04-17 20:07:23 +05:30
scope :with_job, -> { joins(:job).includes(:job) }
2021-03-08 18:12:59 +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
2022-08-27 11:52:29 +05:30
scope :all_reports, -> do
with_file_types(REPORT_TYPES.keys.map(&:to_s))
2020-05-24 23:13:21 +05:30
end
2018-12-05 23:21:45 +05:30
scope :erasable, -> do
2022-03-02 08:16:31 +05:30
where(file_type: self.erasable_file_types)
2018-11-18 11:00:15 +05:30
end
2023-05-27 22:25:52 +05:30
scope :non_trace, -> { where.not(file_type: [:trace]) }
2020-06-23 00:09:42 +05:30
scope :downloadable, -> { where(file_type: DOWNLOADABLE_TYPES) }
2021-03-08 18:12:59 +05:30
scope :unlocked, -> { joins(job: :pipeline).merge(::Ci::Pipeline.unlocked) }
2022-06-21 17:19:12 +05:30
scope :order_expired_asc, -> { order(expire_at: :asc) }
2022-07-23 23:45:48 +05:30
scope :with_destroy_preloads, -> { includes(project: [:route, :statistics, :build_artifacts_size_refresh]) }
2019-03-02 22:35:43 +05:30
2021-09-30 23:02:18 +05:30
scope :for_project, ->(project) { where(project_id: project) }
scope :created_in_time_range, ->(from: nil, to: nil) { where(created_at: from..to) }
2019-12-04 20:38:33 +05:30
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
2021-09-04 01:27:46 +05:30
license_scanning: 101, ## EE-specific
2020-07-28 23:09:34 +05:30
performance: 11, ## EE-specific till 13.2
2020-03-13 15:44:24 +05:30
metrics: 12, ## EE-specific
metrics_referee: 13, ## runner referees
network_referee: 14, ## runner referees
2020-04-08 14:13:33 +05:30
lsif: 15, # LSIF data for code navigation
dotenv: 16,
2020-04-22 19:07:51 +05:30
cobertura: 17,
2020-05-24 23:13:21 +05:30
terraform: 18, # Transformed json
accessibility: 19,
2020-06-23 00:09:42 +05:30
cluster_applications: 20,
secret_detection: 21, ## EE-specific
2020-07-28 23:09:34 +05:30
requirements: 22, ## EE-specific
coverage_fuzzing: 23, ## EE-specific
browser_performance: 24, ## EE-specific
2021-01-03 14:25:43 +05:30
load_performance: 25, ## EE-specific
2021-09-30 23:02:18 +05:30
api_fuzzing: 26, ## EE-specific
2022-08-27 11:52:29 +05:30
cluster_image_scanning: 27, ## EE-specific
2023-03-04 22:38:38 +05:30
cyclonedx: 28, ## EE-specific
requirements_v2: 29 ## EE-specific
2018-11-18 11:00:15 +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
}
2020-05-24 23:13:21 +05:30
def validate_file_format!
2018-11-18 11:00:15 +05:30
unless TYPE_AND_FORMAT_PAIRS[self.file_type&.to_sym] == self.file_format&.to_sym
2020-04-08 14:13:33 +05:30
errors.add(:base, _('Invalid file format with specified file type'))
2018-11-18 11:00:15 +05:30
end
end
2022-10-11 01:57:18 +05:30
def self.of_report_type(report_type)
file_types = file_types_for_report(report_type)
with_file_types(file_types)
end
2022-08-27 11:52:29 +05:30
def self.file_types_for_report(report_type)
2022-10-11 01:57:18 +05:30
REPORT_FILE_TYPES.fetch(report_type) { raise ArgumentError, "Unrecognized report type: #{report_type}" }
2022-08-27 11:52:29 +05:30
end
2020-07-28 23:09:34 +05:30
def self.associated_file_types_for(file_type)
return unless file_types.include?(file_type)
[file_type]
end
2022-03-02 08:16:31 +05:30
def self.erasable_file_types
self.file_types.keys - NON_ERASABLE_FILE_TYPES
end
2019-12-21 20:55:43 +05:30
def self.total_size
self.sum(:size)
end
2018-03-17 18:26:18 +05:30
def self.artifacts_size_for(project)
self.where(project: project).sum(:size)
end
2022-06-21 17:19:12 +05:30
def self.pluck_job_id
pluck(:job_id)
end
2021-06-08 01:23:25 +05:30
##
# FastDestroyAll concerns
# rubocop: disable CodeReuse/ServiceClass
def self.begin_fast_destroy
service = ::Ci::JobArtifacts::DestroyAssociationsService.new(self)
service.destroy_records
service
end
# rubocop: enable CodeReuse/ServiceClass
##
# FastDestroyAll concerns
def self.finalize_fast_destroy(service)
service.update_statistics
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
2020-06-23 00:09:42 +05:30
def expired?
expire_at.present? && expire_at < Time.current
end
def expiring?
expire_at.present? && expire_at > Time.current
end
2018-03-17 18:26:18 +05:30
def expire_in
2020-06-23 00:09:42 +05:30
expire_at - Time.current if expire_at
2018-03-17 18:26:18 +05:30
end
def expire_in=(value)
self.expire_at =
if value
2022-08-13 15:12:31 +05:30
::Gitlab::Ci::Build::DurationParser.new(value).seconds_from_now
2018-03-17 18:26:18 +05:30
end
end
2018-10-15 14:42:47 +05:30
2022-07-16 23:28:13 +05:30
def stored?
2021-04-29 21:17:54 +05:30
file&.file&.exists?
end
2019-10-12 21:52:04 +05:30
def self.archived_trace_exists_for?(job_id)
2022-07-16 23:28:13 +05:30
where(job_id: job_id).trace.take&.stored?
2019-10-12 21:52:04 +05:30
end
2020-07-28 23:09:34 +05:30
def self.max_artifact_size(type:, project:)
2020-10-24 23:57:45 +05:30
limit_name = "#{PLAN_LIMIT_PREFIX}#{type}"
max_size = project.actual_limits.limit_for(
limit_name,
alternate_limit: -> { project.closest_setting(:max_artifacts_size) }
)
2020-07-28 23:09:34 +05:30
max_size&.megabytes.to_i
end
2021-04-17 20:07:23 +05:30
def to_deleted_object_attrs(pick_up_at = nil)
2021-01-03 14:25:43 +05:30
{
file_store: file_store,
store_dir: file.store_dir.to_s,
file: file_identifier,
2021-04-17 20:07:23 +05:30
pick_up_at: pick_up_at || expire_at || Time.current
2021-01-03 14:25:43 +05:30
}
end
2021-12-11 22:18:48 +05:30
def store_after_commit?
strong_memoize(:store_after_commit) do
2022-03-02 08:16:31 +05:30
trace? && JobArtifactUploader.direct_upload_enabled?
2021-12-11 22:18:48 +05:30
end
end
2023-03-17 16:20:25 +05:30
def public_access?
return true unless Feature.enabled?(:non_public_artifacts, type: :development)
public_accessibility?
end
2018-10-15 14:42:47 +05:30
private
2022-07-23 23:45:48 +05:30
def store_file_in_transaction!
store_file_now! if saved_change_to_file?
2021-12-11 22:18:48 +05:30
2022-07-23 23:45:48 +05:30
file_stored_in_transaction_hooks
end
def store_file_after_transaction!
store_file_now! if previous_changes.key?(:file)
file_stored_after_transaction_hooks
end
# method overriden in EE
def file_stored_after_transaction_hooks
end
# method overriden in EE
def file_stored_in_transaction_hooks
2021-12-11 22:18:48 +05:30
end
2018-10-15 14:42:47 +05:30
def set_size
self.size = file.size
end
def project_destroyed?
# Use job.project to avoid extra DB query for project
job.project.pending_delete?
end
2023-04-23 21:23:45 +05:30
def log_create
Gitlab::Ci::Artifacts::Logger.log_created(self)
end
def log_destroy
Gitlab::Ci::Artifacts::Logger.log_deleted(self, __method__)
end
2018-03-17 18:26:18 +05:30
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Ci::JobArtifact.prepend_mod_with('Ci::JobArtifact')