Class: ContainerRegistry::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/container_registry/path.rb

Overview

Class responsible for extracting project and repository name from image repository path provided by a containers registry API response.

Example:

some/group/my_project/my/image ->

project: some/group/my_project
repository: my/image

Constant Summary collapse

InvalidRegistryPathError =
Class.new(StandardError)
LEVELS_SUPPORTED =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, project: nil) ⇒ Path

The ‘project’ argument is optional. If provided during initialization, it will limit the path to the specified project, potentially reducing the need for a database query.



24
25
26
27
# File 'lib/container_registry/path.rb', line 24

def initialize(path, project: nil)
  @path = path.to_s.downcase
  @project = project
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



19
20
21
# File 'lib/container_registry/path.rb', line 19

def project
  @project
end

Instance Method Details

#componentsObject



35
36
37
# File 'lib/container_registry/path.rb', line 35

def components
  @components ||= @path.split('/')
end

#has_project?Boolean

rubocop: enable CodeReuse/ActiveRecord

Returns:

  • (Boolean)


49
50
51
# File 'lib/container_registry/path.rb', line 49

def has_project?
  repository_project.present?
end

#has_repository?Boolean

rubocop: disable CodeReuse/ActiveRecord

Returns:

  • (Boolean)


54
55
56
57
58
59
# File 'lib/container_registry/path.rb', line 54

def has_repository?
  return false unless has_project?

  repository_project.container_repositories
    .where(name: repository_name).any?
end

#nodesObject

rubocop: disable CodeReuse/ActiveRecord



40
41
42
43
44
45
46
# File 'lib/container_registry/path.rb', line 40

def nodes
  raise InvalidRegistryPathError unless valid?

  @nodes ||= components.size.downto(2).map do |length|
    components.take(length).join('/')
  end
end

#project_pathObject



78
79
80
81
82
# File 'lib/container_registry/path.rb', line 78

def project_path
  return unless has_project?

  repository_project.full_path.downcase
end

#repository_nameObject



72
73
74
75
76
# File 'lib/container_registry/path.rb', line 72

def repository_name
  return unless has_project?

  @path.remove(%r{^#{Regexp.escape(project_path)}/?})
end

#repository_projectObject



66
67
68
69
70
# File 'lib/container_registry/path.rb', line 66

def repository_project
  @project ||= Project
    .where_full_path_in(nodes.first(LEVELS_SUPPORTED))
    .first
end

#root_repository?Boolean

rubocop: enable CodeReuse/ActiveRecord

Returns:

  • (Boolean)


62
63
64
# File 'lib/container_registry/path.rb', line 62

def root_repository?
  @path == project_path
end

#to_sObject



84
85
86
# File 'lib/container_registry/path.rb', line 84

def to_s
  @path
end

#valid?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/container_registry/path.rb', line 29

def valid?
  @path =~ Gitlab::Regex.container_repository_name_regex &&
    components.size > 1 &&
    components.size < Namespace::NUMBER_OF_ANCESTORS_ALLOWED
end