summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/external/file/component.rb
blob: 33e7724bf9bb2c0cf3435ffd6c1bd55bba92564d (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
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module External
        module File
          class Component < Base
            extend ::Gitlab::Utils::Override
            include Gitlab::Utils::StrongMemoize

            def initialize(params, context)
              @location = params[:component]
              super
            end

            def matching?
              super && ::Feature.enabled?(:ci_include_components, context.project)
            end

            def content
              return unless component_result.success?

              component_result.payload.fetch(:content)
            end
            strong_memoize_attr :content

            def metadata
              super.merge(
                type: :component,
                location: masked_location,
                blob: masked_blob,
                raw: nil,
                extra: {}
              )
            end

            def validate_location!
              return unless invalid_location_type?

              errors.push("Included file `#{masked_location}` needs to be a string")
            end

            def validate_context!
              return if context.project&.repository

              errors.push('Unable to use components outside of a project context')
            end

            def validate_content!
              return if content.present?

              errors.push(component_result.message)
            end

            private

            attr_reader :path, :version

            def component_result
              ::Ci::Components::FetchService.new(
                address: location,
                current_user: context.user
              ).execute
            end
            strong_memoize_attr :component_result

            override :expand_context_attrs
            def expand_context_attrs
              {
                project: component_path.project,
                sha: component_path.sha,
                user: context.user,
                variables: context.variables
              }
            end

            def masked_blob
              return unless component_path

              context.mask_variables_from(
                Gitlab::Routing.url_helpers.project_blob_url(
                  component_path.project,
                  ::File.join(component_path.sha, component_path.project_file_path))
              )
            end
            strong_memoize_attr :masked_blob

            def component_path
              return unless component_result.success?

              component_result.payload.fetch(:path)
            end
            strong_memoize_attr :component_path
          end
        end
      end
    end
  end
end