Commit bdc1e1b9 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Implement method matching container repository names

parent b15d9042
......@@ -3,6 +3,7 @@ module ContainerRegistry
InvalidRegistryPathError = Class.new(StandardError)
def initialize(name)
@name = name
@nodes = name.to_s.split('/')
end
......@@ -19,11 +20,18 @@ module ContainerRegistry
end
end
def has_repository?
# ContainerRepository.find_by_full_path(@name).present?
end
def repository_project
@project ||= Project.where_full_path_in(components.first(3))&.first
end
def repository_name
return unless repository_project
@name.remove(%r(^?#{Regexp.escape(repository_project.full_path)}/?))
end
end
end
......@@ -114,6 +114,49 @@ describe ContainerRegistry::Path do
end
describe '#repository_name' do
pending 'returns a correct name'
context 'when project does not exist' do
let(:name) { 'some/name' }
it 'returns nil' do
expect(path.repository_name).to be_nil
end
end
context 'when project exists' do
let(:group) { create(:group, path: 'some_group') }
let(:project) do
create(:empty_project, group: group, name: 'some_project')
end
before do
allow(path).to receive(:repository_project)
.and_return(project)
end
context 'when project path equal repository path' do
let(:name) { 'some_group/some_project' }
it 'returns an empty string' do
expect(path.repository_name).to eq ''
end
end
context 'when repository path has one additional level' do
let(:name) { 'some_group/some_project/repository' }
it 'returns a correct repository name' do
expect(path.repository_name).to eq 'repository'
end
end
context 'when repository path has two additional levels' do
let(:name) { 'some_group/some_project/repository/image' }
it 'returns a correct repository name' do
expect(path.repository_name).to eq 'repository/image'
end
end
end
end
end
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