1# frozen_string_literal: true
2
3module Gitlab
4  module Ci
5    class Config
6      module External
7        module File
8          class Local < Base
9            extend ::Gitlab::Utils::Override
10            include Gitlab::Utils::StrongMemoize
11
12            def initialize(params, context)
13              @location = params[:local]
14
15              super
16            end
17
18            def content
19              strong_memoize(:content) { fetch_local_content }
20            end
21
22            private
23
24            def validate_content!
25              if context.project&.repository.nil?
26                errors.push("Local file `#{location}` does not have project!")
27              elsif content.nil?
28                errors.push("Local file `#{location}` does not exist!")
29              elsif content.blank?
30                errors.push("Local file `#{location}` is empty!")
31              end
32            end
33
34            def fetch_local_content
35              context.project.repository.blob_data_at(context.sha, location)
36            end
37
38            override :expand_context_attrs
39            def expand_context_attrs
40              {
41                project: context.project,
42                sha: context.sha,
43                user: context.user,
44                parent_pipeline: context.parent_pipeline,
45                variables: context.variables
46              }
47            end
48          end
49        end
50      end
51    end
52  end
53end
54