Class: Packages::Maven::CreatePackageService

Inherits:
CreatePackageService show all
Defined in:
app/services/packages/maven/create_package_service.rb

Constant Summary collapse

ERROR_RESPONSE_UNAUTHORIZED =
ServiceResponse.error(message: 'Unauthorized', reason: :unauthorized)

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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/packages/maven/create_package_service.rb', line 8

def execute
  return ERROR_RESPONSE_UNAUTHORIZED unless can_create_package?
  return ERROR_RESPONSE_PACKAGE_PROTECTED if package_protected?(package_name: params[:name], package_type: :maven)

  app_group, _, app_name = params[:name].rpartition('/')
  app_group.tr!('/', '.')

  package = create_package!(
    ::Packages::Maven::Package,
    maven_metadatum_attributes: {
      path: params[:path],
      app_group: app_group,
      app_name: app_name,
      app_version: params[:version]
    }
  )

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

  ServiceResponse.error(message: e.message, reason: reason)
end