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

85 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|
parallelized_jobs[job_name.to_sym] || 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
if all_job_names = parallelized_jobs[job_need_name]
all_job_names.map do |job_name|
2020-01-01 13:55:28 +05:30
job_need.merge(name: job_name)
2019-12-26 22:10:19 +05:30
end
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
2019-10-12 21:52:04 +05:30
hash[job_name] = self.class.parallelize_job_names(job_name, config[:parallel])
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)
parallelized_jobs[job_name].each_with_index do |name, index|
hash[name.to_sym] =
yield(name, config.merge(name: name, instance: index + 1))
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
def self.parallelize_job_names(name, total)
2019-10-12 21:52:04 +05:30
Array.new(total) { |index| "#{name} #{index + 1}/#{total}" }
2018-12-13 13:39:08 +05:30
end
end
end
end
end