Commit dd6983a5 authored by Pedro Pombeiro's avatar Pedro Pombeiro

Extract AssignRunnerService

parent 0175304d
...@@ -8,7 +8,7 @@ class Admin::RunnerProjectsController < Admin::ApplicationController ...@@ -8,7 +8,7 @@ class Admin::RunnerProjectsController < Admin::ApplicationController
def create def create
@runner = Ci::Runner.find(params[:runner_project][:runner_id]) @runner = Ci::Runner.find(params[:runner_project][:runner_id])
if @runner.assign_to(@project, current_user) if ::Ci::AssignRunnerService.new(@runner, @project, current_user).execute
redirect_to edit_admin_runner_url(@runner), notice: s_('Runners|Runner assigned to project.') redirect_to edit_admin_runner_url(@runner), notice: s_('Runners|Runner assigned to project.')
else else
redirect_to edit_admin_runner_url(@runner), alert: 'Failed adding runner to project' redirect_to edit_admin_runner_url(@runner), alert: 'Failed adding runner to project'
......
...@@ -14,7 +14,7 @@ class Projects::RunnerProjectsController < Projects::ApplicationController ...@@ -14,7 +14,7 @@ class Projects::RunnerProjectsController < Projects::ApplicationController
path = project_runners_path(project) path = project_runners_path(project)
if @runner.assign_to(project, current_user) if ::Ci::AssignRunnerService.new(@runner, @project, current_user).execute
redirect_to path, notice: s_('Runners|Runner assigned to project.') redirect_to path, notice: s_('Runners|Runner assigned to project.')
else else
assign_to_messages = @runner.errors.messages[:assign_to] assign_to_messages = @runner.errors.messages[:assign_to]
......
# frozen_string_literal: true
module Ci
class AssignRunnerService
# @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
def initialize(runner, project, user)
@runner = runner
@project = project
@user = user
end
def execute
return false unless @user.nil? || @user.can?(:assign_runner, @runner)
@runner.assign_to(@project, @user)
end
end
end
...@@ -187,7 +187,7 @@ module API ...@@ -187,7 +187,7 @@ module API
runner = get_runner(params[:runner_id]) runner = get_runner(params[:runner_id])
authenticate_enable_runner!(runner) authenticate_enable_runner!(runner)
if runner.assign_to(user_project) if ::Ci::AssignRunnerService.new(runner, user_project, current_user).execute
present runner, with: Entities::Ci::Runner present runner, with: Entities::Ci::Runner
else else
render_validation_error!(runner) render_validation_error!(runner)
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ::Ci::AssignRunnerService, '#execute' do
subject { described_class.new(runner, project, user).execute }
let_it_be(:runner) { build(:ci_runner) }
let_it_be(:project) { build(:project) }
context 'without user' do
let(:user) { nil }
it 'calls assign_to on runner and returns value unchanged' do
expect(runner).to receive(:assign_to).with(project, nil).once.and_return('assign_to return value')
is_expected.to eq('assign_to return value')
end
end
context 'with unauthorized user' do
let(:user) { build(:user) }
it 'does not call assign_to on runner and returns false' do
expect(runner).not_to receive(:assign_to)
is_expected.to eq(false)
end
end
context 'with admin user', :enable_admin_mode do
let(:user) { create_default(:user, :admin) }
it 'calls assign_to on runner and returns value unchanged' do
expect(runner).to receive(:assign_to).with(project, user).once.and_return('assign_to return value')
is_expected.to eq('assign_to return value')
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