2016-01-26 11:03:38 -05:00
|
|
|
module API
|
|
|
|
# Runners API
|
|
|
|
class Runners < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
|
|
|
|
resource :runners do
|
2016-02-02 09:52:02 -05:00
|
|
|
# Get runners available for user
|
2016-01-26 11:03:38 -05:00
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# GET /runners
|
|
|
|
get do
|
2016-02-05 09:35:21 -05:00
|
|
|
runners = filter_runners(current_user.ci_authorized_runners, params[:scope], without: ['specific', 'shared'])
|
2016-02-02 09:52:02 -05:00
|
|
|
present paginate(runners), with: Entities::Runner
|
|
|
|
end
|
2016-01-26 11:03:38 -05:00
|
|
|
|
2016-02-02 09:52:02 -05:00
|
|
|
# Get all runners - shared and specific
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# GET /runners/all
|
|
|
|
get 'all' do
|
|
|
|
authenticated_as_admin!
|
|
|
|
runners = filter_runners(Ci::Runner.all, params[:scope])
|
2016-01-26 11:03:38 -05:00
|
|
|
present paginate(runners), with: Entities::Runner
|
|
|
|
end
|
|
|
|
|
2016-01-29 10:11:48 -05:00
|
|
|
# Get runner's details
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of ther runner
|
|
|
|
# Example Request:
|
|
|
|
# GET /runners/:id
|
2016-01-26 11:03:38 -05:00
|
|
|
get ':id' do
|
|
|
|
runner = get_runner(params[:id])
|
2016-02-04 05:01:16 -05:00
|
|
|
authenticate_show_runner!(runner)
|
2016-01-26 11:03:38 -05:00
|
|
|
|
2016-02-10 06:26:56 -05:00
|
|
|
present runner, with: Entities::RunnerDetails, current_user: current_user
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
|
|
|
|
2016-01-29 10:11:48 -05:00
|
|
|
# Update runner's details
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of ther runner
|
|
|
|
# description (optional) - Runner's description
|
|
|
|
# active (optional) - Runner's status
|
|
|
|
# tag_list (optional) - Array of tags for runner
|
|
|
|
# Example Request:
|
|
|
|
# PUT /runners/:id
|
2016-01-26 11:03:38 -05:00
|
|
|
put ':id' do
|
|
|
|
runner = get_runner(params[:id])
|
2016-02-04 05:01:16 -05:00
|
|
|
authenticate_update_runner!(runner)
|
2016-01-26 11:03:38 -05:00
|
|
|
|
2016-05-06 06:32:17 -04:00
|
|
|
attrs = attributes_for_keys [:description, :active, :tag_list, :run_untagged]
|
2016-01-26 11:03:38 -05:00
|
|
|
if runner.update(attrs)
|
2016-02-10 08:20:51 -05:00
|
|
|
present runner, with: Entities::RunnerDetails, current_user: current_user
|
2016-01-26 11:03:38 -05:00
|
|
|
else
|
|
|
|
render_validation_error!(runner)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-29 10:11:48 -05:00
|
|
|
# Remove runner
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of ther runner
|
|
|
|
# Example Request:
|
|
|
|
# DELETE /runners/:id
|
2016-01-26 11:03:38 -05:00
|
|
|
delete ':id' do
|
|
|
|
runner = get_runner(params[:id])
|
2016-02-04 05:01:16 -05:00
|
|
|
authenticate_delete_runner!(runner)
|
2016-01-26 11:03:38 -05:00
|
|
|
runner.destroy!
|
|
|
|
|
2016-02-10 08:20:51 -05:00
|
|
|
present runner, with: Entities::Runner
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
resource :projects do
|
|
|
|
before { authorize_admin_project }
|
|
|
|
|
|
|
|
# Get runners available for project
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/runners
|
|
|
|
get ':id/runners' do
|
|
|
|
runners = filter_runners(Ci::Runner.owned_or_shared(user_project.id), params[:scope])
|
|
|
|
present paginate(runners), with: Entities::Runner
|
|
|
|
end
|
2016-01-29 09:40:25 -05:00
|
|
|
|
2016-01-29 10:11:48 -05:00
|
|
|
# Enable runner for project
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of the project
|
|
|
|
# runner_id (required) - The ID of the runner
|
|
|
|
# Example Request:
|
2016-02-02 08:52:33 -05:00
|
|
|
# POST /projects/:id/runners/:runner_id
|
2016-02-16 06:05:48 -05:00
|
|
|
post ':id/runners' do
|
|
|
|
required_attributes! [:runner_id]
|
|
|
|
|
2016-01-29 09:40:25 -05:00
|
|
|
runner = get_runner(params[:runner_id])
|
2016-02-04 05:01:16 -05:00
|
|
|
authenticate_enable_runner!(runner)
|
2016-01-29 09:40:25 -05:00
|
|
|
Ci::RunnerProject.create(runner: runner, project: user_project)
|
|
|
|
|
|
|
|
present runner, with: Entities::Runner
|
|
|
|
end
|
|
|
|
|
2016-01-29 10:11:48 -05:00
|
|
|
# Disable project's runner
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of the project
|
|
|
|
# runner_id (required) - The ID of the runner
|
|
|
|
# Example Request:
|
|
|
|
# DELETE /projects/:id/runners/:runner_id
|
2016-01-29 09:40:25 -05:00
|
|
|
delete ':id/runners/:runner_id' do
|
|
|
|
runner_project = user_project.runner_projects.find_by(runner_id: params[:runner_id])
|
|
|
|
not_found!('Runner') unless runner_project
|
|
|
|
|
|
|
|
runner = runner_project.runner
|
2016-02-04 05:01:16 -05:00
|
|
|
forbidden!("Only one project associated with the runner. Please remove the runner instead") if runner.projects.count == 1
|
2016-01-29 09:40:25 -05:00
|
|
|
|
|
|
|
runner_project.destroy
|
|
|
|
|
|
|
|
present runner, with: Entities::Runner
|
|
|
|
end
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
helpers do
|
2016-02-05 09:35:21 -05:00
|
|
|
def filter_runners(runners, scope, options = {})
|
2016-01-26 11:03:38 -05:00
|
|
|
return runners unless scope.present?
|
|
|
|
|
|
|
|
available_scopes = ::Ci::Runner::AVAILABLE_SCOPES
|
2016-02-05 09:35:21 -05:00
|
|
|
if options[:without]
|
|
|
|
available_scopes = available_scopes - options[:without]
|
|
|
|
end
|
|
|
|
|
2016-01-29 10:44:29 -05:00
|
|
|
if (available_scopes & [scope]).empty?
|
2016-01-26 11:03:38 -05:00
|
|
|
render_api_error!('Scope contains invalid value', 400)
|
|
|
|
end
|
2016-01-29 10:44:29 -05:00
|
|
|
|
|
|
|
runners.send(scope)
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_runner(id)
|
|
|
|
runner = Ci::Runner.find(id)
|
|
|
|
not_found!('Runner') unless runner
|
|
|
|
runner
|
|
|
|
end
|
|
|
|
|
2016-02-04 05:01:16 -05:00
|
|
|
def authenticate_show_runner!(runner)
|
|
|
|
return if runner.is_shared || current_user.is_admin?
|
|
|
|
forbidden!("No access granted") unless user_can_access_runner?(runner)
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
|
|
|
|
2016-02-04 05:01:16 -05:00
|
|
|
def authenticate_update_runner!(runner)
|
|
|
|
return if current_user.is_admin?
|
|
|
|
forbidden!("Runner is shared") if runner.is_shared?
|
|
|
|
forbidden!("No access granted") unless user_can_access_runner?(runner)
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
|
|
|
|
2016-02-04 05:01:16 -05:00
|
|
|
def authenticate_delete_runner!(runner)
|
|
|
|
return if current_user.is_admin?
|
|
|
|
forbidden!("Runner is shared") if runner.is_shared?
|
|
|
|
forbidden!("Runner associated with more than one project") if runner.projects.count > 1
|
|
|
|
forbidden!("No access granted") unless user_can_access_runner?(runner)
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
|
|
|
|
2016-02-04 05:01:16 -05:00
|
|
|
def authenticate_enable_runner!(runner)
|
|
|
|
forbidden!("Runner is shared") if runner.is_shared?
|
|
|
|
return if current_user.is_admin?
|
|
|
|
forbidden!("No access granted") unless user_can_access_runner?(runner)
|
2016-01-29 09:40:25 -05:00
|
|
|
end
|
|
|
|
|
2016-01-26 11:03:38 -05:00
|
|
|
def user_can_access_runner?(runner)
|
2016-02-04 05:01:16 -05:00
|
|
|
current_user.ci_authorized_runners.exists?(runner.id)
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|