Class: Ci::JobArtifact

Constant Summary collapse

TEST_REPORT_FILE_TYPES =
%w[junit].freeze
COVERAGE_REPORT_FILE_TYPES =
%w[cobertura].freeze
CODEQUALITY_REPORT_FILE_TYPES =
%w[codequality].freeze
ACCESSIBILITY_REPORT_FILE_TYPES =
%w[accessibility].freeze
NON_ERASABLE_FILE_TYPES =
%w[trace].freeze
TERRAFORM_REPORT_FILE_TYPES =
%w[terraform].freeze
SAST_REPORT_TYPES =
%w[sast].freeze
SECRET_DETECTION_REPORT_TYPES =
%w[secret_detection].freeze
DEFAULT_FILE_NAMES =
{
  archive: nil,
  metadata: nil,
  trace: nil,
  metrics_referee: nil,
  network_referee: nil,
  junit: 'junit.xml',
  accessibility: 'gl-accessibility.json',
  codequality: 'gl-code-quality-report.json',
  sast: 'gl-sast-report.json',
  secret_detection: 'gl-secret-detection-report.json',
  dependency_scanning: 'gl-dependency-scanning-report.json',
  container_scanning: 'gl-container-scanning-report.json',
  cluster_image_scanning: 'gl-cluster-image-scanning-report.json',
  dast: 'gl-dast-report.json',
  license_scanning: 'gl-license-scanning-report.json',
  performance: 'performance.json',
  browser_performance: 'browser-performance.json',
  load_performance: 'load-performance.json',
  metrics: 'metrics.txt',
  lsif: 'lsif.json',
  dotenv: '.env',
  cobertura: 'cobertura-coverage.xml',
  terraform: 'tfplan.json',
  cluster_applications: 'gl-cluster-applications.json', # DEPRECATED: https://gitlab.com/gitlab-org/gitlab/-/issues/361094
  requirements: 'requirements.json',
  coverage_fuzzing: 'gl-coverage-fuzzing.json',
  api_fuzzing: 'gl-api-fuzzing-report.json'
}.freeze
INTERNAL_TYPES =
{
  archive: :zip,
  metadata: :gzip,
  trace: :raw
}.freeze
REPORT_TYPES =
{
  junit: :gzip,
  metrics: :gzip,
  metrics_referee: :gzip,
  network_referee: :gzip,
  dotenv: :gzip,
  cobertura: :gzip,
  cluster_applications: :gzip, # DEPRECATED: https://gitlab.com/gitlab-org/gitlab/-/issues/361094
  lsif: :zip,

  # 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.
  sast: :raw,
  secret_detection: :raw,
  dependency_scanning: :raw,
  container_scanning: :raw,
  cluster_image_scanning: :raw,
  dast: :raw,
  license_scanning: :raw,

  # 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,
  performance: :raw,
  browser_performance: :raw,
  load_performance: :raw,
  terraform: :raw,
  requirements: :raw,
  coverage_fuzzing: :raw,
  api_fuzzing: :raw
}.freeze
DOWNLOADABLE_TYPES =
%w[
  accessibility
  api_fuzzing
  archive
  cobertura
  codequality
  container_scanning
  dast
  dependency_scanning
  dotenv
  junit
  license_scanning
  lsif
  metrics
  performance
  browser_performance
  load_performance
  sast
  secret_detection
  requirements
  cluster_image_scanning
].freeze
TYPE_AND_FORMAT_PAIRS =
INTERNAL_TYPES.merge(REPORT_TYPES).freeze
PLAN_LIMIT_PREFIX =
'ci_max_artifact_size_'

Constants included from Artifactable

Artifactable::FILE_FORMAT_ADAPTERS, Artifactable::NotSupportedAdapterError, Artifactable::STORE_COLUMN

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from FileStoreMounter

#update_file_store

Methods included from Artifactable

#each_blob

Methods included from AfterCommitQueue

#run_after_commit, #run_after_commit_or_now

Methods included from ObjectStorage::BackgroundMove

#background_upload, #changed_mounts

Methods inherited from ApplicationRecord

model_name, table_name_prefix

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.archived_trace_exists_for?(job_id) ⇒ Boolean

Returns:

  • (Boolean)

330
331
332
# File 'app/models/ci/job_artifact.rb', line 330

def self.archived_trace_exists_for?(job_id)
  where(job_id: job_id).trace.take&.archived_trace_exists?
end

.artifacts_size_for(project) ⇒ Object


273
274
275
# File 'app/models/ci/job_artifact.rb', line 273

