Commit 4ff90dab authored by Alexandru Croitor's avatar Alexandru Croitor

Allow resetting issue labels

Add the ability to set an array of lables to an issue, without having
to remove/add labels to do that.

Changelog :added
parent a639a44d
...@@ -23,6 +23,10 @@ module Mutations ...@@ -23,6 +23,10 @@ module Mutations
required: false, required: false,
description: 'The IDs of labels to be removed from the issue.' 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, argument :state_event, Types::IssueStateEventEnum,
description: 'Close or reopen an issue.', description: 'Close or reopen an issue.',
required: false required: false
...@@ -31,6 +35,8 @@ module Mutations ...@@ -31,6 +35,8 @@ module Mutations
issue = authorized_find!(project_path: project_path, iid: iid) issue = authorized_find!(project_path: project_path, iid: iid)
project = issue.project project = issue.project
args = parse_arguments(args)
spam_params = ::Spam::SpamParams.new_from_request(request: context[:request]) 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) ::Issues::UpdateService.new(project: project, current_user: current_user, params: args, spam_params: spam_params).execute(issue)
...@@ -39,6 +45,32 @@ module Mutations ...@@ -39,6 +45,32 @@ module Mutations
errors: errors_on_object(issue) errors: errors_on_object(issue)
} }
end 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 end
end end
......
...@@ -4176,6 +4176,7 @@ Input type: `UpdateIssueInput` ...@@ -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="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="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="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="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="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. | | <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 ...@@ -131,6 +131,28 @@ RSpec.describe Mutations::Issues::Update do
expect(issue.reload.labels).to match_array([project_label, label_2]) expect(issue.reload.labels).to match_array([project_label, label_2])
end 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 end
context 'when changing type' do context 'when changing type' do
......
...@@ -8,6 +8,8 @@ RSpec.describe 'Update of an existing issue' do ...@@ -8,6 +8,8 @@ RSpec.describe 'Update of an existing issue' do
let_it_be(:current_user) { create(:user) } let_it_be(:current_user) { create(:user) }
let_it_be(:project) { create(:project, :public) } let_it_be(:project) { create(:project, :public) }
let_it_be(:issue) { create(:issue, project: project) } 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 let(:input) do
{ {
...@@ -20,7 +22,9 @@ RSpec.describe 'Update of an existing issue' do ...@@ -20,7 +22,9 @@ RSpec.describe 'Update of an existing issue' do
} }
end 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) } let(:mutation_response) { graphql_mutation_response(:update_issue) }
context 'the user is not allowed to update issue' do context 'the user is not allowed to update issue' do
...@@ -39,5 +43,82 @@ RSpec.describe 'Update of an existing 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(input)
expect(mutation_response['issue']).to include('discussionLocked' => true) expect(mutation_response['issue']).to include('discussionLocked' => true)
end 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
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