2020-11-24 15:15:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Packages
|
|
|
|
class CreatePackageService < BaseService
|
|
|
|
protected
|
|
|
|
|
|
|
|
def find_or_create_package!(package_type, name: params[:name], version: params[:version])
|
2021-11-11 11:23:49 +05:30
|
|
|
# safe_find_or_create_by! was originally called here.
|
|
|
|
# We merely switched to `find_or_create_by!`
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2020-11-24 15:15:51 +05:30
|
|
|
project
|
|
|
|
.packages
|
|
|
|
.with_package_type(package_type)
|
2022-03-02 08:16:31 +05:30
|
|
|
.not_pending_destruction
|
2021-11-11 11:23:49 +05:30
|
|
|
.find_or_create_by!(name: name, version: version) do |package|
|
2021-03-11 19:13:27 +05:30
|
|
|
package.status = params[:status] if params[:status]
|
2021-02-22 17:27:13 +05:30
|
|
|
package.creator = package_creator
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
add_build_info(package)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
2021-11-11 11:23:49 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create_package!(package_type, attrs = {})
|
|
|
|
project
|
|
|
|
.packages
|
|
|
|
.with_package_type(package_type)
|
2021-02-22 17:27:13 +05:30
|
|
|
.create!(package_attrs(attrs)) do |package|
|
|
|
|
add_build_info(package)
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def package_attrs(attrs)
|
|
|
|
{
|
|
|
|
creator: package_creator,
|
|
|
|
name: params[:name],
|
2021-03-11 19:13:27 +05:30
|
|
|
version: params[:version],
|
|
|
|
status: params[:status]
|
|
|
|
}.compact.merge(attrs)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def package_creator
|
|
|
|
current_user if current_user.is_a?(User)
|
|
|
|
end
|
2021-02-22 17:27:13 +05:30
|
|
|
|
|
|
|
def add_build_info(package)
|
|
|
|
if params[:build].present?
|
|
|
|
package.build_infos.new(pipeline: params[:build].pipeline)
|
|
|
|
end
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
end
|