diff options
Diffstat (limited to 'app/services/packages')
6 files changed, 210 insertions, 9 deletions
diff --git a/app/services/packages/create_package_service.rb b/app/services/packages/create_package_service.rb index fcf252cf971..3dc06497d9f 100644 --- a/app/services/packages/create_package_service.rb +++ b/app/services/packages/create_package_service.rb @@ -9,7 +9,9 @@ module Packages .packages .with_package_type(package_type) .safe_find_or_create_by!(name: name, version: version) do |package| + package.status = params[:status] if params[:status] package.creator = package_creator + add_build_info(package) end end @@ -29,8 +31,9 @@ module Packages { creator: package_creator, name: params[:name], - version: params[:version] - }.merge(attrs) + version: params[:version], + status: params[:status] + }.compact.merge(attrs) end def package_creator diff --git a/app/services/packages/debian/create_distribution_service.rb b/app/services/packages/debian/create_distribution_service.rb new file mode 100644 index 00000000000..c6df033e3c1 --- /dev/null +++ b/app/services/packages/debian/create_distribution_service.rb @@ -0,0 +1,75 @@ +# 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 diff --git a/app/services/packages/debian/destroy_distribution_service.rb b/app/services/packages/debian/destroy_distribution_service.rb new file mode 100644 index 00000000000..bef1127fece --- /dev/null +++ b/app/services/packages/debian/destroy_distribution_service.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Packages + module Debian + class DestroyDistributionService + def initialize(distribution) + @distribution = distribution + end + + def execute + destroy_distribution + end + + private + + def destroy_distribution + if @distribution.destroy + success + else + error("Unable to destroy Debian #{@distribution.model_name.human.downcase}") + end + end + + def success + ServiceResponse.success + end + + def error(message) + ServiceResponse.error(message: message, payload: { distribution: @distribution }) + end + end + end +end diff --git a/app/services/packages/debian/update_distribution_service.rb b/app/services/packages/debian/update_distribution_service.rb new file mode 100644 index 00000000000..5bb59b854e9 --- /dev/null +++ b/app/services/packages/debian/update_distribution_service.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +module Packages + module Debian + class UpdateDistributionService + def initialize(distribution, params) + @distribution, @params = distribution, params + + @components = params.delete(:components) + + @architectures = params.delete(:architectures) + @architectures += ['all'] unless @architectures.nil? + + @errors = [] + end + + def execute + update_distribution + end + + private + + attr_reader :distribution, :params, :components, :architectures, :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 update_distribution + distribution.transaction do + if distribution.update(params) + update_components if components + update_architectures if architectures + + success + else + append_errors(distribution) + error + end + end || error + end + + def update_components + update_objects(distribution.components, components, error_label: 'Component') + end + + def update_architectures + update_objects(distribution.architectures, architectures, error_label: 'Architecture') + end + + def update_objects(objects, object_names_from_params, error_label: ) + current_object_names = objects.map(&:name) + missing_object_names = object_names_from_params - current_object_names + extra_object_names = current_object_names - object_names_from_params + + missing_object_names.each do |name| + new_object = objects.create(name: name) + append_errors(new_object, error_label) + raise ActiveRecord::Rollback unless new_object.persisted? + end + + extra_object_names.each do |name| + object = objects.with_name(name).first + raise ActiveRecord::Rollback unless object.destroy + end + end + + def success + ServiceResponse.success(payload: { distribution: distribution }) + end + + def error + ServiceResponse.error(message: errors.to_sentence, payload: { distribution: distribution }) + end + end + end +end diff --git a/app/services/packages/generic/create_package_file_service.rb b/app/services/packages/generic/create_package_file_service.rb index b14b1c193ec..1451a022a39 100644 --- a/app/services/packages/generic/create_package_file_service.rb +++ b/app/services/packages/generic/create_package_file_service.rb @@ -15,13 +15,16 @@ module Packages package_params = { name: params[:package_name], version: params[:package_version], - build: params[:build] + build: params[:build], + status: params[:status] } package = ::Packages::Generic::FindOrCreatePackageService .new(project, current_user, package_params) .execute + package.update_column(:status, params[:status]) if params[:status] && params[:status] != package.status + package.build_infos.safe_find_or_create_by!(pipeline: params[:build].pipeline) if params[:build].present? package end diff --git a/app/services/packages/maven/find_or_create_package_service.rb b/app/services/packages/maven/find_or_create_package_service.rb index 8ee449cbfdc..4c916d264a7 100644 --- a/app/services/packages/maven/find_or_create_package_service.rb +++ b/app/services/packages/maven/find_or_create_package_service.rb @@ -11,12 +11,7 @@ module Packages .execute unless Namespace::PackageSetting.duplicates_allowed?(package) - files = package&.package_files || [] - current_maven_files = files.map { |file| extname(file.file_name) } - - if current_maven_files.compact.include?(extname(params[:file_name])) - return ServiceResponse.error(message: 'Duplicate package is not allowed') - end + return ServiceResponse.error(message: 'Duplicate package is not allowed') if target_package_is_duplicate?(package) end unless package @@ -47,6 +42,7 @@ module Packages package_params = { name: package_name, path: params[:path], + status: params[:status], version: version } @@ -67,6 +63,17 @@ module Packages File.extname(filename) end + + def target_package_is_duplicate?(package) + # duplicate metadata files can be uploaded multiple times + return false if package.version.nil? + + package + .package_files + .map { |file| extname(file.file_name) } + .compact + .include?(extname(params[:file_name])) + end end end end |