summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/external/mapper/location_expander.rb
blob: a4ca058f0d94e140fb6ee5e8f1f078e5d07185b3 (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

module Gitlab
  module Ci
    class Config
      module External
        class Mapper
          # Expands locations to include all files matching the pattern
          class LocationExpander < Base
            private

            def process_without_instrumentation(locations)
              locations.flat_map do |location|
                if location[:project]
                  expand_project_files(location)
                elsif location[:local]
                  expand_wildcard_paths(location)
                else
                  location
                end
              end
            end

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

            def expand_wildcard_paths(location)
              return location unless location[:local].include?('*')

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