summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/external/mapper.rb
blob: 79a04ad409e7e2f62c7e5ee1daa28f75cf8ecf21 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module External
        class Mapper
          include Gitlab::Utils::StrongMemoize

          MAX_INCLUDES = 100

          FILE_CLASSES = [
            External::File::Remote,
            External::File::Template,
            External::File::Local,
            External::File::Project,
            External::File::Artifact
          ].freeze

          Error = Class.new(StandardError)
          AmbigiousSpecificationError = Class.new(Error)
          TooManyIncludesError = Class.new(Error)

          def initialize(values, context)
            @locations = Array.wrap(values.fetch(:include, []))
            @context = context
          end

          def process
            return [] if locations.empty?

            logger.instrument(:config_mapper_process) do
              process_without_instrumentation
            end
          end

          private

          attr_reader :locations, :context

          delegate :expandset, :logger, to: :context

          def process_without_instrumentation
            locations
              .compact
              .map(&method(:normalize_location))
              .filter_map(&method(:verify_rules))
              .flat_map(&method(:expand_project_files))
              .flat_map(&method(:expand_wildcard_paths))
              .map(&method(:expand_variables))
              .each(&method(:verify_duplicates!))
              .map(&method(:select_first_matching))
          end

          def normalize_location(location)
            logger.instrument(:config_mapper_normalize) do
              normalize_location_without_instrumentation(location)
            end
          end

          # convert location if String to canonical form
          def normalize_location_without_instrumentation(location)
            if location.is_a?(String)
              expanded_location = expand_variables(location)
              normalize_location_string(expanded_location)
            else
              location.deep_symbolize_keys
            end
          end

          def verify_rules(location)
            logger.instrument(:config_mapper_rules) do
              verify_rules_without_instrumentation(location)
            end
          end

          def verify_rules_without_instrumentation(location)
            return unless Rules.new(location[:rules]).evaluate(context).pass?

            location
          end

          def expand_project_files(location)
            return location unless location[:project]

            Array.wrap(location[:file]).map do |file|
              location.merge(file: file)
            end
          end

          def expand_wildcard_paths(location)
            logger.instrument(:config_mapper_wildcards) do
              expand_wildcard_paths_without_instrumentation(location)
            end
          end

          def expand_wildcard_paths_without_instrumentation(location)
            # We only support local files for wildcard paths
            return location unless location[:local] && location[:local].include?('*')

            context.project.repository.search_files_by_wildcard_path(location[:local], context.sha).map do |path|
              { local: path }
            end
          end

          def normalize_location_string(location)
            if ::Gitlab::UrlSanitizer.valid?(location)
              { remote: location }
            else
              { local: location }
            end
          end

          def verify_duplicates!(location)
            logger.instrument(:config_mapper_verify) do
              verify_max_includes_and_add_location!(location)
            end
          end

          def verify_max_includes_and_add_location!(location)
            if expandset.count >= MAX_INCLUDES
              raise TooManyIncludesError, "Maximum of #{MAX_INCLUDES} nested includes are allowed!"
            end

            # Scope location to context to allow support of
            # relative includes
            scoped_location = location.merge(
              context_project: context.project,
              context_sha: context.sha)

            expandset.add(scoped_location)
          end

          def select_first_matching(location)
            logger.instrument(:config_mapper_select) do
              select_first_matching_without_instrumentation(location)
            end
          end

          def select_first_matching_without_instrumentation(location)
            matching = FILE_CLASSES.map do |file_class|
              file_class.new(location, context)
            end.select(&:matching?)

            raise AmbigiousSpecificationError, "Include `#{masked_location(location.to_json)}` needs to match exactly one accessor!" unless matching.one?

            matching.first
          end

          def expand_variables(data)
            logger.instrument(:config_mapper_variables) do
              expand_variables_without_instrumentation(data)
            end
          end

          def expand_variables_without_instrumentation(data)
            if data.is_a?(String)
              expand(data)
            else
              transform(data)
            end
          end

          def transform(data)
            data.transform_values do |values|
              case values
              when Array
                values.map { |value| expand(value.to_s) }
              when String
                expand(values)
              else
                values
              end
            end
          end

          def expand(data)
            ExpandVariables.expand(data, -> { context.variables_hash })
          end

          def masked_location(location)
            context.mask_variables_from(location)
          end
        end
      end
    end
  end
end