Class: Packages::Npm::CreateTemporaryPackageService

Inherits:
CreateTemporaryPackageService show all
Defined in:
app/services/packages/npm/create_temporary_package_service.rb

Constant Summary collapse

CONTENT_TYPE =
'application/json'
ERRORS =
{
  unauthorized: ServiceResponse.error(message: 'Unauthorized', reason: :unauthorized)
}.freeze

Constants inherited from CreateTemporaryPackageService

CreateTemporaryPackageService::PACKAGE_VERSION

Constants inherited from CreatePackageService

CreatePackageService::ERROR_RESPONSE_PACKAGE_PROTECTED

Constants inherited from BaseService

BaseService::UnauthorizedError

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?, #can_all?, #can_any?

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#executeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/packages/npm/create_temporary_package_service.rb', line 12

def execute
  return ERRORS[:unauthorized] unless can_create_package?
  return ERROR_RESPONSE_PACKAGE_PROTECTED if package_protected?(package_name: name, package_type: :npm)

  package, package_file = ApplicationRecord.transaction do
    package = super(::Packages::Npm::Package, name: name)
    package_file = ::Packages::CreatePackageFileService.new(package, file_params).execute

    [package, package_file]
  end

  ::Packages::Npm::ProcessTemporaryPackageFileWorker.perform_async(
    current_user.to_global_id.to_s,
    package_file.id,
    params[:deprecate]
  )

  ServiceResponse.success(payload: { package: package })
rescue ActiveRecord::RecordInvalid => e
  reason = e.record.errors.of_kind?(:name, :taken) ? :name_taken : :invalid_parameter
  ServiceResponse.error(message: e.message, reason: reason)
end