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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
1d760ba9
Commit
1d760ba9
authored
Jan 10, 2021
by
Mathieu Parent
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Packages::Debian::CreateDistributionService
parent
035285ba
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
193 additions
and
0 deletions
+193
-0
app/services/packages/debian/create_distribution_service.rb
app/services/packages/debian/create_distribution_service.rb
+75
-0
spec/services/packages/debian/create_distribution_service_spec.rb
...vices/packages/debian/create_distribution_service_spec.rb
+118
-0
No files found.
app/services/packages/debian/create_distribution_service.rb
0 → 100644
View file @
1d760ba9
# frozen_string_literal: true
module
Packages
module
Debian
class
CreateDistributionService
def
initialize
(
container
,
user
,
params
)
@container
,
@params
=
container
,
params
@params
[
:creator
]
=
user
@components
=
params
.
delete
(
:components
)
||
[
'main'
]
@architectures
=
params
.
delete
(
:architectures
)
||
[
'amd64'
]
@architectures
+=
[
'all'
]
@distribution
=
nil
@errors
=
[]
end
def
execute
create_distribution
end
private
attr_reader
:container
,
:params
,
:components
,
:architectures
,
:distribution
,
:errors
def
append_errors
(
record
,
prefix
=
''
)
return
if
record
.
valid?
prefix
=
"
#{
prefix
}
"
unless
prefix
.
empty?
@errors
+=
record
.
errors
.
full_messages
.
map
{
|
message
|
"
#{
prefix
}#{
message
}
"
}
end
def
create_distribution
@distribution
=
container
.
debian_distributions
.
new
(
params
)
append_errors
(
distribution
)
return
error
unless
errors
.
empty?
distribution
.
transaction
do
if
distribution
.
save
create_components
create_architectures
success
end
end
||
error
end
def
create_components
create_objects
(
distribution
.
components
,
components
,
error_label:
'Component'
)
end
def
create_architectures
create_objects
(
distribution
.
architectures
,
architectures
,
error_label:
'Architecture'
)
end
def
create_objects
(
objects
,
object_names_from_params
,
error_label:
)
object_names_from_params
.
each
do
|
name
|
new_object
=
objects
.
create
(
name:
name
)
append_errors
(
new_object
,
error_label
)
raise
ActiveRecord
::
Rollback
unless
new_object
.
persisted?
end
end
def
success
ServiceResponse
.
success
(
payload:
{
distribution:
distribution
},
http_status: :created
)
end
def
error
ServiceResponse
.
error
(
message:
errors
.
to_sentence
,
payload:
{
distribution:
distribution
})
end
end
end
end
spec/services/packages/debian/create_distribution_service_spec.rb
0 → 100644
View file @
1d760ba9
# frozen_string_literal: true
require
'spec_helper'
RSpec
.
describe
Packages
::
Debian
::
CreateDistributionService
do
RSpec
.
shared_examples
'Create Debian Distribution'
do
|
expected_message
,
expected_components
,
expected_architectures
|
it
'returns ServiceResponse'
,
:aggregate_failures
do
if
expected_message
.
nil?
expect
{
response
}
.
to
change
{
container
.
debian_distributions
.
klass
.
all
.
count
}
.
from
(
0
).
to
(
1
)
.
and
change
{
container
.
debian_distributions
.
count
}
.
from
(
0
).
to
(
1
)
.
and
change
{
container
.
debian_distributions
.
first
&
.
components
&
.
count
}
.
from
(
nil
).
to
(
expected_components
.
count
)
.
and
change
{
container
.
debian_distributions
.
first
&
.
architectures
&
.
count
}
.
from
(
nil
).
to
(
expected_architectures
.
count
)
else
expect
{
response
}
.
to
not_change
{
container
.
debian_distributions
.
klass
.
all
.
count
}
.
and
not_change
{
container
.
debian_distributions
.
count
}
.
and
not_change
{
Packages
::
Debian
::
ProjectComponent
.
count
}
.
and
not_change
{
Packages
::
Debian
::
GroupComponent
.
count
}
.
and
not_change
{
Packages
::
Debian
::
ProjectArchitecture
.
count
}
.
and
not_change
{
Packages
::
Debian
::
GroupArchitecture
.
count
}
end
expect
(
response
).
to
be_a
(
ServiceResponse
)
expect
(
response
.
success?
).
to
eq
(
expected_message
.
nil?
)
expect
(
response
.
error?
).
to
eq
(
!
expected_message
.
nil?
)
expect
(
response
.
message
).
to
eq
(
expected_message
)
distribution
=
response
.
payload
[
:distribution
]
expect
(
distribution
.
persisted?
).
to
eq
(
expected_message
.
nil?
)
expect
(
distribution
.
container
).
to
eq
(
container
)
expect
(
distribution
.
creator
).
to
eq
(
user
)
params
.
each_pair
do
|
name
,
value
|
expect
(
distribution
.
send
(
name
)).
to
eq
(
value
)
end
expect
(
distribution
.
components
.
map
(
&
:name
)).
to
contain_exactly
(
*
expected_components
)
expect
(
distribution
.
architectures
.
map
(
&
:name
)).
to
contain_exactly
(
*
expected_architectures
)
end
end
shared_examples
'Debian Create Distribution Service'
do
context
'with only the codename param'
do
let
(
:params
)
{
{
codename:
'my-codename'
}
}
it_behaves_like
'Create Debian Distribution'
,
nil
,
%w[main]
,
%w[all amd64]
end
context
'with codename, components and architectures'
do
let
(
:params
)
do
{
codename:
'my-codename'
,
components:
%w[contrib non-free]
,
architectures:
%w[arm64]
}
end
it_behaves_like
'Create Debian Distribution'
,
nil
,
%w[contrib non-free]
,
%w[all arm64]
end
context
'with invalid suite'
do
let
(
:params
)
do
{
codename:
'my-codename'
,
suite:
'erroné'
}
end
it_behaves_like
'Create Debian Distribution'
,
'Suite is invalid'
,
%w[]
,
%w[]
end
context
'with invalid component name'
do
let
(
:params
)
do
{
codename:
'my-codename'
,
components:
%w[before erroné after]
,
architectures:
%w[arm64]
}
end
it_behaves_like
'Create Debian Distribution'
,
'Component Name is invalid'
,
%w[before erroné]
,
%w[]
end
context
'with invalid architecture name'
do
let
(
:params
)
do
{
codename:
'my-codename'
,
components:
%w[contrib non-free]
,
architectures:
%w[before erroné after']
}
end
it_behaves_like
'Create Debian Distribution'
,
'Architecture Name is invalid'
,
%w[contrib non-free]
,
%w[before erroné]
end
end
let_it_be
(
:user
)
{
create
(
:user
)
}
subject
{
described_class
.
new
(
container
,
user
,
params
)
}
let
(
:response
)
{
subject
.
execute
}
context
'within a projet'
do
let_it_be
(
:container
)
{
create
(
:project
)
}
it_behaves_like
'Debian Create Distribution Service'
end
context
'within a group'
do
let_it_be
(
:container
)
{
create
(
:group
)
}
it_behaves_like
'Debian Create Distribution Service'
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