2022-02-24 04:14:06 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
|
|
|
module Runners
|
|
|
|
class AssignRunnerService
|
2022-03-07 10:52:05 -05:00
|
|
|
# @param [Ci::Runner] runner: the runner to assign to a project
|
|
|
|
# @param [Project] project: the new project to assign the runner to
|
|
|
|
# @param [User] user: the user performing the operation
|
2022-02-24 04:14:06 -05:00
|
|
|
def initialize(runner, project, user)
|
|
|
|
@runner = runner
|
|
|
|
@project = project
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2022-08-05 05:12:06 -04:00
|
|
|
unless @user.present? && @user.can?(:assign_runner, @runner)
|
2022-08-05 11:12:12 -04:00
|
|
|
return ServiceResponse.error(message: 'user not allowed to assign runner', http_status: :forbidden)
|
2022-08-05 05:12:06 -04:00
|
|
|
end
|
2022-02-24 04:14:06 -05:00
|
|
|
|
2022-08-05 05:12:06 -04:00
|
|
|
if @runner.assign_to(@project, @user)
|
|
|
|
ServiceResponse.success
|
|
|
|
else
|
|
|
|
ServiceResponse.error(message: 'failed to assign runner')
|
|
|
|
end
|
2022-02-24 04:14:06 -05:00
|
|
|
end
|
2022-03-07 10:52:05 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :runner, :project, :user
|
2022-02-24 04:14:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-03-07 10:52:05 -05:00
|
|
|
|
|
|
|
Ci::Runners::AssignRunnerService.prepend_mod
|