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 Method Summary collapse

Constructor Details

#initialize(path) ⇒ Path

Returns a new instance of Path.


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

def initialize(path)
  @path = path.to_s.downcase
end

Instance Method Details

#componentsObject


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

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

#has_project?Boolean

rubocop: enable CodeReuse/ActiveRecord

Returns:

  • (Boolean)

43
44
45
# File 'lib/container_registry/path.rb', line 43

def has_project?
  repository_project.present?
end

#has_repository?Boolean

rubocop: disable CodeReuse/ActiveRecord

Returns:

  • (Boolean)

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

def has_repository?
  return false unless has_project?

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

#nodesObject

rubocop: disable CodeReuse/ActiveRecord


34
35
36
37
38
39
40
# File 'lib/container_registry/path.rb', line 34

def nodes
  raise InvalidRegistryPathError unless valid?

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

#project_pathObject


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

def project_path
  return unless has_project?

  repository_project.full_path.downcase
end

#repository_nameObject


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

def repository_name
  return unless has_project?

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

#repository_projectObject


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

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

#root_repository?Boolean

rubocop: enable CodeReuse/ActiveRecord

Returns:

  • (Boolean)

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

def root_repository?
  @path == project_path
end

#to_sObject


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

def to_s
  @path
end

#valid?Boolean

Returns:

  • (Boolean)

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

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