Class: Ci::Runners::CreateRunnerService

Inherits:
Object
  • Object
show all
Defined in:
app/services/ci/runners/create_runner_service.rb

Constant Summary collapse

RUNNER_CLASS_MAPPING =
{
  'instance_type' => Ci::Runners::RunnerCreationStrategies::InstanceRunnerStrategy,
  'group_type' => Ci::Runners::RunnerCreationStrategies::GroupRunnerStrategy,
  'project_type' => Ci::Runners::RunnerCreationStrategies::ProjectRunnerStrategy
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(user:, params:) ⇒ CreateRunnerService

Returns a new instance of CreateRunnerService.



12
13
14
15
16
# File 'app/services/ci/runners/create_runner_service.rb', line 12

def initialize(user:, params:)
  @user = user
  @params = params
  @strategy = RUNNER_CLASS_MAPPING[params[:runner_type]].new(user: user, params: params)
end

Instance Method Details

#executeObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/ci/runners/create_runner_service.rb', line 18

def execute
  normalize_params

  error = strategy.validate_params
  return ServiceResponse.error(message: error, reason: :validation_error) if error

  unless strategy.authorized_user?
    return ServiceResponse.error(message: _('Insufficient permissions'), reason: :forbidden)
  end

  runner = ::Ci::Runner.new(params)

  return ServiceResponse.success(payload: { runner: runner }) if runner.save

  ServiceResponse.error(message: runner.errors.full_messages, reason: :save_error)
end

#normalize_paramsObject



35
36
37
38
39
40
41
# File 'app/services/ci/runners/create_runner_service.rb', line 35

def normalize_params
  params[:registration_type] = :authenticated_user
  params[:active] = !params.delete(:paused) if params.key?(:paused)
  params[:creator] = user

  strategy.normalize_params
end