Class: Gitlab::Ci::Config::External::File::Base

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/ci/config/external/file/base.rb

Direct Known Subclasses

Artifact, Component, Local, Project, Remote, Template

Constant Summary collapse

YAML_WHITELIST_EXTENSION =
/.+\.(yml|yaml)$/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, context) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
# File 'lib/gitlab/ci/config/external/file/base.rb', line 15

def initialize(params, context)
  @params = params
  @context = context
  @errors = []
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



11
12
13
# File 'lib/gitlab/ci/config/external/file/base.rb', line 11

def context
  @context
end

#errorsObject (readonly)

Returns the value of attribute errors.



11
12
13
# File 'lib/gitlab/ci/config/external/file/base.rb', line 11

def errors
  @errors
end

#locationObject (readonly)

Returns the value of attribute location.



11
12
13
# File 'lib/gitlab/ci/config/external/file/base.rb', line 11

def location
  @location
end

#paramsObject (readonly)

Returns the value of attribute params.



11
12
13
# File 'lib/gitlab/ci/config/external/file/base.rb', line 11

def params
  @params
end

Instance Method Details

#contentObject

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/gitlab/ci/config/external/file/base.rb', line 41

def content
  raise NotImplementedError, 'subclass must implement fetching raw content'
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/gitlab/ci/config/external/file/base.rb', line 56

def eql?(other)
  other.hash == hash
end

#error_messageObject



37
38
39
# File 'lib/gitlab/ci/config/external/file/base.rb', line 37

def error_message
  errors.first
end

#hashObject



60
61
62
# File 'lib/gitlab/ci/config/external/file/base.rb', line 60

def hash
  [params, context.project&.full_path, context.sha].hash
end

#invalid_extension?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/gitlab/ci/config/external/file/base.rb', line 29

def invalid_extension?
  location.nil? || !::File.basename(location).match?(YAML_WHITELIST_EXTENSION)
end

#invalid_location_type?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/gitlab/ci/config/external/file/base.rb', line 25

def invalid_location_type?
  !location.is_a?(String)
end

#load_and_validate_expanded_hash!Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gitlab/ci/config/external/file/base.rb', line 92

def load_and_validate_expanded_hash!
  return errors.push("`#{masked_location}`: #{content_result.error}") unless content_result.valid?

  if content_result.interpolated? && context.user.present?
    ::Gitlab::UsageDataCounters::HLLRedisCounter
      .track_event('ci_interpolation_users', values: context.user.id)
  end

  context.logger.instrument(:config_file_expand_content_includes) do
    expanded_content_hash # calling the method expands then memoizes the result
  end

  validate_hash!
end

#matching?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/gitlab/ci/config/external/file/base.rb', line 21

def matching?
  location.present?
end

#metadataObject



49
50
51
52
53
54
# File 'lib/gitlab/ci/config/external/file/base.rb', line 49

def 
  {
    context_project: context.project&.full_path,
    context_sha: context.sha
  }
end

#preload_contentObject



70
71
72
73
74
# File 'lib/gitlab/ci/config/external/file/base.rb', line 70

def preload_content
  # calling the `content` method either loads content into the memoized result
  # or lazily loads it via BatchLoader
  content
end

#preload_contextObject

This method is overridden to load context into the memoized result or to lazily load context via BatchLoader



66
67
68
# File 'lib/gitlab/ci/config/external/file/base.rb', line 66

def preload_context
  # no-op
end

#to_hashObject



45
46
47
# File 'lib/gitlab/ci/config/external/file/base.rb', line 45

def to_hash
  expanded_content_hash
end

#valid?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/gitlab/ci/config/external/file/base.rb', line 33

def valid?
  errors.none?
end

#validate_content!Object



88
89
90
# File 'lib/gitlab/ci/config/external/file/base.rb', line 88

def validate_content!
  errors.push("Included file `#{masked_location}` is empty or does not exist!") if content.blank?
end

#validate_context!Object

Raises:

  • (NotImplementedError)


84
85
86
# File 'lib/gitlab/ci/config/external/file/base.rb', line 84

def validate_context!
  raise NotImplementedError, 'subclass must implement `validate_context!`'
end

#validate_location!Object



76
77
78
79
80
81
82
# File 'lib/gitlab/ci/config/external/file/base.rb', line 76

def validate_location!
  if invalid_location_type?
    errors.push("Included file `#{masked_location}` needs to be a string")
  elsif invalid_extension?
    errors.push("Included file `#{masked_location}` does not have YAML extension!")
  end
end