Commit 4ba960c1 authored by Matija Čupić's avatar Matija Čupić

Use instance based approach for Normalizer

parent f48261a4
......@@ -4,31 +4,34 @@ module Gitlab
module Ci
class Config
class Normalizer
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)
def initialize(jobs_config)
@jobs_config = jobs_config
end
def normalize_jobs
extract_parallelized_jobs
parallelized_config = parallelize_jobs
parallelize_dependencies(parallelized_config)
end
private
def extract_parallelized_jobs(jobs_config)
parallelized_jobs = {}
def extract_parallelized_jobs
@parallelized_jobs = {}
jobs_config.each do |job_name, config|
@jobs_config.each do |job_name, config|
if config[:parallel]
parallelized_jobs[job_name] = parallelize_job_names(job_name, config[:parallel])
@parallelized_jobs[job_name] = self.class.parallelize_job_names(job_name, config[:parallel])
end
end
parallelized_jobs
@parallelized_jobs
end
def parallelize_jobs(jobs_config, parallelized_jobs)
jobs_config.each_with_object({}) do |(job_name, config), hash|
if parallelized_jobs.key?(job_name)
parallelized_jobs[job_name].each { |name, index| hash[name.to_sym] = config.merge(name: name, instance: index) }
def parallelize_jobs
@jobs_config.each_with_object({}) do |(job_name, config), hash|
if @parallelized_jobs.key?(job_name)
@parallelized_jobs[job_name].each { |name, index| hash[name.to_sym] = config.merge(name: name, instance: index) }
else
hash[job_name] = config
end
......@@ -37,11 +40,11 @@ module Gitlab
end
end
def parallelize_dependencies(parallelized_config, parallelized_jobs)
def parallelize_dependencies(parallelized_config)
parallelized_config.each_with_object({}) do |(job_name, config), hash|
intersection = config[:dependencies] & parallelized_jobs.keys.map(&:to_s)
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
deps = intersection.map { |dep| @parallelized_jobs[dep.to_sym].map(&:first) }.flatten
hash[job_name] = config.merge(dependencies: deps)
else
hash[job_name] = config
......@@ -51,11 +54,10 @@ module Gitlab
end
end
def parallelize_job_names(name, total)
def self.parallelize_job_names(name, total)
Array.new(total) { |index| ["#{name} #{index + 1}/#{total}", index + 1] }
end
end
end
end
end
end
......@@ -104,7 +104,7 @@ module Gitlab
##
# Jobs
#
@jobs = Ci::Config::Normalizer.normalize_jobs(@ci_config.jobs)
@jobs = Ci::Config::Normalizer.new(@ci_config.jobs).normalize_jobs
@jobs.each do |name, job|
# logical validation for job
......
......@@ -8,7 +8,7 @@ describe Gitlab::Ci::Config::Normalizer do
let(:config) { { job_name => job_config } }
describe '.normalize_jobs' do
subject { described_class.normalize_jobs(config) }
subject { described_class.new(config).normalize_jobs }
it 'does not have original job' do
is_expected.not_to include(job_name)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment