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, Local, Project, Remote, Template

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

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)

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

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


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

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

#matching?Boolean

Returns:

  • (Boolean)

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

def matching?
  location.present?
end

#metadataObject


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

def 
  {
    context_project: context.project&.full_path,
    context_sha: context.sha
  }
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!Object


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

def validate!
  context.logger.instrument(:config_file_validation) do
    validate_execution_time!
    validate_location!
    validate_content! if errors.none?
    validate_hash! if errors.none?
  end
end