Welcome to mirror list, hosted at ThFree Co, Russian Federation.

generator.rb « downstream « variables « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 350d29958cfc12ab14e8bdc0e2c5098262f29741 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# frozen_string_literal: true

module Gitlab
  module Ci
    module Variables
      module Downstream
        class Generator
          include Gitlab::Utils::StrongMemoize

          Context = Struct.new(:all_bridge_variables, :expand_file_refs, keyword_init: true)

          def initialize(bridge)
            @bridge = bridge

            context = Context.new(all_bridge_variables: bridge.variables, expand_file_refs: bridge.expand_file_refs?)

            @raw_variable_generator = RawVariableGenerator.new(context)
            @expandable_variable_generator = ExpandableVariableGenerator.new(context)
          end

          def calculate
            calculate_downstream_variables
              .reverse # variables priority
              .uniq { |var| var[:key] } # only one variable key to pass
              .reverse
          end

          private

          attr_reader :bridge, :all_bridge_variables

          def calculate_downstream_variables
            # The order of this list refers to the priority of the variables
            # The variables added later takes priority.
            downstream_yaml_variables +
              downstream_pipeline_variables +
              downstream_pipeline_schedule_variables
          end

          def downstream_yaml_variables
            return [] unless bridge.forward_yaml_variables?

            build_downstream_variables_from(bridge.yaml_variables)
          end

          def downstream_pipeline_variables
            return [] unless bridge.forward_pipeline_variables?

            pipeline_variables = bridge.pipeline_variables.to_a
            build_downstream_variables_from(pipeline_variables)
          end

          def downstream_pipeline_schedule_variables
            return [] unless bridge.forward_pipeline_variables?

            pipeline_schedule_variables = bridge.pipeline_schedule_variables.to_a
            build_downstream_variables_from(pipeline_schedule_variables)
          end

          def build_downstream_variables_from(variables)
            Gitlab::Ci::Variables::Collection.fabricate(variables).flat_map do |item|
              if item.raw?
                @raw_variable_generator.for(item)
              else
                @expandable_variable_generator.for(item)
              end
            end
          end
        end
      end
    end
  end
end