gitlab-org--gitlab-foss/lib/api/runners.rb

220 lines
7.3 KiB
Ruby
Raw Normal View History

module API
class Runners < Grape::API
2016-12-04 12:11:19 -05:00
include PaginationParams
before { authenticate! }
resource :runners do
2016-11-09 10:29:07 -05:00
desc 'Get runners available for user' do
success Entities::Runner
end
params do
2018-06-07 11:43:23 -04:00
optional :scope, type: String, values: Ci::Runner::AVAILABLE_STATUSES,
2016-11-09 10:29:07 -05:00
desc: 'The scope of specific runners to show'
2016-12-04 12:11:19 -05:00
use :pagination
2016-11-09 10:29:07 -05:00
end
get do
runners = filter_runners(current_user.ci_owned_runners, params[:scope], allowed_scopes: Ci::Runner::AVAILABLE_STATUSES)
present paginate(runners), with: Entities::Runner
end
2016-11-09 10:29:07 -05:00
desc 'Get all runners - shared and specific' do
success Entities::Runner
end
params do
2018-06-07 11:43:23 -04:00
optional :scope, type: String, values: Ci::Runner::AVAILABLE_SCOPES,
2016-11-09 10:29:07 -05:00
desc: 'The scope of specific runners to show'
2016-12-04 12:11:19 -05:00
use :pagination
2016-11-09 10:29:07 -05:00
end
get 'all' do
authenticated_as_admin!
runners = filter_runners(Ci::Runner.all, params[:scope])
present paginate(runners), with: Entities::Runner
end
2016-11-09 10:29:07 -05:00
desc "Get runner's details" do
success Entities::RunnerDetails
end
params do
requires :id, type: Integer, desc: 'The ID of the runner'
end
get ':id' do
runner = get_runner(params[:id])
authenticate_show_runner!(runner)
present runner, with: Entities::RunnerDetails, current_user: current_user
end
2016-11-09 10:29:07 -05:00
desc "Update runner's details" do
success Entities::RunnerDetails
end
params do
requires :id, type: Integer, desc: 'The ID of the runner'
optional :description, type: String, desc: 'The description of the runner'
optional :active, type: Boolean, desc: 'The state of a runner'
optional :tag_list, type: Array[String], desc: 'The list of tags for a runner'
optional :run_untagged, type: Boolean, desc: 'Flag indicating the runner can execute untagged jobs'
optional :locked, type: Boolean, desc: 'Flag indicating the runner is locked'
2017-08-29 03:09:30 -04:00
optional :access_level, type: String, values: Ci::Runner.access_levels.keys,
desc: 'The access_level of the runner'
optional :maximum_timeout, type: Integer, desc: 'Maximum timeout set when this Runner will handle the job'
2018-07-11 05:13:28 -04:00
at_least_one_of :description, :active, :tag_list, :run_untagged, :locked, :access_level, :maximum_timeout
2016-11-09 10:29:07 -05:00
end
put ':id' do
2016-11-09 10:29:07 -05:00
runner = get_runner(params.delete(:id))
authenticate_update_runner!(runner)
update_service = Ci::UpdateRunnerService.new(runner)
if update_service.update(declared_params(include_missing: false))
present runner, with: Entities::RunnerDetails, current_user: current_user
else
render_validation_error!(runner)
end
end
2016-11-09 10:29:07 -05:00
desc 'Remove a runner' do
success Entities::Runner
end
params do
requires :id, type: Integer, desc: 'The ID of the runner'
end
delete ':id' do
runner = get_runner(params[:id])
authenticate_delete_runner!(runner)
2017-03-02 07:14:13 -05:00
destroy_conditionally!(runner)
end
2017-11-16 13:44:14 -05:00
desc 'List jobs running on a runner' do
2017-11-21 06:37:18 -05:00
success Entities::JobBasicWithProject
2017-11-16 13:44:14 -05:00
end
params do
requires :id, type: Integer, desc: 'The ID of the runner'
2017-11-27 16:59:01 -05:00
optional :status, type: String, desc: 'Status of the job', values: Ci::Build::AVAILABLE_STATUSES
use :pagination
end
get ':id/jobs' do
runner = get_runner(params[:id])
authenticate_list_runners_jobs!(runner)
2017-11-27 16:59:01 -05:00
jobs = RunnerJobsFinder.new(runner, params).execute
2017-11-21 06:37:07 -05:00
2017-11-21 06:37:18 -05:00
present paginate(jobs), with: Entities::JobBasicWithProject
end
end
2016-11-09 10:29:07 -05:00
params do
requires :id, type: String, desc: 'The ID of a project'
end
2017-08-31 07:44:49 -04:00
resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
before { authorize_admin_project }
2016-11-09 10:29:07 -05:00
desc 'Get runners available for project' do
success Entities::Runner
end
params do
2018-06-07 11:43:23 -04:00
optional :scope, type: String, values: Ci::Runner::AVAILABLE_SCOPES,
2016-11-09 10:29:07 -05:00
desc: 'The scope of specific runners to show'
2016-12-04 12:11:19 -05:00
use :pagination
2016-11-09 10:29:07 -05:00
end
get ':id/runners' do
runners = filter_runners(Ci::Runner.owned_or_instance_wide(user_project.id), params[:scope])
present paginate(runners), with: Entities::Runner
end
2016-11-09 10:29:07 -05:00
desc 'Enable a runner for a project' do
success Entities::Runner
end
params do
requires :runner_id, type: Integer, desc: 'The ID of the runner'
end
post ':id/runners' do
runner = get_runner(params[:runner_id])
authenticate_enable_runner!(runner)
if runner.assign_to(user_project)
present runner, with: Entities::Runner
else
2018-05-28 07:41:04 -04:00
render_validation_error!(runner)
end
end
2016-11-09 10:29:07 -05:00
desc "Disable project's runner" do
success Entities::Runner
end
params do
requires :runner_id, type: Integer, desc: 'The ID of the runner'
end
# rubocop: disable CodeReuse/ActiveRecord
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
forbidden!("Only one project associated with the runner. Please remove the runner instead") if runner.projects.count == 1
2017-03-02 07:14:13 -05:00
destroy_conditionally!(runner_project)
end
# rubocop: enable CodeReuse/ActiveRecord
end
helpers do
def filter_runners(runners, scope, allowed_scopes: ::Ci::Runner::AVAILABLE_SCOPES)
return runners unless scope.present?
unless allowed_scopes.include?(scope)
render_api_error!('Scope contains invalid value', 400)
end
2016-01-29 10:44:29 -05:00
# Support deprecated scopes
if runners.respond_to?("deprecated_#{scope}")
scope = "deprecated_#{scope}"
end
runners.public_send(scope) # rubocop:disable GitlabSecurity/PublicSend
end
def get_runner(id)
runner = Ci::Runner.find(id)
not_found!('Runner') unless runner
runner
end
def authenticate_show_runner!(runner)
return if runner.instance_type? || current_user.admin?
forbidden!("No access granted") unless can?(current_user, :read_runner, runner)
end
def authenticate_update_runner!(runner)
2017-04-08 22:20:57 -04:00
return if current_user.admin?
forbidden!("No access granted") unless can?(current_user, :update_runner, runner)
end
def authenticate_delete_runner!(runner)
2017-04-08 22:20:57 -04:00
return if current_user.admin?
forbidden!("Runner associated with more than one project") if runner.projects.count > 1
forbidden!("No access granted") unless can?(current_user, :delete_runner, runner)
end
def authenticate_enable_runner!(runner)
forbidden!("Runner is a group runner") if runner.group_type?
2017-04-08 22:20:57 -04:00
return if current_user.admin?
forbidden!("Runner is locked") if runner.locked?
forbidden!("No access granted") unless can?(current_user, :assign_runner, runner)
end
def authenticate_list_runners_jobs!(runner)
return if current_user.admin?
forbidden!("No access granted") unless can?(current_user, :read_runner, runner)
end
end
end
end