2023-05-27 22:25:52 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Mutations
|
|
|
|
module Ci
|
|
|
|
module Runner
|
|
|
|
class Create < BaseMutation
|
|
|
|
graphql_name 'RunnerCreate'
|
|
|
|
|
|
|
|
authorize :create_runner
|
|
|
|
|
|
|
|
include Mutations::Ci::Runner::CommonMutationArguments
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
argument :runner_type, ::Types::Ci::RunnerTypeEnum,
|
|
|
|
required: true,
|
|
|
|
description: 'Type of the runner to create.'
|
|
|
|
|
|
|
|
argument :group_id, ::Types::GlobalIDType[Group],
|
|
|
|
required: false,
|
|
|
|
description: 'Global ID of the group that the runner is created in (valid only for group runner).'
|
|
|
|
|
|
|
|
argument :project_id, ::Types::GlobalIDType[Project],
|
|
|
|
required: false,
|
|
|
|
description: 'Global ID of the project that the runner is created in (valid only for project runner).'
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
field :runner,
|
|
|
|
Types::Ci::RunnerType,
|
|
|
|
null: true,
|
|
|
|
description: 'Runner after mutation.'
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
def ready?(**args)
|
|
|
|
case args[:runner_type]
|
|
|
|
when 'group_type'
|
|
|
|
raise Gitlab::Graphql::Errors::ArgumentError, '`group_id` is missing' unless args[:group_id].present?
|
|
|
|
when 'project_type'
|
|
|
|
raise Gitlab::Graphql::Errors::ArgumentError, '`project_id` is missing' unless args[:project_id].present?
|
2023-05-27 22:25:52 +05:30
|
|
|
end
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
parse_gid(**args)
|
|
|
|
|
|
|
|
check_feature_flag(**args)
|
|
|
|
|
|
|
|
super
|
2023-05-27 22:25:52 +05:30
|
|
|
end
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
def resolve(**args)
|
|
|
|
case args[:runner_type]
|
|
|
|
when 'group_type', 'project_type'
|
|
|
|
args[:scope] = authorized_find!(**args)
|
|
|
|
args.except!(:group_id, :project_id)
|
|
|
|
else
|
|
|
|
raise_resource_not_available_error! unless current_user.can?(:create_instance_runner)
|
|
|
|
end
|
2023-05-27 22:25:52 +05:30
|
|
|
|
|
|
|
response = { runner: nil, errors: [] }
|
2023-06-20 00:43:36 +05:30
|
|
|
result = ::Ci::Runners::CreateRunnerService.new(user: current_user, params: args).execute
|
2023-05-27 22:25:52 +05:30
|
|
|
|
|
|
|
if result.success?
|
|
|
|
response[:runner] = result.payload[:runner]
|
|
|
|
else
|
|
|
|
response[:errors] = result.errors
|
|
|
|
end
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
2023-06-20 00:43:36 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_object(**args)
|
|
|
|
obj = parse_gid(**args)
|
|
|
|
|
|
|
|
GitlabSchema.find_by_gid(obj) if obj
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_gid(runner_type:, **args)
|
|
|
|
case runner_type
|
|
|
|
when 'group_type'
|
|
|
|
GitlabSchema.parse_gid(args[:group_id], expected_type: ::Group)
|
|
|
|
when 'project_type'
|
|
|
|
GitlabSchema.parse_gid(args[:project_id], expected_type: ::Project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_feature_flag(**args)
|
|
|
|
case args[:runner_type]
|
|
|
|
when 'instance_type'
|
|
|
|
if Feature.disabled?(:create_runner_workflow_for_admin, current_user)
|
|
|
|
raise Gitlab::Graphql::Errors::ResourceNotAvailable,
|
|
|
|
'`create_runner_workflow_for_admin` feature flag is disabled.'
|
|
|
|
end
|
|
|
|
when 'group_type'
|
|
|
|
namespace = find_object(**args).sync
|
|
|
|
if Feature.disabled?(:create_runner_workflow_for_namespace, namespace)
|
|
|
|
raise Gitlab::Graphql::Errors::ResourceNotAvailable,
|
|
|
|
'`create_runner_workflow_for_namespace` feature flag is disabled.'
|
|
|
|
end
|
|
|
|
when 'project_type'
|
|
|
|
project = find_object(**args).sync
|
|
|
|
if project && Feature.disabled?(:create_runner_workflow_for_namespace, project.namespace)
|
|
|
|
raise Gitlab::Graphql::Errors::ResourceNotAvailable,
|
|
|
|
'`create_runner_workflow_for_namespace` feature flag is disabled.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-05-27 22:25:52 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|