2018-11-02 13:20:27 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-31 15:40:52 +00:00
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Config
|
|
|
|
class Normalizer
|
2018-11-05 14:53:37 +00:00
|
|
|
class << self
|
|
|
|
def normalize_jobs(jobs_config)
|
|
|
|
parallelized_jobs = extract_parallelized_jobs(jobs_config)
|
|
|
|
parallelized_config = parallelize_jobs(jobs_config, parallelized_jobs)
|
|
|
|
parallelize_dependencies(parallelized_config, parallelized_jobs)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def extract_parallelized_jobs(jobs_config)
|
|
|
|
parallelized_jobs = {}
|
|
|
|
|
|
|
|
jobs_config.each do |job_name, config|
|
|
|
|
if config[:parallel]
|
|
|
|
parallelized_jobs[job_name] = parallelize_job_names(job_name, config[:parallel])
|
|
|
|
end
|
|
|
|
end
|
2018-11-05 13:36:52 +00:00
|
|
|
|
2018-11-05 14:53:37 +00:00
|
|
|
parallelized_jobs
|
|
|
|
end
|
2018-11-05 13:36:52 +00:00
|
|
|
|
2018-11-05 14:53:37 +00:00
|
|
|
def parallelize_jobs(jobs_config, parallelized_jobs)
|
|
|
|
jobs_config.each_with_object({}) do |(job_name, config), hash|
|
2018-11-06 17:23:45 +00:00
|
|
|
if parallelized_jobs.key?(job_name)
|
2018-11-05 14:53:37 +00:00
|
|
|
parallelized_jobs[job_name].each { |name, index| hash[name.to_sym] = config.merge(name: name, instance: index) }
|
|
|
|
else
|
|
|
|
hash[job_name] = config
|
|
|
|
end
|
2018-11-05 13:36:52 +00:00
|
|
|
|
2018-11-05 14:53:37 +00:00
|
|
|
hash
|
2018-10-31 15:40:52 +00:00
|
|
|
end
|
2018-11-05 14:53:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def parallelize_dependencies(parallelized_config, parallelized_jobs)
|
|
|
|
parallelized_config.each_with_object({}) do |(job_name, config), hash|
|
|
|
|
intersection = config[:dependencies] & parallelized_jobs.keys.map(&:to_s)
|
|
|
|
if intersection && intersection.any?
|
|
|
|
deps = intersection.map { |dep| parallelized_jobs[dep.to_sym].map(&:first) }.flatten
|
|
|
|
hash[job_name] = config.merge(dependencies: deps)
|
|
|
|
else
|
|
|
|
hash[job_name] = config
|
|
|
|
end
|
|
|
|
|
|
|
|
hash
|
2018-11-05 13:36:52 +00:00
|
|
|
end
|
2018-11-05 14:53:37 +00:00
|
|
|
end
|
2018-10-31 15:40:52 +00:00
|
|
|
|
2018-11-05 14:53:37 +00:00
|
|
|
def parallelize_job_names(name, total)
|
|
|
|
Array.new(total) { |index| ["#{name} #{index + 1}/#{total}", index + 1] }
|
|
|
|
end
|
2018-10-31 15:40:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|