2018-11-02 09:20:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-31 11:40:52 -04:00
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Config
|
|
|
|
class Normalizer
|
2019-08-01 10:26:49 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2018-11-06 12:35:44 -05:00
|
|
|
def initialize(jobs_config)
|
|
|
|
@jobs_config = jobs_config
|
|
|
|
end
|
|
|
|
|
|
|
|
def normalize_jobs
|
2020-09-01 14:10:48 -04:00
|
|
|
return {} unless @jobs_config
|
2019-08-01 10:26:49 -04:00
|
|
|
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-11-07 07:01:33 -05:00
|
|
|
|
2019-11-08 07:06:32 -05:00
|
|
|
if job_needs = config.dig(:needs, :job)
|
|
|
|
config[:needs][:job] = expand_needs(job_needs)
|
2019-08-01 10:26:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
config
|
|
|
|
end
|
2018-11-06 12:35:44 -05:00
|
|
|
end
|
2018-11-05 09:53:37 -05:00
|
|
|
|
2018-11-06 12:35:44 -05:00
|
|
|
private
|
2018-11-05 09:53:37 -05:00
|
|
|
|
2019-08-01 10:26:49 -04:00
|
|
|
def expand_names(job_names)
|
|
|
|
return unless job_names
|
2018-11-05 09:53:37 -05:00
|
|
|
|
2019-08-01 10:26:49 -04:00
|
|
|
job_names.flat_map do |job_name|
|
2020-07-20 05:09:22 -04:00
|
|
|
parallelized_jobs[job_name.to_sym]&.map(&:name) || job_name
|
2018-11-05 09:53:37 -05:00
|
|
|
end
|
2018-11-06 12:35:44 -05:00
|
|
|
end
|
2018-11-05 08:36:52 -05:00
|
|
|
|
2019-11-08 07:06:32 -05:00
|
|
|
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-07-20 05:09:22 -04:00
|
|
|
if all_jobs = parallelized_jobs[job_need_name]
|
|
|
|
all_jobs.map { |job| job_need.merge(name: job.name) }
|
2019-11-08 07:06:32 -05:00
|
|
|
else
|
|
|
|
job_need
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-01 10:26:49 -04:00
|
|
|
def parallelized_jobs
|
|
|
|
strong_memoize(:parallelized_jobs) do
|
|
|
|
@jobs_config.each_with_object({}) do |(job_name, config), hash|
|
|
|
|
next unless config[:parallel]
|
2018-11-06 12:35:44 -05:00
|
|
|
|
2020-07-20 05:09:22 -04:00
|
|
|
hash[job_name] = parallelize_job_config(job_name, config[:parallel])
|
2019-08-01 10:26:49 -04:00
|
|
|
end
|
2018-11-05 09:53:37 -05:00
|
|
|
end
|
2018-11-06 12:35:44 -05:00
|
|
|
end
|
2018-11-05 09:53:37 -05:00
|
|
|
|
2019-08-01 10:26:49 -04:00
|
|
|
def expand_parallelize_jobs
|
|
|
|
@jobs_config.each_with_object({}) do |(job_name, config), hash|
|
|
|
|
if parallelized_jobs.key?(job_name)
|
2020-07-20 05:09:22 -04:00
|
|
|
parallelized_jobs[job_name].each do |job|
|
|
|
|
hash[job.name.to_sym] =
|
|
|
|
yield(job.name, config.deep_merge(job.attributes))
|
2019-08-01 10:26:49 -04:00
|
|
|
end
|
2018-11-06 12:35:44 -05:00
|
|
|
else
|
2019-08-01 10:26:49 -04:00
|
|
|
hash[job_name] = yield(job_name, config)
|
2018-11-05 08:36:52 -05:00
|
|
|
end
|
2018-11-05 09:53:37 -05:00
|
|
|
end
|
2018-10-31 11:40:52 -04:00
|
|
|
end
|
2018-11-06 12:35:44 -05:00
|
|
|
|
2020-07-20 05:09:22 -04:00
|
|
|
def parallelize_job_config(name, config)
|
|
|
|
Normalizer::Factory.new(name, config).create
|
2018-11-06 12:35:44 -05:00
|
|
|
end
|
2018-10-31 11:40:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|