Class: ContainerRegistry::Path
- Inherits:
-
Object
- Object
- ContainerRegistry::Path
- 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
-
#project ⇒ Object
readonly
Returns the value of attribute project.
Instance Method Summary collapse
- #components ⇒ Object
-
#has_project? ⇒ Boolean
rubocop: enable CodeReuse/ActiveRecord.
-
#has_repository? ⇒ Boolean
rubocop: disable CodeReuse/ActiveRecord.
-
#initialize(path, project: nil) ⇒ Path
constructor
The ‘project’ argument is optional.
-
#nodes ⇒ Object
rubocop: disable CodeReuse/ActiveRecord.
- #project_path ⇒ Object
- #repository_name ⇒ Object
- #repository_project ⇒ Object
-
#root_repository? ⇒ Boolean
rubocop: enable CodeReuse/ActiveRecord.
- #to_s ⇒ Object
- #valid? ⇒ Boolean
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
#project ⇒ Object (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
#components ⇒ Object
35 36 37 |
# File 'lib/container_registry/path.rb', line 35 def components @components ||= @path.split('/') end |
#has_project? ⇒ Boolean
rubocop: enable CodeReuse/ActiveRecord
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
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 |
#nodes ⇒ Object
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_path ⇒ Object
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_name ⇒ Object
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_project ⇒ Object
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
62 63 64 |
# File 'lib/container_registry/path.rb', line 62 def root_repository? @path == project_path end |
#to_s ⇒ Object
84 85 86 |
# File 'lib/container_registry/path.rb', line 84 def to_s @path end |
#valid? ⇒ 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 |