2019-01-28 07:37:09 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
|
|
|
##
|
|
|
|
# This module implements methods that need to read and write
|
|
|
|
# metadata for CI/CD entities.
|
|
|
|
#
|
|
|
|
module Metadatable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
has_one :metadata, class_name: 'Ci::BuildMetadata',
|
|
|
|
foreign_key: :build_id,
|
|
|
|
inverse_of: :build,
|
|
|
|
autosave: true
|
|
|
|
|
|
|
|
delegate :timeout, to: :metadata, prefix: true, allow_nil: true
|
2019-09-05 10:50:39 -04:00
|
|
|
delegate :interruptible, to: :metadata, prefix: false, allow_nil: true
|
2019-01-28 07:37:09 -05:00
|
|
|
before_create :ensure_metadata
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_metadata
|
|
|
|
metadata || build_metadata(project: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def degenerated?
|
|
|
|
self.options.blank?
|
|
|
|
end
|
|
|
|
|
|
|
|
def degenerate!
|
|
|
|
self.class.transaction do
|
|
|
|
self.update!(options: nil, yaml_variables: nil)
|
2019-08-01 10:26:49 -04:00
|
|
|
self.needs.all.delete_all
|
2019-01-28 07:37:09 -05:00
|
|
|
self.metadata&.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def options
|
|
|
|
read_metadata_attribute(:options, :config_options, {})
|
|
|
|
end
|
|
|
|
|
|
|
|
def yaml_variables
|
|
|
|
read_metadata_attribute(:yaml_variables, :config_variables, [])
|
|
|
|
end
|
|
|
|
|
|
|
|
def options=(value)
|
|
|
|
write_metadata_attribute(:options, :config_options, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def yaml_variables=(value)
|
|
|
|
write_metadata_attribute(:yaml_variables, :config_variables, value)
|
|
|
|
end
|
|
|
|
|
2019-09-05 10:50:39 -04:00
|
|
|
def interruptible
|
|
|
|
metadata&.interruptible
|
|
|
|
end
|
|
|
|
|
|
|
|
def interruptible=(value)
|
|
|
|
ensure_metadata.interruptible = value
|
|
|
|
end
|
|
|
|
|
2019-01-28 07:37:09 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def read_metadata_attribute(legacy_key, metadata_key, default_value = nil)
|
|
|
|
read_attribute(legacy_key) || metadata&.read_attribute(metadata_key) || default_value
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_metadata_attribute(legacy_key, metadata_key, value)
|
|
|
|
# save to metadata or this model depending on the state of feature flag
|
|
|
|
if Feature.enabled?(:ci_build_metadata_config)
|
|
|
|
ensure_metadata.write_attribute(metadata_key, value)
|
|
|
|
write_attribute(legacy_key, nil)
|
|
|
|
else
|
|
|
|
write_attribute(legacy_key, value)
|
|
|
|
metadata&.write_attribute(metadata_key, nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|