2020-08-07 06:09:47 +00:00
# frozen_string_literal: true
2020-08-07 18:09:53 +00:00
# This class is being used to persist additional artifacts after a pipeline completes, which is a great place to cache a computed result in object storage
2020-08-07 06:09:47 +00:00
module Ci
2021-08-03 06:08:50 +00:00
class PipelineArtifact < Ci :: ApplicationRecord
2020-08-24 12:10:17 +00:00
include UpdateProjectStatistics
2020-08-12 00:09:52 +00:00
include Artifactable
2020-08-19 21:10:32 +00:00
include FileStoreMounter
2022-09-08 15:12:29 +00:00
include Lockable
2020-09-01 21:10:21 +00:00
include Presentable
2020-08-07 06:09:47 +00:00
FILE_SIZE_LIMIT = 10 . megabytes . freeze
2020-08-21 15:10:03 +00:00
EXPIRATION_DATE = 1 . week . freeze
DEFAULT_FILE_NAMES = {
2021-01-22 18:09:10 +00:00
code_coverage : 'code_coverage.json' ,
2021-02-03 18:09:25 +00:00
code_quality_mr_diff : 'code_quality_mr_diff.json'
2020-08-21 15:10:03 +00:00
} . freeze
2020-08-07 06:09:47 +00:00
2021-02-05 12:09:31 +00:00
REPORT_TYPES = {
code_coverage : :raw ,
code_quality_mr_diff : :raw
} . freeze
2020-08-07 06:09:47 +00:00
belongs_to :project , class_name : " Project " , inverse_of : :pipeline_artifacts
belongs_to :pipeline , class_name : " Ci::Pipeline " , inverse_of : :pipeline_artifacts
2020-08-12 12:10:25 +00:00
validates :pipeline , :project , :file_format , :file , presence : true
2020-09-17 21:09:39 +00:00
validates :file_store , presence : true , inclusion : { in : ObjectStorage :: SUPPORTED_STORES }
2020-08-07 06:09:47 +00:00
validates :size , presence : true , numericality : { less_than_or_equal_to : FILE_SIZE_LIMIT }
2020-08-12 18:10:05 +00:00
validates :file_type , presence : true
2020-08-07 06:09:47 +00:00
2020-08-19 21:10:32 +00:00
mount_file_store_uploader Ci :: PipelineArtifactUploader
2020-08-13 15:10:03 +00:00
2020-08-24 12:10:17 +00:00
update_project_statistics project_statistics_name : :pipeline_artifacts_size
2020-08-07 06:09:47 +00:00
enum file_type : {
2021-01-19 21:10:45 +00:00
code_coverage : 1 ,
2021-02-03 18:09:25 +00:00
code_quality_mr_diff : 2
2020-08-07 06:09:47 +00:00
}
2020-08-13 15:10:03 +00:00
2021-04-29 15:10:07 +00:00
scope :unlocked , - > { joins ( :pipeline ) . merge ( :: Ci :: Pipeline . unlocked ) }
2021-01-19 21:10:45 +00:00
class << self
2021-02-05 12:09:31 +00:00
def report_exists? ( file_type )
return false unless REPORT_TYPES . key? ( file_type )
2021-01-19 21:10:45 +00:00
where ( file_type : file_type ) . exists?
end
2020-08-25 12:04:30 +00:00
2021-01-19 21:10:45 +00:00
def find_by_file_type ( file_type )
find_by ( file_type : file_type )
end
2022-06-23 18:09:26 +00:00
def create_or_replace_for_pipeline! ( pipeline : , file_type : , file : , size : )
transaction do
pipeline . pipeline_artifacts . find_by_file_type ( file_type ) & . destroy!
pipeline . pipeline_artifacts . create! (
file_type : file_type ,
project_id : pipeline . project_id ,
size : size ,
file : file ,
file_format : REPORT_TYPES [ file_type ] ,
expire_at : EXPIRATION_DATE . from_now
)
end
rescue ActiveRecord :: ActiveRecordError = > err
Gitlab :: ErrorTracking . track_and_raise_exception ( err , { pipeline_id : pipeline . id , file_type : file_type } )
end
2020-08-25 12:04:30 +00:00
end
2020-09-01 21:10:21 +00:00
def present
super ( presenter_class : " Ci::PipelineArtifacts:: #{ self . file_type . camelize } Presenter " . constantize )
end
2020-08-07 06:09:47 +00:00
end
end
2021-04-01 18:13:56 +00:00
2021-05-11 21:10:21 +00:00
Ci :: PipelineArtifact . prepend_mod