debian-mirror-gitlab/app/models/concerns/ci/metadatable.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

114 lines
3 KiB
Ruby
Raw Normal View History

2019-03-02 22:35:43 +05:30
# 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
2020-04-08 14:13:33 +05:30
accepts_nested_attributes_for :metadata
2019-03-02 22:35:43 +05:30
delegate :timeout, to: :metadata, prefix: true, allow_nil: true
2019-12-04 20:38:33 +05:30
delegate :interruptible, to: :metadata, prefix: false, allow_nil: true
2020-01-01 13:55:28 +05:30
delegate :environment_auto_stop_in, to: :metadata, prefix: false, allow_nil: true
2021-12-11 22:18:48 +05:30
delegate :set_cancel_gracefully, to: :metadata, prefix: false, allow_nil: false
2022-08-27 11:52:29 +05:30
delegate :id_tokens, to: :metadata, allow_nil: true
2023-04-23 21:23:45 +05:30
before_validation :ensure_metadata, on: :create
2019-03-02 22:35:43 +05:30
end
2022-03-02 08:16:31 +05:30
def has_exposed_artifacts?
!!metadata&.has_exposed_artifacts?
end
def cancel_gracefully?
!!metadata&.cancel_gracefully?
end
2019-03-02 22:35:43 +05:30
def ensure_metadata
2023-04-23 21:23:45 +05:30
metadata || build_metadata(project: project)
2019-03-02 22:35:43 +05:30
end
def degenerated?
self.options.blank?
end
def degenerate!
self.class.transaction do
self.update!(options: nil, yaml_variables: nil)
2019-10-12 21:52:04 +05:30
self.needs.all.delete_all
2019-03-02 22:35:43 +05:30
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)
2019-12-26 22:10:19 +05:30
2020-01-01 13:55:28 +05:30
ensure_metadata.tap do |metadata|
# Store presence of exposed artifacts in build metadata to make it easier to query
metadata.has_exposed_artifacts = value&.dig(:artifacts, :expose_as).present?
metadata.environment_auto_stop_in = value&.dig(:environment, :auto_stop_in)
end
2019-03-02 22:35:43 +05:30
end
def yaml_variables=(value)
write_metadata_attribute(:yaml_variables, :config_variables, value)
end
2019-12-04 20:38:33 +05:30
def interruptible
metadata&.interruptible
end
def interruptible=(value)
ensure_metadata.interruptible = value
end
2022-08-27 11:52:29 +05:30
def id_tokens?
2022-11-25 23:54:43 +05:30
metadata&.id_tokens.present?
2022-08-27 11:52:29 +05:30
end
def id_tokens=(value)
ensure_metadata.id_tokens = value
end
2023-01-13 00:05:48 +05:30
def enqueue_immediately?
!!options[:enqueue_immediately]
end
def set_enqueue_immediately!
# ensures that even if `config_options: nil` in the database we set the
# new value correctly.
self.options = options.merge(enqueue_immediately: true)
end
2019-03-02 22:35:43 +05:30
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)
2021-10-27 15:23:28 +05:30
ensure_metadata.write_attribute(metadata_key, value)
write_attribute(legacy_key, nil)
2019-03-02 22:35:43 +05:30
end
end
end
2020-07-28 23:09:34 +05:30
2021-06-08 01:23:25 +05:30
Ci::Metadatable.prepend_mod_with('Ci::Metadatable')