debian-mirror-gitlab/app/finders/ci/runners_finder.rb

131 lines
3.4 KiB
Ruby
Raw Normal View History

2020-06-23 00:09:42 +05:30
# frozen_string_literal: true
module Ci
class RunnersFinder < UnionFinder
include Gitlab::Allowable
2022-04-04 11:22:00 +05:30
ALLOWED_SORTS = %w[contacted_asc contacted_desc created_at_asc created_at_desc created_date token_expires_at_asc token_expires_at_desc].freeze
2021-09-04 01:27:46 +05:30
DEFAULT_SORT = 'created_at_desc'
2021-11-11 11:23:49 +05:30
def initialize(current_user:, params:)
2020-06-23 00:09:42 +05:30
@params = params
2021-11-11 11:23:49 +05:30
@group = params.delete(:group)
2023-03-04 22:38:38 +05:30
@project = params.delete(:project)
2020-06-23 00:09:42 +05:30
@current_user = current_user
end
def execute
search!
2022-01-26 12:08:38 +05:30
filter_by_active!
2020-06-23 00:09:42 +05:30
filter_by_status!
2022-08-13 15:12:31 +05:30
filter_by_upgrade_status!
2020-06-23 00:09:42 +05:30
filter_by_runner_type!
filter_by_tag_list!
sort!
2021-09-30 23:02:18 +05:30
request_tag_list!
2020-06-23 00:09:42 +05:30
2021-09-30 23:02:18 +05:30
@runners
2020-06-23 00:09:42 +05:30
rescue Gitlab::Access::AccessDeniedError
Ci::Runner.none
end
def sort_key
2021-09-04 01:27:46 +05:30
ALLOWED_SORTS.include?(@params[:sort]) ? @params[:sort] : DEFAULT_SORT
2020-06-23 00:09:42 +05:30
end
private
def search!
2023-03-17 16:20:25 +05:30
if @project
2023-03-04 22:38:38 +05:30
project_runners
elsif @group
group_runners
else
all_runners
end
2020-06-23 00:09:42 +05:30
@runners = @runners.search(@params[:search]) if @params[:search].present?
end
def all_runners
2023-03-04 22:38:38 +05:30
raise Gitlab::Access::AccessDeniedError unless @current_user&.can_admin_all_resources?
2020-06-23 00:09:42 +05:30
@runners = Ci::Runner.all
end
def group_runners
2022-03-02 08:16:31 +05:30
raise Gitlab::Access::AccessDeniedError unless can?(@current_user, :read_group_runners, @group)
2020-06-23 00:09:42 +05:30
2021-11-11 11:23:49 +05:30
@runners = case @params[:membership]
when :direct
Ci::Runner.belonging_to_group(@group.id)
when :descendants, nil
2022-04-04 11:22:00 +05:30
Ci::Runner.belonging_to_group_or_project_descendants(@group.id)
2022-11-25 23:54:43 +05:30
when :all_available
unless can?(@current_user, :read_group_all_available_runners, @group)
raise Gitlab::Access::AccessDeniedError
end
Ci::Runner.usable_from_scope(@group)
2021-11-11 11:23:49 +05:30
else
raise ArgumentError, 'Invalid membership filter'
end
2020-06-23 00:09:42 +05:30
end
2023-03-04 22:38:38 +05:30
def project_runners
raise Gitlab::Access::AccessDeniedError unless can?(@current_user, :admin_project, @project)
@runners = ::Ci::Runner.owned_or_instance_wide(@project.id)
end
2022-01-26 12:08:38 +05:30
def filter_by_active!
@runners = @runners.active(@params[:active]) if @params.include?(:active)
end
2020-06-23 00:09:42 +05:30
def filter_by_status!
filter_by!(:status_status, Ci::Runner::AVAILABLE_STATUSES)
end
2022-08-13 15:12:31 +05:30
def filter_by_upgrade_status!
2022-08-27 11:52:29 +05:30
upgrade_status = @params[:upgrade_status]
2022-08-13 15:12:31 +05:30
2022-08-27 11:52:29 +05:30
return unless upgrade_status
unless Ci::RunnerVersion.statuses.key?(upgrade_status)
raise ArgumentError, "Invalid upgrade status value '#{upgrade_status}'"
end
@runners = @runners.with_upgrade_status(upgrade_status)
2022-08-13 15:12:31 +05:30
end
2020-06-23 00:09:42 +05:30
def filter_by_runner_type!
filter_by!(:type_type, Ci::Runner::AVAILABLE_TYPES)
end
def filter_by_tag_list!
tag_list = @params[:tag_name].presence
if tag_list
@runners = @runners.tagged_with(tag_list)
end
end
def sort!
@runners = @runners.order_by(sort_key)
end
2021-09-30 23:02:18 +05:30
def request_tag_list!
@runners = @runners.with_tags if !@params[:preload].present? || @params.dig(:preload, :tag_name)
end
2020-06-23 00:09:42 +05:30
def filter_by!(scope_name, available_scopes)
scope = @params[scope_name]
if scope.present? && available_scopes.include?(scope)
@runners = @runners.public_send(scope) # rubocop:disable GitlabSecurity/PublicSend
end
end
end
end