Commit 61b97fe3 authored by Pedro Pombeiro's avatar Pedro Pombeiro

Implement a :type argument in the Reset mutation

parent 059a0149
......@@ -10,8 +10,12 @@ module Mutations
ScopeID = ::GraphQL::ID_TYPE
argument :type, ::Types::Ci::RunnerTypeEnum,
required: true,
description: 'Scope of the object to reset the token for.'
argument :id, ScopeID,
required: true,
required: false,
description: 'ID of the project or group to reset the token for. Omit if resetting instance runner token.'
field :token,
......@@ -21,27 +25,37 @@ module Mutations
def resolve(**args)
{
token: reset_token(args[:id]),
token: reset_token(**args),
errors: []
}
end
private
def find_object(id:)
return unless id
def find_object(type:, **args)
id = args[:id]
GitlabSchema.object_from_id(id, expected_type: [::Project, ::Group])
case type
when 'group_type'
GitlabSchema.object_from_id(id, expected_type: ::Group)
when 'project_type'
GitlabSchema.object_from_id(id, expected_type: ::Project)
end
end
def reset_token(id)
if id.blank?
def reset_token(type:, **args)
id = args[:id]
case type
when 'instance_type'
raise Gitlab::Graphql::Errors::ArgumentError, "id must not be specified for '#{type}' scope" if id.present?
authorize!(:global)
ApplicationSetting.current.reset_runners_registration_token!
ApplicationSetting.current_without_cache.runners_registration_token
else
project_or_group = authorized_find!(id: id)
when 'group_type', 'project_type'
project_or_group = authorized_find!(type: type, id: id)
project_or_group.reset_runners_token!
project_or_group.runners_token
end
......
......@@ -3538,7 +3538,8 @@ Input type: `RunnersRegistrationTokenResetInput`
| Name | Type | Description |
| ---- | ---- | ----------- |
| <a id="mutationrunnersregistrationtokenresetclientmutationid"></a>`clientMutationId` | [`String`](#string) | A unique identifier for the client performing the mutation. |
| <a id="mutationrunnersregistrationtokenresetid"></a>`id` | [`ID!`](#id) | ID of the project or group to reset the token for. Omit if resetting instance runner token. |
| <a id="mutationrunnersregistrationtokenresetid"></a>`id` | [`ID`](#id) | ID of the project or group to reset the token for. Omit if resetting instance runner token. |
| <a id="mutationrunnersregistrationtokenresettype"></a>`type` | [`CiRunnerType!`](#cirunnertype) | Scope of the object to reset the token for. |
#### Fields
......
......@@ -38,7 +38,7 @@ RSpec.describe 'RunnersRegistrationTokenReset' do
end
end
shared_context 'when authorized' do
shared_context 'when authorized' do |scope|
it 'resets runner registration token' do
expect { subject }.to change { get_token }
expect(response).to have_gitlab_http_status(:success)
......@@ -49,8 +49,8 @@ RSpec.describe 'RunnersRegistrationTokenReset' do
expect(mutation_response['token']).to eq(get_token)
end
context 'when bad arguments are provided' do
let(:input) { { id: 'some string' } }
context 'when malformed id is provided' do
let(:input) { { type: "#{scope.upcase}_TYPE", id: 'some string' } }
it 'returns errors' do
expect { subject }.not_to change { get_token }
......@@ -64,13 +64,13 @@ RSpec.describe 'RunnersRegistrationTokenReset' do
context 'applied to project' do
let_it_be(:project) { create_default(:project) }
let(:input) { { id: project.to_global_id.to_s } }
let(:input) { { type: 'PROJECT_TYPE', id: project.to_global_id.to_s } }
include_context 'when unauthorized', 'project' do
let(:target) { project }
end
include_context 'when authorized' do
include_context 'when authorized', 'project' do
let_it_be(:user) { project.owner }
def get_token
......@@ -82,13 +82,13 @@ RSpec.describe 'RunnersRegistrationTokenReset' do
context 'applied to group' do
let_it_be(:group) { create_default(:group) }
let(:input) { { id: group.to_global_id.to_s } }
let(:input) { { type: 'GROUP_TYPE', id: group.to_global_id.to_s } }
include_context 'when unauthorized', 'group' do
let(:target) { group }
end
include_context 'when authorized' do
include_context 'when authorized', 'group' do
let_it_be(:user) { create_default(:group_member, :maintainer, user: create(:user), group: group ).user }
def get_token
......@@ -103,7 +103,7 @@ RSpec.describe 'RunnersRegistrationTokenReset' do
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
end
let(:input) { {} }
let(:input) { { type: 'INSTANCE_TYPE' } }
context 'when unauthorized' do
let(:user) { create(:user) }
......@@ -111,7 +111,7 @@ RSpec.describe 'RunnersRegistrationTokenReset' do
it_behaves_like 'unauthorized'
end
include_context 'when authorized' do
include_context 'when authorized', 'instance' do
let_it_be(:user) { create(:user, :admin) }
def get_token
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment