debian-mirror-gitlab/lib/api/runners.rb

253 lines
9.4 KiB
Ruby
Raw Permalink Normal View History

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