summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/yaml_processor/dag.rb
blob: 8ab9573dd20dbe8aff114cb8e9aa15ed91a0d2dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42
# frozen_string_literal: true

# Represents Dag pipeline
module Gitlab
  module Ci
    class YamlProcessor
      class Dag
        include TSort

        MissingNodeError = Class.new(StandardError)

        def initialize(nodes)
          @nodes = nodes
        end

        def self.check_circular_dependencies!(jobs)
          nodes = jobs.values.to_h do |job|
            name = job[:name].to_s
            needs = job.dig(:needs, :job).to_a

            [name, needs.map { |need| need[:name].to_s }]
          end

          new(nodes).tsort
        rescue TSort::Cyclic
          raise ValidationError, 'The pipeline has circular dependencies'
        rescue MissingNodeError
        end

        def tsort_each_child(node, &block)
          raise MissingNodeError, "node #{node} is missing" unless @nodes[node]

          @nodes[node].each(&block)
        end

        def tsort_each_node(&block)
          @nodes.each_key(&block)
        end
      end
    end
  end
end