2020-09-21 23:09:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Packages
|
|
|
|
module Generic
|
|
|
|
class CreatePackageFileService < BaseService
|
|
|
|
def execute
|
|
|
|
::Packages::Package.transaction do
|
|
|
|
create_package_file(find_or_create_package)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_or_create_package
|
|
|
|
package_params = {
|
|
|
|
name: params[:package_name],
|
|
|
|
version: params[:package_version],
|
2021-02-17 07:09:26 -05:00
|
|
|
build: params[:build],
|
|
|
|
status: params[:status]
|
2020-09-21 23:09:49 -04:00
|
|
|
}
|
|
|
|
|
2020-12-03 19:09:55 -05:00
|
|
|
package = ::Packages::Generic::FindOrCreatePackageService
|
2020-09-21 23:09:49 -04:00
|
|
|
.new(project, current_user, package_params)
|
|
|
|
.execute
|
2020-12-03 19:09:55 -05:00
|
|
|
|
2021-05-07 02:10:43 -04:00
|
|
|
unless Namespace::PackageSetting.duplicates_allowed?(package)
|
|
|
|
raise ::Packages::DuplicatePackageError if target_file_is_duplicate?(package)
|
|
|
|
end
|
|
|
|
|
2021-02-17 07:09:26 -05:00
|
|
|
package.update_column(:status, params[:status]) if params[:status] && params[:status] != package.status
|
|
|
|
|
2020-12-03 19:09:55 -05:00
|
|
|
package.build_infos.safe_find_or_create_by!(pipeline: params[:build].pipeline) if params[:build].present?
|
|
|
|
package
|
2020-09-21 23:09:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_package_file(package)
|
|
|
|
file_params = {
|
|
|
|
file: params[:file],
|
|
|
|
size: params[:file].size,
|
|
|
|
file_sha256: params[:file].sha256,
|
2020-11-16 19:09:16 -05:00
|
|
|
file_name: params[:file_name],
|
|
|
|
build: params[:build]
|
2020-09-21 23:09:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
::Packages::CreatePackageFileService.new(package, file_params).execute
|
|
|
|
end
|
2021-05-07 02:10:43 -04:00
|
|
|
|
|
|
|
def target_file_is_duplicate?(package)
|
|
|
|
package.package_files.with_file_name(params[:file_name]).exists?
|
|
|
|
end
|
2020-09-21 23:09:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|