Commit ad728ffc authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '205578-refactor-package-creation' into 'master'

Refactor package creation

See merge request gitlab-org/gitlab!41028
parents ab8edd16 6d3a8f08
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
module Packages module Packages
module Composer module Composer
class CreatePackageService < BaseService class CreatePackageService < ::Packages::CreatePackageService
include ::Gitlab::Utils::StrongMemoize include ::Gitlab::Utils::StrongMemoize
def execute def execute
...@@ -21,10 +21,7 @@ module Packages ...@@ -21,10 +21,7 @@ module Packages
private private
def created_package def created_package
project find_or_create_package!(:composer, name: package_name, version: package_version)
.packages
.composer
.safe_find_or_create_by!(name: package_name, version: package_version)
end end
def composer_json def composer_json
......
...@@ -2,12 +2,11 @@ ...@@ -2,12 +2,11 @@
module Packages module Packages
module Conan module Conan
class CreatePackageService < BaseService class CreatePackageService < ::Packages::CreatePackageService
def execute def execute
project.packages.create!( create_package!(:conan,
name: params[:package_name], name: params[:package_name],
version: params[:package_version], version: params[:package_version],
package_type: :conan,
conan_metadatum_attributes: { conan_metadatum_attributes: {
package_username: params[:package_username], package_username: params[:package_username],
package_channel: params[:package_channel] 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 # frozen_string_literal: true
module Packages module Packages
module Maven module Maven
class CreatePackageService < BaseService class CreatePackageService < ::Packages::CreatePackageService
def execute def execute
app_group, _, app_name = params[:name].rpartition('/') app_group, _, app_name = params[:name].rpartition('/')
app_group.tr!('/', '.') app_group.tr!('/', '.')
package = project.packages.create!( package = create_package!(:maven,
name: params[:name],
version: params[:version],
package_type: :maven,
maven_metadatum_attributes: { maven_metadatum_attributes: {
path: params[:path], path: params[:path],
app_group: app_group, app_group: app_group,
......
# frozen_string_literal: true # frozen_string_literal: true
module Packages module Packages
module Npm module Npm
class CreatePackageService < BaseService class CreatePackageService < ::Packages::CreatePackageService
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
def execute def execute
...@@ -9,17 +9,13 @@ module Packages ...@@ -9,17 +9,13 @@ module Packages
return error('Package already exists.', 403) if current_package_exists? return error('Package already exists.', 403) if current_package_exists?
return error('File is too large.', 400) if file_size_exceeded? return error('File is too large.', 400) if file_size_exceeded?
ActiveRecord::Base.transaction { create_package! } ActiveRecord::Base.transaction { create_npm_package! }
end end
private private
def create_package! def create_npm_package!
package = project.packages.create!( package = create_package!(:npm, name: name, version: version)
name: name,
version: version,
package_type: 'npm'
)
if build.present? if build.present?
package.create_build_info!(pipeline: build.pipeline) package.create_build_info!(pipeline: build.pipeline)
......
...@@ -2,12 +2,12 @@ ...@@ -2,12 +2,12 @@
module Packages module Packages
module Nuget module Nuget
class CreatePackageService < BaseService class CreatePackageService < ::Packages::CreatePackageService
TEMPORARY_PACKAGE_NAME = 'NuGet.Temporary.Package' TEMPORARY_PACKAGE_NAME = 'NuGet.Temporary.Package'
PACKAGE_VERSION = '0.0.0' PACKAGE_VERSION = '0.0.0'
def execute def execute
project.packages.nuget.create!( create_package!(:nuget,
name: TEMPORARY_PACKAGE_NAME, name: TEMPORARY_PACKAGE_NAME,
version: "#{PACKAGE_VERSION}-#{uuid}" version: "#{PACKAGE_VERSION}-#{uuid}"
) )
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
module Packages module Packages
module Pypi module Pypi
class CreatePackageService < BaseService class CreatePackageService < ::Packages::CreatePackageService
include ::Gitlab::Utils::StrongMemoize include ::Gitlab::Utils::StrongMemoize
def execute def execute
...@@ -20,10 +20,7 @@ module Packages ...@@ -20,10 +20,7 @@ module Packages
def created_package def created_package
strong_memoize(:created_package) do strong_memoize(:created_package) do
project find_or_create_package!(:pypi)
.packages
.pypi
.safe_find_or_create_by!(name: params[:name], version: params[:version])
end end
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