def self.artifacts_size_for(project)
  self.where(project: project).sum(:size)
end

.associated_file_types_for(file_type) ⇒ Object


259
260
261
262
263
# File 'app/models/ci/job_artifact.rb', line 259

def self.associated_file_types_for(file_type)
  return unless file_types.include?(file_type)

  [file_type]
end

.begin_fast_destroyObject

FastDestroyAll concerns rubocop: disable CodeReuse/ServiceClass


284
285
286
287
288
# File 'app/models/ci/job_artifact.rb', line 284

def self.begin_fast_destroy
  service = ::Ci::JobArtifacts::DestroyAssociationsService.new(self)
  service.destroy_records
  service
end

.erasable_file_typesObject


265
266
267
# File 'app/models/ci/job_artifact.rb', line 265

def self.erasable_file_types
  self.file_types.keys - NON_ERASABLE_FILE_TYPES
end

.finalize_fast_destroy(service) ⇒ Object

FastDestroyAll concerns


293
294
295
# File 'app/models/ci/job_artifact.rb', line 293

def self.finalize_fast_destroy(service)
  service.update_statistics
end

.max_artifact_size(type:, project:) ⇒ Object


334
335
336
337
338
339
340
341
342
343
# File 'app/models/ci/job_artifact.rb', line 334

def self.max_artifact_size(type:, project:)
  limit_name = "#{PLAN_LIMIT_PREFIX}#{type}"

  max_size = project.actual_limits.limit_for(
    limit_name,
    alternate_limit: -> { project.closest_setting(:max_artifacts_size) }
  )

  max_size&.megabytes.to_i
end

.pluck_job_idObject


277
278
279
# File 'app/models/ci/job_artifact.rb', line 277

def self.pluck_job_id
  pluck(:job_id)
end

.total_sizeObject


269
270
271
# File 'app/models/ci/job_artifact.rb', line 269

def self.total_size
  self.sum(:size)
end

Instance Method Details

#archived_trace_exists?Boolean

Returns:

  • (Boolean)

326
327
328
# File 'app/models/ci/job_artifact.rb', line 326

def archived_trace_exists?
  file&.file&.exists?
end

#expire_inObject


315
316
317
# File 'app/models/ci/job_artifact.rb', line 315

def expire_in
  expire_at - Time.current if expire_at
end

#expire_in=(value) ⇒ Object


319
320
321
322
323
324
# File 'app/models/ci/job_artifact.rb', line 319

def expire_in=(value)
  self.expire_at =
    if value
      ::Gitlab::Ci::Build::Artifacts::ExpireInParser.new(value).seconds_from_now
    end
end

#expired?Boolean

Returns:

  • (Boolean)

307
308
309
# File 'app/models/ci/job_artifact.rb', line 307

def expired?
  expire_at.present? && expire_at < Time.current
end

#expiring?Boolean

Returns:

  • (Boolean)

311
312
313
# File 'app/models/ci/job_artifact.rb', line 311

def expiring?
  expire_at.present? && expire_at > Time.current
end

#hashed_path?Boolean

Returns:

  • (Boolean)

301
302
303
304
305
# File 'app/models/ci/job_artifact.rb', line 301

def hashed_path?
  return true if trace? # ArchiveLegacyTraces background migration might not have `file_location` column

  super || self.file_location.nil?
end

#local_store?Boolean

Returns:

  • (Boolean)

297
298
299
# File 'app/models/ci/job_artifact.rb', line 297

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

#store_after_commit?Boolean

Returns:

  • (Boolean)

354
355
356
357
358
# File 'app/models/ci/job_artifact.rb', line 354

def store_after_commit?
  strong_memoize(:store_after_commit) do
    trace? && JobArtifactUploader.direct_upload_enabled?
  end
end

#to_deleted_object_attrs(pick_up_at = nil) ⇒ Object


345
346
347
348
349
350
351
352
# File 'app/models/ci/job_artifact.rb', line 345

def to_deleted_object_attrs(pick_up_at = nil)
  {
    file_store: file_store,
    store_dir: file.store_dir.to_s,
    file: file_identifier,
    pick_up_at: pick_up_at || expire_at || Time.current
  }
end

#validate_file_format!Object


253
254
255
256
257
# File 'app/models/ci/job_artifact.rb', line 253

def validate_file_format!
  unless TYPE_AND_FORMAT_PAIRS[self.file_type&.to_sym] == self.file_format&.to_sym
    errors.add(:base, _('Invalid file format with specified file type'))
  end
end