Commit ddb8b928 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch 'set_issue_labels' into 'master'

Allow resetting issue labels

See merge request gitlab-org/gitlab!66859
parents 9db63ee5 4ff90dab
......@@ -23,6 +23,10 @@ module Mutations
required: false,
description: 'The IDs of labels to be removed from the issue.'
argument :label_ids, [GraphQL::Types::ID],
required: false,
description: 'The IDs of labels to be set. Replaces existing issue labels.'
argument :state_event, Types::IssueStateEventEnum,
description: 'Close or reopen an issue.',
required: false
......@@ -31,6 +35,8 @@ module Mutations
issue = authorized_find!(project_path: project_path, iid: iid)
project = issue.project
args = parse_arguments(args)
spam_params = ::Spam::SpamParams.new_from_request(request: context[:request])
::Issues::UpdateService.new(project: project, current_user: current_user, params: args, spam_params: spam_params).execute(issue)
......@@ -39,6 +45,32 @@ module Mutations
errors: errors_on_object(issue)
}
end
def ready?(label_ids: [], add_label_ids: [], remove_label_ids: [], **args)
if label_ids.any? && (add_label_ids.any? || remove_label_ids.any?)
raise Gitlab::Graphql::Errors::ArgumentError, 'labelIds is mutually exclusive with any of addLabelIds or removeLabelIds'
end
super
end
private
def parse_arguments(args)
args[:add_label_ids] = parse_label_ids(args[:add_label_ids])
args[:remove_label_ids] = parse_label_ids(args[:remove_label_ids])
args[:label_ids] = parse_label_ids(args[:label_ids])
args
end
def parse_label_ids(ids)
ids&.map do |gid|
GitlabSchema.parse_gid(gid, expected_type: ::Label).model_id
rescue Gitlab::Graphql::Errors::ArgumentError
gid
end
end
end
end
end
......
......@@ -4176,6 +4176,7 @@ Input type: `UpdateIssueInput`
| <a id="mutationupdateissueepicid"></a>`epicId` | [`EpicID`](#epicid) | The ID of the parent epic. NULL when removing the association. |
| <a id="mutationupdateissuehealthstatus"></a>`healthStatus` | [`HealthStatus`](#healthstatus) | The desired health status. |
| <a id="mutationupdateissueiid"></a>`iid` | [`String!`](#string) | The IID of the issue to mutate. |
| <a id="mutationupdateissuelabelids"></a>`labelIds` | [`[ID!]`](#id) | The IDs of labels to be set. Replaces existing issue labels. |
| <a id="mutationupdateissuelocked"></a>`locked` | [`Boolean`](#boolean) | Indicates discussion is locked on the issue. |
| <a id="mutationupdateissuemilestoneid"></a>`milestoneId` | [`ID`](#id) | The ID of the milestone to assign to the issue. On update milestone will be removed if set to null. |
| <a id="mutationupdateissueprojectpath"></a>`projectPath` | [`ID!`](#id) | The project the issue to mutate is in. |
......
......@@ -131,6 +131,28 @@ RSpec.describe Mutations::Issues::Update do
expect(issue.reload.labels).to match_array([project_label, label_2])
end
context 'when setting labels with label_ids' do
it 'replaces existing labels with provided ones' do
expect(issue.reload.labels).to match_array([project_label])
mutation_params[:label_ids] = [label_1.id, label_2.id]
subject
expect(issue.reload.labels).to match_array([label_1, label_2])
end
it 'raises error when label_ids is combined with remove_label_ids' do
expect { mutation.ready?(label_ids: [label_1.id, label_2.id], remove_label_ids: [label_1.id]) }
.to raise_error(Gitlab::Graphql::Errors::ArgumentError, 'labelIds is mutually exclusive with any of addLabelIds or removeLabelIds')
end
it 'raises error when label_ids is combined with add_label_ids' do
expect { mutation.ready?(label_ids: [label_1.id, label_2.id], add_label_ids: [label_2.id]) }
.to raise_error(Gitlab::Graphql::Errors::ArgumentError, 'labelIds is mutually exclusive with any of addLabelIds or removeLabelIds')
end
end
end
context 'when changing type' do
......
......@@ -8,6 +8,8 @@ RSpec.describe 'Update of an existing issue' do
let_it_be(:current_user) { create(:user) }
let_it_be(:project) { create(:project, :public) }
let_it_be(:issue) { create(:issue, project: project) }
let_it_be(:label1) { create(:label, project: project) }
let_it_be(:label2) { create(:label, project: project) }
let(:input) do
{
......@@ -20,7 +22,9 @@ RSpec.describe 'Update of an existing issue' do
}
end
let(:mutation) { graphql_mutation(:update_issue, input.merge(project_path: project.full_path, locked: true)) }
let(:extra_params) { { project_path: project.full_path, locked: true } }
let(:input_params) { input.merge(extra_params) }
let(:mutation) { graphql_mutation(:update_issue, input_params) }
let(:mutation_response) { graphql_mutation_response(:update_issue) }
context 'the user is not allowed to update issue' do
......@@ -39,5 +43,82 @@ RSpec.describe 'Update of an existing issue' do
expect(mutation_response['issue']).to include(input)
expect(mutation_response['issue']).to include('discussionLocked' => true)
end
context 'setting labels' do
let(:mutation) do
graphql_mutation(:update_issue, input_params) do
<<~QL
issue {
labels {
nodes {
id
}
}
}
errors
QL
end
end
context 'reset labels' do
let(:input_params) { input.merge(extra_params).merge({ labelIds: [label1.id, label2.id] }) }
it 'resets labels' do
post_graphql_mutation(mutation, current_user: current_user)
expect(response).to have_gitlab_http_status(:success)
expect(json_response['errors']).to be_nil
expect(mutation_response['issue']['labels']).to include({ "nodes" => [{ "id" => label1.to_global_id.to_s }, { "id" => label2.to_global_id.to_s }] })
end
context 'reset labels and add labels' do
let(:input_params) { input.merge(extra_params).merge({ labelIds: [label1.id], addLabelIds: [label2.id] }) }
it 'returns error for mutually exclusive arguments' do
post_graphql_mutation(mutation, current_user: current_user)
expect(response).to have_gitlab_http_status(:success)
expect(json_response['errors'].first['message']).to eq('labelIds is mutually exclusive with any of addLabelIds or removeLabelIds')
expect(mutation_response).to be_nil
end
end
context 'reset labels and remove labels' do
let(:input_params) { input.merge(extra_params).merge({ labelIds: [label1.id], removeLabelIds: [label2.id] }) }
it 'returns error for mutually exclusive arguments' do
post_graphql_mutation(mutation, current_user: current_user)
expect(response).to have_gitlab_http_status(:success)
expect(json_response['errors'].first['message']).to eq('labelIds is mutually exclusive with any of addLabelIds or removeLabelIds')
expect(mutation_response).to be_nil
end
end
context 'with global label ids' do
let(:input_params) { input.merge(extra_params).merge({ labelIds: [label1.to_global_id.to_s, label2.to_global_id.to_s] }) }
it 'resets labels' do
post_graphql_mutation(mutation, current_user: current_user)
expect(response).to have_gitlab_http_status(:success)
expect(json_response['errors']).to be_nil
expect(mutation_response['issue']['labels']).to include({ "nodes" => [{ "id" => label1.to_global_id.to_s }, { "id" => label2.to_global_id.to_s }] })
end
end
end
context 'add and remove labels' do
let(:input_params) { input.merge(extra_params).merge({ addLabelIds: [label1.id], removeLabelIds: [label2.id] }) }
it 'returns error for mutually exclusive arguments' do
post_graphql_mutation(mutation, current_user: current_user)
expect(response).to have_gitlab_http_status(:success)
expect(json_response['errors']).to be_nil
expect(mutation_response['issue']['labels']).to include({ "nodes" => [{ "id" => label1.to_global_id.to_s }] })
end
end
end
end
end
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