Class: Ci::JobArtifact

Inherits:
ApplicationRecord show all
Extended by:
Gitlab::Ci::Model
Includes:
AfterCommitQueue, Artifactable, FileStoreMounter, ObjectStorage::BackgroundMove, Sortable, UpdateProjectStatistics, UsageStatistics
Defined in:
app/models/ci/job_artifact.rb

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
UNSUPPORTED_FILE_TYPES =
%i[license_management].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',
  dast: 'gl-dast-report.json',
  license_management: 'gl-license-management-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',
  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,
  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,
  dast: :raw,
  license_management: :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_management
  license_scanning
  lsif
  metrics
  performance
  browser_performance
  load_performance
  sast
  secret_detection
  requirements
].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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Ci::Model

model_name, table_name_prefix

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

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, where_exists, with_fast_statement_timeout, without_order

Class Method Details

.archived_trace_exists_for?(job_id) ⇒ Boolean

Returns:

  • (Boolean)

281
282
283
# File 'app/models/ci/job_artifact.rb', line 281

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

.artifacts_size_for(project) ⇒ Object


248
249
250
# File 'app/models/ci/job_artifact.rb', line 248

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

.associated_file_types_for(file_type) ⇒ Object


238
239
240
241
242
# File 'app/models/ci/job_artifact.rb', line 238

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

  [file_type]
end

.max_artifact_size(type:, project:) ⇒ Object


285
286
287
288
289
290
291
292
293
294
# File 'app/models/ci/job_artifact.rb', line 285

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

.total_sizeObject


244
245
246
# File 'app/models/ci/job_artifact.rb', line 244

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

Instance Method Details

#expire_inObject


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

def expire_in
  expire_at - Time.current if expire_at
end

#expire_in=(value) ⇒ Object


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

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)

262
263
264
# File 'app/models/ci/job_artifact.rb', line 262

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

#expiring?Boolean

Returns:

  • (Boolean)

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

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

#hashed_path?Boolean

Returns:

  • (Boolean)

256
257
258
259
260
# File 'app/models/ci/job_artifact.rb', line 256

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)

252
253
254
# File 'app/models/ci/job_artifact.rb', line 252

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

#to_deleted_object_attrsObject


296
297
298
299
300
301
302
303
# File 'app/models/ci/job_artifact.rb', line 296

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

#validate_file_format!Object


232
233
234
235
236
# File 'app/models/ci/job_artifact.rb', line 232

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

#validate_supported_file_format!Object


224
225
226
227
228
229
230
# File 'app/models/ci/job_artifact.rb', line 224

def validate_supported_file_format!
  return if Feature.disabled?(:drop_license_management_artifact, project, default_enabled: true)

  if UNSUPPORTED_FILE_TYPES.include?(self.file_type&.to_sym)
    errors.add(:base, _("File format is no longer supported"))
  end
end