Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
031122eb
Commit
031122eb
authored
Mar 30, 2017
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add container repository create service with specs
parent
4407d3cf
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
111 additions
and
0 deletions
+111
-0
app/models/container_repository.rb
app/models/container_repository.rb
+4
-0
app/services/container_registry/create_repository_service.rb
app/services/container_registry/create_repository_service.rb
+33
-0
spec/services/container_registry/create_repository_service_spec.rb
...ices/container_registry/create_repository_service_spec.rb
+74
-0
No files found.
app/models/container_repository.rb
View file @
031122eb
...
@@ -58,4 +58,8 @@ class ContainerRepository < ActiveRecord::Base
...
@@ -58,4 +58,8 @@ class ContainerRepository < ActiveRecord::Base
client
.
delete_repository_tag
(
self
.
path
,
digest
)
client
.
delete_repository_tag
(
self
.
path
,
digest
)
end
end
end
end
def
self
.
create_from_path
(
path
)
self
.
create
(
project:
path
.
repository_project
,
name:
path
.
repository_name
)
end
end
end
app/services/container_registry/create_repository_service.rb
0 → 100644
View file @
031122eb
module
ContainerRegistry
##
# Service for creating a container repository.
#
# It is usually executed before registry authenticator returns
# a token for given request.
#
class
CreateRepositoryService
<
BaseService
def
execute
(
path
)
@path
=
path
return
if
path
.
has_repository?
unless
user_can_create?
||
legacy_trigger_can_create?
raise
Gitlab
::
Access
::
AccessDeniedError
end
ContainerRepository
.
create_from_path
(
path
)
end
private
def
user_can_create?
can?
(
@current_user
,
:create_container_image
,
@path
.
repository_project
)
end
## TODO, remove it after removing legacy triggers.
#
def
legacy_trigger_can_create?
@current_user
.
nil?
&&
@project
==
@path
.
repository_project
end
end
end
spec/services/container_registry/create_repository_service_spec.rb
0 → 100644
View file @
031122eb
require
'spec_helper'
describe
ContainerRegistry
::
CreateRepositoryService
,
'#execute'
do
let
(
:project
)
{
create
(
:empty_project
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:path
)
do
ContainerRegistry
::
Path
.
new
(
"
#{
project
.
full_path
}
/my/image"
)
end
let
(
:service
)
{
described_class
.
new
(
project
,
user
)
}
before
do
stub_container_registry_config
(
enabled:
true
)
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 user has no ability to create a repository'
do
it
'does not create a new container repository'
do
expect
{
service
.
execute
(
path
)
}
.
to
raise_error
(
Gitlab
::
Access
::
AccessDeniedError
)
.
and
change
{
ContainerRepository
.
count
}.
by
(
0
)
end
end
context
'when user has ability do create a repository'
do
before
do
project
.
add_developer
(
user
)
end
it
'creates a new container repository'
do
expect
{
service
.
execute
(
path
)
}
.
to
change
{
project
.
container_repositories
.
count
}.
by
(
1
)
end
end
end
context
'when repository is created by a legacy pipeline trigger'
do
let
(
:user
)
{
nil
}
context
'when repository path matches authenticated project'
do
it
'creates a new container repository'
do
expect
{
service
.
execute
(
path
)
}
.
to
change
{
project
.
container_repositories
.
count
}.
by
(
1
)
end
end
context
'when repository path does not match authenticated project'
do
let
(
:private_project
)
{
create
(
:empty_project
,
:private
)
}
let
(
:path
)
do
ContainerRegistry
::
Path
.
new
(
"
#{
private_project
.
full_path
}
/my/image"
)
end
it
'does not create a new container repository'
do
expect
{
service
.
execute
(
path
)
}
.
to
raise_error
(
Gitlab
::
Access
::
AccessDeniedError
)
.
and
change
{
ContainerRepository
.
count
}.
by
(
0
)
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment