debian-mirror-gitlab/lib/gitlab/ci/config/normalizer.rb

83 lines
2.2 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
module Gitlab
module Ci
class Config
class Normalizer
2019-10-12 21:52:04 +05:30
include Gitlab::Utils::StrongMemoize
2018-12-13 13:39:08 +05:30
def initialize(jobs_config)
@jobs_config = jobs_config
end
def normalize_jobs
2019-10-12 21:52:04 +05:30
return @jobs_config if parallelized_jobs.empty?
expand_parallelize_jobs do |job_name, config|
if config[:dependencies]
config[:dependencies] = expand_names(config[:dependencies])
end
2018-12-13 13:39:08 +05:30
2019-12-26 22:10:19 +05:30
if job_needs = config.dig(:needs, :job)
config[:needs][:job] = expand_needs(job_needs)
2019-10-12 21:52:04 +05:30
end
config
end
2018-12-13 13:39:08 +05:30
end
private
2019-10-12 21:52:04 +05:30
def expand_names(job_names)
return unless job_names
2018-12-13 13:39:08 +05:30
2019-10-12 21:52:04 +05:30
job_names.flat_map do |job_name|
2020-10-24 23:57:45 +05:30
parallelized_jobs[job_name.to_sym]&.map(&:name) || job_name
2018-12-13 13:39:08 +05:30
end
end
2019-12-26 22:10:19 +05:30
def expand_needs(job_needs)
return unless job_needs
job_needs.flat_map do |job_need|
job_need_name = job_need[:name].to_sym
2020-10-24 23:57:45 +05:30
if all_jobs = parallelized_jobs[job_need_name]
all_jobs.map { |job| job_need.merge(name: job.name) }
2019-12-26 22:10:19 +05:30
else
job_need
end
end
end
2019-10-12 21:52:04 +05:30
def parallelized_jobs
strong_memoize(:parallelized_jobs) do
@jobs_config.each_with_object({}) do |(job_name, config), hash|
next unless config[:parallel]
2018-12-13 13:39:08 +05:30
2020-10-24 23:57:45 +05:30
hash[job_name] = parallelize_job_config(job_name, config[:parallel])
2019-10-12 21:52:04 +05:30
end
2018-12-13 13:39:08 +05:30
end
end
2019-10-12 21:52:04 +05:30
def expand_parallelize_jobs
@jobs_config.each_with_object({}) do |(job_name, config), hash|
if parallelized_jobs.key?(job_name)
2020-10-24 23:57:45 +05:30
parallelized_jobs[job_name].each do |job|
hash[job.name.to_sym] =
yield(job.name, config.deep_merge(job.attributes))
2019-10-12 21:52:04 +05:30
end
2018-12-13 13:39:08 +05:30
else
2019-10-12 21:52:04 +05:30
hash[job_name] = yield(job_name, config)
2018-12-13 13:39:08 +05:30
end
end
end
2020-10-24 23:57:45 +05:30
def parallelize_job_config(name, config)
Normalizer::Factory.new(name, config).create
2018-12-13 13:39:08 +05:30
end
end
end
end
end