2020-09-03 12:08:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Packages
|
|
|
|
class CreatePackageService < BaseService
|
|
|
|
protected
|
|
|
|
|
|
|
|
def find_or_create_package!(package_type, name: params[:name], version: params[:version])
|
|
|
|
project
|
|
|
|
.packages
|
|
|
|
.with_package_type(package_type)
|
2020-12-04 00:09:55 +00:00
|
|
|
.safe_find_or_create_by!(name: name, version: version) do |package|
|
2021-02-17 12:09:26 +00:00
|
|
|
package.status = params[:status] if params[:status]
|
2020-12-04 00:09:55 +00:00
|
|
|
package.creator = package_creator
|
2021-02-17 12:09:26 +00:00
|
|
|
|
2020-12-04 00:09:55 +00:00
|
|
|
add_build_info(package)
|
2020-09-08 15:08:41 +00:00
|
|
|
end
|
2020-09-03 12:08:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_package!(package_type, attrs = {})
|
|
|
|
project
|
|
|
|
.packages
|
|
|
|
.with_package_type(package_type)
|
2020-12-04 00:09:55 +00:00
|
|
|
.create!(package_attrs(attrs)) do |package|
|
|
|
|
add_build_info(package)
|
|
|
|
end
|
2020-09-03 12:08:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def package_attrs(attrs)
|
|
|
|
{
|
2020-09-08 15:08:41 +00:00
|
|
|
creator: package_creator,
|
2020-09-03 12:08:47 +00:00
|
|
|
name: params[:name],
|
2021-02-17 12:09:26 +00:00
|
|
|
version: params[:version],
|
|
|
|
status: params[:status]
|
|
|
|
}.compact.merge(attrs)
|
2020-09-03 12:08:47 +00:00
|
|
|
end
|
2020-09-08 15:08:41 +00:00
|
|
|
|
|
|
|
def package_creator
|
|
|
|
current_user if current_user.is_a?(User)
|
|
|
|
end
|
2020-12-04 00:09:55 +00:00
|
|
|
|
|
|
|
def add_build_info(package)
|
|
|
|
if params[:build].present?
|
|
|
|
package.build_infos.new(pipeline: params[:build].pipeline)
|
|
|
|
end
|
|
|
|
end
|
2020-09-03 12:08:47 +00:00
|
|
|
end
|
|
|
|
end
|