normalizer.rb 1.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
module Gitlab
  module Ci
    class Config
      class Normalizer
        class << self
          def normalize_jobs(jobs_config)
            parallelized_jobs = {}

            parallelized_config = jobs_config.map do |name, config|
10
              if config[:parallel]
11
12
                total = config[:parallel]
                names = parallelize_job_names(name, total)
13
14
                parallelized_jobs[name] = names.map(&:first)
                Hash[names.collect { |job_name, index| [job_name.to_sym, config.merge(name: job_name, instance: index)] }]
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
              else
                { name => config }
              end
            end.reduce(:merge)

            parallelized_config.each do |name, config|
              next unless config[:dependencies]

              deps = config[:dependencies].map do |dep|
                if parallelized_jobs.keys.include?(dep.to_sym)
                  config[:dependencies].delete(dep)
                  parallelized_jobs[dep.to_sym]
                else
                  dep
                end
              end.flatten

              config[:dependencies] = deps
            end
          end

          private

          def parallelize_job_names(name, total)
            jobs = []

            total.times do |idx|
42
              jobs << ["#{name} #{idx + 1}/#{total}", idx + 1]
43
44
45
46
47
48
49
50
51
            end

            jobs
          end
        end
      end
    end
  end
end