Commit 6d3a8f08 authored by Giorgenes Gelatti's avatar Giorgenes Gelatti

Refactor package object creation

Create a base service for package creation
to centralize package creation.
parent 146c43c9
......@@ -2,7 +2,7 @@
module Packages
module Composer
class CreatePackageService < BaseService
class CreatePackageService < ::Packages::CreatePackageService
include ::Gitlab::Utils::StrongMemoize
def execute
......@@ -21,10 +21,7 @@ module Packages
private
def created_package
project
.packages
.composer
.safe_find_or_create_by!(name: package_name, version: package_version)
find_or_create_package!(:composer, name: package_name, version: package_version)
end
def composer_json
......
......@@ -2,12 +2,11 @@
module Packages
module Conan
class CreatePackageService < BaseService
class CreatePackageService < ::Packages::CreatePackageService
def execute
project.packages.create!(
create_package!(:conan,
name: params[:package_name],
version: params[:package_version],
package_type: :conan,
conan_metadatum_attributes: {
package_username: params[:package_username],
package_channel: params[:package_channel]
......
# 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)
.safe_find_or_create_by!(name: name, version: version)
end
def create_package!(package_type, attrs = {})
project
.packages
.with_package_type(package_type)
.create!(package_attrs(attrs))
end
private
def package_attrs(attrs)
{
name: params[:name],
version: params[:version]
}.merge(attrs)
end
end
end
# frozen_string_literal: true
module Packages
module Maven
class CreatePackageService < BaseService
class CreatePackageService < ::Packages::CreatePackageService
def execute
app_group, _, app_name = params[:name].rpartition('/')
app_group.tr!('/', '.')
package = project.packages.create!(
name: params[:name],
version: params[:version],
package_type: :maven,
package = create_package!(:maven,
maven_metadatum_attributes: {
path: params[:path],
app_group: app_group,
......
# frozen_string_literal: true
module Packages
module Npm
class CreatePackageService < BaseService
class CreatePackageService < ::Packages::CreatePackageService
include Gitlab::Utils::StrongMemoize
def execute
......@@ -9,17 +9,13 @@ module Packages
return error('Package already exists.', 403) if current_package_exists?
return error('File is too large.', 400) if file_size_exceeded?
ActiveRecord::Base.transaction { create_package! }
ActiveRecord::Base.transaction { create_npm_package! }
end
private
def create_package!
package = project.packages.create!(
name: name,
version: version,
package_type: 'npm'
)
def create_npm_package!
package = create_package!(:npm, name: name, version: version)
if build.present?
package.create_build_info!(pipeline: build.pipeline)
......
......@@ -2,12 +2,12 @@
module Packages
module Nuget
class CreatePackageService < BaseService
class CreatePackageService < ::Packages::CreatePackageService
TEMPORARY_PACKAGE_NAME = 'NuGet.Temporary.Package'
PACKAGE_VERSION = '0.0.0'
def execute
project.packages.nuget.create!(
create_package!(:nuget,
name: TEMPORARY_PACKAGE_NAME,
version: "#{PACKAGE_VERSION}-#{uuid}"
)
......
......@@ -2,7 +2,7 @@
module Packages
module Pypi
class CreatePackageService < BaseService
class CreatePackageService < ::Packages::CreatePackageService
include ::Gitlab::Utils::StrongMemoize
def execute
......@@ -20,10 +20,7 @@ module Packages
def created_package
strong_memoize(:created_package) do
project
.packages
.pypi
.safe_find_or_create_by!(name: params[:name], version: params[:version])
find_or_create_package!(:pypi)
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment