diff options
author | Matija Čupić <matteeyah@gmail.com> | 2018-11-05 16:36:52 +0300 |
---|---|---|
committer | Matija Čupić <matteeyah@gmail.com> | 2018-11-05 16:36:52 +0300 |
commit | 5fd8933d97b694fb8ae6a57acaae8fb4e7922e73 (patch) | |
tree | 953d2e43b41d57af7b57f6406d6121eb4db74618 /lib/gitlab/ci | |
parent | a8da5238474f8f7ad4dab5461c5c5605894535db (diff) |
Use instance based approach for Normalizer
Diffstat (limited to 'lib/gitlab/ci')
-rw-r--r-- | lib/gitlab/ci/config/normalizer.rb | 96 | ||||
-rw-r--r-- | lib/gitlab/ci/yaml_processor.rb | 2 |
2 files changed, 47 insertions, 51 deletions
diff --git a/lib/gitlab/ci/config/normalizer.rb b/lib/gitlab/ci/config/normalizer.rb index 43331b28e2b..0eae6b9770b 100644 --- a/lib/gitlab/ci/config/normalizer.rb +++ b/lib/gitlab/ci/config/normalizer.rb @@ -4,58 +4,54 @@ module Gitlab module Ci class Config class Normalizer - class << self - def normalize_jobs(jobs_config) - parallelized_config, parallelized_jobs = parallelize_jobs(jobs_config) - parallelize_dependencies(parallelized_config, parallelized_jobs) - end - - private - - def parallelize_jobs(jobs_config) - parallelized_jobs = {} - - parallelized_config = jobs_config.map do |name, config| - if config[:parallel] - total = config[:parallel] - names = parallelize_job_names(name, total) - parallelized_jobs[name] = names.map(&:first) - Hash[names.collect { |job_name, index| [job_name.to_sym, config.merge(name: job_name, instance: index)] }] - else - { name => config } - end - end.reduce(:merge) - - [parallelized_config, parallelized_jobs] - end - - def parallelize_dependencies(jobs_config, parallelized_jobs) - jobs_config.map do |name, config| - if config[:dependencies] - deps = config[:dependencies].map do |dep| - if parallelized_jobs.keys.include?(dep.to_sym) - parallelized_jobs[dep.to_sym] - else - dep - end - end.flatten - - { name => config.merge(dependencies: deps) } - else - { name => config } - end - end.reduce(:merge) - end - - def parallelize_job_names(name, total) - jobs = [] - - 1.upto(total) do |idx| - jobs << ["#{name} #{idx}/#{total}", idx] + def initialize(jobs_config) + @jobs_config = jobs_config + end + + def normalize_jobs + parallelized_jobs = parallelize_jobs + parallelize_dependencies(parallelized_jobs) + end + + private + + def parallelize_jobs + parallelized_jobs = {} + + @jobs_config = @jobs_config.map do |name, config| + if config[:parallel] + total = config[:parallel] + names = self.class.parallelize_job_names(name, total) + parallelized_jobs[name] = names.map(&:first) + Hash[names.collect { |job_name, index| [job_name.to_sym, config.merge(name: job_name, instance: index)] }] + else + { name => config } end + end.reduce(:merge) + + parallelized_jobs + end + + def parallelize_dependencies(parallelized_jobs) + @jobs_config.map do |name, config| + if config[:dependencies] + deps = config[:dependencies].map do |dep| + if parallelized_jobs.keys.include?(dep.to_sym) + parallelized_jobs[dep.to_sym] + else + dep + end + end.flatten + + { name => config.merge(dependencies: deps) } + else + { name => config } + end + end.reduce(:merge) + end - jobs - end + def self.parallelize_job_names(name, total) + Array.new(total) { |index| ["#{name} #{index + 1}/#{total}", index + 1] } end end end diff --git a/lib/gitlab/ci/yaml_processor.rb b/lib/gitlab/ci/yaml_processor.rb index 63b55c57913..f65a4e198cc 100644 --- a/lib/gitlab/ci/yaml_processor.rb +++ b/lib/gitlab/ci/yaml_processor.rb @@ -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 |