debian-mirror-gitlab/lib/gitlab/ci/config/entry/jobs.rb

80 lines
2 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
module Gitlab
module Ci
class Config
2017-08-17 22:00:37 +05:30
module Entry
2016-09-13 17:45:13 +05:30
##
# Entry that represents a set of jobs.
#
2019-02-15 15:39:39 +05:30
class Jobs < ::Gitlab::Config::Entry::Node
include ::Gitlab::Config::Entry::Validatable
2016-09-13 17:45:13 +05:30
validations do
validates :config, type: Hash
validate do
2019-09-30 21:07:59 +05:30
unless has_valid_jobs?
errors.add(:config, 'should contain valid jobs')
end
2016-09-13 17:45:13 +05:30
unless has_visible_job?
errors.add(:config, 'should contain at least one visible job')
end
end
2019-09-30 21:07:59 +05:30
def has_valid_jobs?
config.all? do |name, value|
Jobs.find_type(name, value)
end
end
2016-09-13 17:45:13 +05:30
def has_visible_job?
2019-09-30 21:07:59 +05:30
config.any? do |name, value|
Jobs.find_type(name, value)&.visible?
end
2016-09-13 17:45:13 +05:30
end
end
2020-03-13 15:44:24 +05:30
TYPES = [Entry::Hidden, Entry::Job, Entry::Bridge].freeze
2019-09-30 21:07:59 +05:30
private_constant :TYPES
def self.all_types
TYPES
2016-09-13 17:45:13 +05:30
end
2019-09-30 21:07:59 +05:30
def self.find_type(name, config)
self.all_types.find do |type|
type.matching?(name, config)
end
2019-03-02 22:35:43 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-09-29 09:46:39 +05:30
def compose!(deps = nil)
super do
@config.each do |name, config|
2019-09-30 21:07:59 +05:30
node = self.class.find_type(name, config)
next unless node
2016-09-29 09:46:39 +05:30
2019-02-15 15:39:39 +05:30
factory = ::Gitlab::Config::Entry::Factory.new(node)
2016-09-29 09:46:39 +05:30
.value(config || {})
.metadata(name: name)
.with(key: name, parent: self,
description: "#{name} job definition.")
@entries[name] = factory.create!
end
2016-09-13 17:45:13 +05:30
2016-09-29 09:46:39 +05:30
@entries.each_value do |entry|
entry.compose!(deps)
end
2016-09-13 17:45:13 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-09-13 17:45:13 +05:30
end
end
end
end
end