Commit e10dae3e authored by Grzegorz Bizon's avatar Grzegorz Bizon

Improve code in container repository path class

parent 1a47986b
...@@ -14,24 +14,23 @@ module ContainerRegistry ...@@ -14,24 +14,23 @@ module ContainerRegistry
def initialize(path) def initialize(path)
@path = path @path = path
@nodes = path.to_s.split('/')
end
def to_s
@path
end end
def valid? def valid?
@path =~ Gitlab::Regex.container_repository_name_regex && @path =~ Gitlab::Regex.container_repository_name_regex &&
@nodes.size > 1 && nodes.size > 1 &&
@nodes.size < Namespace::NUMBER_OF_ANCESTORS_ALLOWED nodes.size < Namespace::NUMBER_OF_ANCESTORS_ALLOWED
end
def nodes
@nodes ||= @path.to_s.split('/')
end end
def components def components
raise InvalidRegistryPathError unless valid? raise InvalidRegistryPathError unless valid?
@components ||= @nodes.size.downto(2).map do |length| @components ||= nodes.size.downto(2).map do |length|
@nodes.take(length).join('/') nodes.take(length).join('/')
end end
end end
...@@ -51,7 +50,7 @@ module ContainerRegistry ...@@ -51,7 +50,7 @@ module ContainerRegistry
end end
def repository_project def repository_project
@project ||= Project.where_full_path_in(components.first(3))&.first @project ||= Project.where_full_path_in(components.first(3)).first
end end
def repository_name def repository_name
...@@ -59,5 +58,9 @@ module ContainerRegistry ...@@ -59,5 +58,9 @@ module ContainerRegistry
@path.remove(%r(^?#{Regexp.escape(repository_project.full_path)}/?)) @path.remove(%r(^?#{Regexp.escape(repository_project.full_path)}/?))
end end
def to_s
@path
end
end end
end end
...@@ -3,6 +3,14 @@ require 'spec_helper' ...@@ -3,6 +3,14 @@ require 'spec_helper'
describe ContainerRegistry::Path do describe ContainerRegistry::Path do
subject { described_class.new(path) } subject { described_class.new(path) }
describe '#nodes' do
let(:path) { 'path/to/some/project' }
it 'splits elements by a forward slash' do
expect(subject.nodes).to eq %w[path to some project]
end
end
describe '#components' do describe '#components' do
context 'when repository path is valid' do context 'when repository path is valid' do
let(:path) { 'path/to/some/project' } let(:path) { 'path/to/some/project' }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment