2020-09-03 08:08:47 -04: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-09-08 11:08:41 -04:00
|
|
|
.safe_find_or_create_by!(name: name, version: version) do |pkg|
|
|
|
|
pkg.creator = package_creator
|
2020-09-21 23:09:49 -04:00
|
|
|
yield pkg if block_given?
|
2020-09-08 11:08:41 -04:00
|
|
|
end
|
2020-09-03 08:08:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_package!(package_type, attrs = {})
|
|
|
|
project
|
|
|
|
.packages
|
|
|
|
.with_package_type(package_type)
|
|
|
|
.create!(package_attrs(attrs))
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def package_attrs(attrs)
|
|
|
|
{
|
2020-09-08 11:08:41 -04:00
|
|
|
creator: package_creator,
|
2020-09-03 08:08:47 -04:00
|
|
|
name: params[:name],
|
|
|
|
version: params[:version]
|
|
|
|
}.merge(attrs)
|
|
|
|
end
|
2020-09-08 11:08:41 -04:00
|
|
|
|
|
|
|
def package_creator
|
|
|
|
current_user if current_user.is_a?(User)
|
|
|
|
end
|
2020-09-03 08:08:47 -04:00
|
|
|
end
|
|
|
|
end
|