Commit 003a51d1 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Check container repository exists for a given path

parent 031122eb
...@@ -33,8 +33,15 @@ module ContainerRegistry ...@@ -33,8 +33,15 @@ module ContainerRegistry
end end
end end
def has_project?
repository_project.present?
end
def has_repository? def has_repository?
# ContainerRepository.find_by_full_path(@path).present? return false unless has_project?
repository_project.container_repositories
.where(name: repository_name).any?
end end
def repository_project def repository_project
...@@ -42,7 +49,7 @@ module ContainerRegistry ...@@ -42,7 +49,7 @@ module ContainerRegistry
end end
def repository_name def repository_name
return unless repository_project return unless has_project?
@path.remove(%r(^?#{Regexp.escape(repository_project.full_path)}/?)) @path.remove(%r(^?#{Regexp.escape(repository_project.full_path)}/?))
end end
......
...@@ -58,6 +58,34 @@ describe ContainerRegistry::Path do ...@@ -58,6 +58,34 @@ describe ContainerRegistry::Path do
end end
end end
describe '#has_repository?' do
context 'when project exists' do
let(:project) { create(:empty_project) }
let(:path) { "#{project.full_path}/my/image" }
context 'when path already has matching repository' do
before do
create(:container_repository, project: project, name: 'my/image')
end
it { is_expected.to have_repository }
it { is_expected.to have_project }
end
context 'when path does not have matching repository' do
it { is_expected.not_to have_repository }
it { is_expected.to have_project }
end
end
context 'when project does not exist' do
let(:path) { 'some/project/my/image' }
it { is_expected.not_to have_repository }
it { is_expected.not_to have_project }
end
end
describe '#repository_project' do describe '#repository_project' do
let(:group) { create(:group, path: 'some_group') } let(:group) { create(:group, path: 'some_group') }
......
...@@ -14,18 +14,6 @@ describe ContainerRegistry::CreateRepositoryService, '#execute' do ...@@ -14,18 +14,6 @@ describe ContainerRegistry::CreateRepositoryService, '#execute' do
stub_container_registry_config(enabled: true) stub_container_registry_config(enabled: true)
end end
context 'when container repository already exists' do
before do
create(:container_repository, project: project, name: 'my/image')
end
it 'does not create container repository again' do
expect { service.execute(path) }
.to raise_error(Gitlab::Access::AccessDeniedError)
.and change { ContainerRepository.count }.by(0)
end
end
context 'when repository is created by an user' do context 'when repository is created by an user' do
context 'when user has no ability to create a repository' do context 'when user has no ability to create a repository' do
it 'does not create a new container repository' do it 'does not create a new container repository' do
...@@ -40,12 +28,25 @@ describe ContainerRegistry::CreateRepositoryService, '#execute' do ...@@ -40,12 +28,25 @@ describe ContainerRegistry::CreateRepositoryService, '#execute' do
project.add_developer(user) project.add_developer(user)
end end
context 'when repository already exists' do
before do
create(:container_repository, project: project, name: 'my/image')
end
it 'does not create container repository again' do
expect { service.execute(path) }
.to_not change { ContainerRepository.count }
end
end
context 'when repository does not exist yet' do
it 'creates a new container repository' do it 'creates a new container repository' do
expect { service.execute(path) } expect { service.execute(path) }
.to change { project.container_repositories.count }.by(1) .to change { project.container_repositories.count }.by(1)
end end
end end
end end
end
context 'when repository is created by a legacy pipeline trigger' do context 'when repository is created by a legacy pipeline trigger' do
let(:user) { nil } let(:user) { nil }
......
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