Commit 5fd8933d authored by Matija Čupić's avatar Matija Čupić

Use instance based approach for Normalizer

parent a8da5238
...@@ -4,21 +4,24 @@ module Gitlab ...@@ -4,21 +4,24 @@ module Gitlab
module Ci module Ci
class Config class Config
class Normalizer class Normalizer
class << self def initialize(jobs_config)
def normalize_jobs(jobs_config) @jobs_config = jobs_config
parallelized_config, parallelized_jobs = parallelize_jobs(jobs_config) end
parallelize_dependencies(parallelized_config, parallelized_jobs)
def normalize_jobs
parallelized_jobs = parallelize_jobs
parallelize_dependencies(parallelized_jobs)
end end
private private
def parallelize_jobs(jobs_config) def parallelize_jobs
parallelized_jobs = {} parallelized_jobs = {}
parallelized_config = jobs_config.map do |name, config| @jobs_config = @jobs_config.map do |name, config|
if config[:parallel] if config[:parallel]
total = config[:parallel] total = config[:parallel]
names = parallelize_job_names(name, total) names = self.class.parallelize_job_names(name, total)
parallelized_jobs[name] = names.map(&:first) parallelized_jobs[name] = names.map(&:first)
Hash[names.collect { |job_name, index| [job_name.to_sym, config.merge(name: job_name, instance: index)] }] Hash[names.collect { |job_name, index| [job_name.to_sym, config.merge(name: job_name, instance: index)] }]
else else
...@@ -26,11 +29,11 @@ module Gitlab ...@@ -26,11 +29,11 @@ module Gitlab
end end
end.reduce(:merge) end.reduce(:merge)
[parallelized_config, parallelized_jobs] parallelized_jobs
end end
def parallelize_dependencies(jobs_config, parallelized_jobs) def parallelize_dependencies(parallelized_jobs)
jobs_config.map do |name, config| @jobs_config.map do |name, config|
if config[:dependencies] if config[:dependencies]
deps = config[:dependencies].map do |dep| deps = config[:dependencies].map do |dep|
if parallelized_jobs.keys.include?(dep.to_sym) if parallelized_jobs.keys.include?(dep.to_sym)
...@@ -47,15 +50,8 @@ module Gitlab ...@@ -47,15 +50,8 @@ module Gitlab
end.reduce(:merge) end.reduce(:merge)
end end
def parallelize_job_names(name, total) def self.parallelize_job_names(name, total)
jobs = [] Array.new(total) { |index| ["#{name} #{index + 1}/#{total}", index + 1] }
1.upto(total) do |idx|
jobs << ["#{name} #{idx}/#{total}", idx]
end
jobs
end
end end
end end
end end
......
...@@ -104,7 +104,7 @@ module Gitlab ...@@ -104,7 +104,7 @@ module Gitlab
## ##
# Jobs # 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| @jobs.each do |name, job|
# logical validation for job # logical validation for job
......
...@@ -8,7 +8,7 @@ describe Gitlab::Ci::Config::Normalizer do ...@@ -8,7 +8,7 @@ describe Gitlab::Ci::Config::Normalizer do
let(:config) { { job_name => job_config } } let(:config) { { job_name => job_config } }
describe '.normalize_jobs' do describe '.normalize_jobs' do
subject { described_class.normalize_jobs(config) } subject { described_class.new(config).normalize_jobs }
it 'does not have original job' do it 'does not have original job' do
is_expected.not_to include(job_name) 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