Commit 4dbaf0d6 authored by Ash McKenzie's avatar Ash McKenzie

Merge branch 'debian_create_distributionservice' into 'master'

Add Packages::Debian::CreateDistributionService

See merge request gitlab-org/gitlab!51892
parents 8a9c6a5e 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
# 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
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