Commit 2df73c2f authored by Eugenia Grieff's avatar Eugenia Grieff Committed by Bob Van Landuyt

Add GraphQL mutation to move issue within lists

 - Issue can be repositioned within a board list
 and moved between board lists
parent c6e88d58
# frozen_string_literal: true
module Mutations
module Boards
module Issues
class IssueMoveList < Mutations::Issues::Base
graphql_name 'IssueMoveList'
argument :board_id, GraphQL::ID_TYPE,
required: true,
loads: Types::BoardType,
description: 'Global ID of the board that the issue is in'
argument :project_path, GraphQL::ID_TYPE,
required: true,
description: 'Project the issue to mutate is in'
argument :iid, GraphQL::STRING_TYPE,
required: true,
description: 'IID of the issue to mutate'
argument :from_list_id, GraphQL::ID_TYPE,
required: false,
description: 'ID of the board list that the issue will be moved from'
argument :to_list_id, GraphQL::ID_TYPE,
required: false,
description: 'ID of the board list that the issue will be moved to'
argument :move_before_id, GraphQL::ID_TYPE,
required: false,
description: 'ID of issue before which the current issue will be positioned at'
argument :move_after_id, GraphQL::ID_TYPE,
required: false,
description: 'ID of issue after which the current issue will be positioned at'
def ready?(**args)
if move_arguments(args).blank?
raise Gitlab::Graphql::Errors::ArgumentError,
'At least one of the arguments fromListId, toListId, afterId or beforeId is required'
end
if move_list_arguments(args).one?
raise Gitlab::Graphql::Errors::ArgumentError,
'Both fromListId and toListId must be present'
end
super
end
def resolve(board:, **args)
raise_resource_not_available_error! unless board
authorize_board!(board)
issue = authorized_find!(project_path: args[:project_path], iid: args[:iid])
move_params = { id: issue.id, board_id: board.id }.merge(move_arguments(args))
move_issue(board, issue, move_params)
{
issue: issue.reset,
errors: issue.errors.full_messages
}
end
private
def move_issue(board, issue, move_params)
service = ::Boards::Issues::MoveService.new(board.resource_parent, current_user, move_params)
service.execute(issue)
end
def move_list_arguments(args)
args.slice(:from_list_id, :to_list_id)
end
def move_arguments(args)
args.slice(:from_list_id, :to_list_id, :move_after_id, :move_before_id)
end
def authorize_board!(board)
return if Ability.allowed?(current_user, :read_board, board.resource_parent)
raise_resource_not_available_error!
end
end
end
end
end
...@@ -14,6 +14,7 @@ module Types ...@@ -14,6 +14,7 @@ module Types
mount_mutation Mutations::AwardEmojis::Add mount_mutation Mutations::AwardEmojis::Add
mount_mutation Mutations::AwardEmojis::Remove mount_mutation Mutations::AwardEmojis::Remove
mount_mutation Mutations::AwardEmojis::Toggle mount_mutation Mutations::AwardEmojis::Toggle
mount_mutation Mutations::Boards::Issues::IssueMoveList
mount_mutation Mutations::Branches::Create, calls_gitaly: true mount_mutation Mutations::Branches::Create, calls_gitaly: true
mount_mutation Mutations::Commits::Create, calls_gitaly: true mount_mutation Mutations::Commits::Create, calls_gitaly: true
mount_mutation Mutations::Discussions::ToggleResolve mount_mutation Mutations::Discussions::ToggleResolve
......
---
title: GraphQL mutation to move issue within board lists
merge_request: 38309
author:
type: added
...@@ -6667,6 +6667,71 @@ type IssueEdge { ...@@ -6667,6 +6667,71 @@ type IssueEdge {
node: Issue node: Issue
} }
"""
Autogenerated input type of IssueMoveList
"""
input IssueMoveListInput {
"""
Global ID of the board that the issue is in
"""
boardId: ID!
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
"""
ID of the board list that the issue will be moved from
"""
fromListId: ID
"""
IID of the issue to mutate
"""
iid: String!
"""
ID of issue after which the current issue will be positioned at
"""
moveAfterId: ID
"""
ID of issue before which the current issue will be positioned at
"""
moveBeforeId: ID
"""
Project the issue to mutate is in
"""
projectPath: ID!
"""
ID of the board list that the issue will be moved to
"""
toListId: ID
}
"""
Autogenerated return type of IssueMoveList
"""
type IssueMoveListPayload {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
"""
Errors encountered during execution of the mutation.
"""
errors: [String!]!
"""
The issue after mutation
"""
issue: Issue
}
""" """
Check permissions for the current user on a issue Check permissions for the current user on a issue
""" """
...@@ -8971,6 +9036,7 @@ type Mutation { ...@@ -8971,6 +9036,7 @@ type Mutation {
epicAddIssue(input: EpicAddIssueInput!): EpicAddIssuePayload epicAddIssue(input: EpicAddIssueInput!): EpicAddIssuePayload
epicSetSubscription(input: EpicSetSubscriptionInput!): EpicSetSubscriptionPayload epicSetSubscription(input: EpicSetSubscriptionInput!): EpicSetSubscriptionPayload
epicTreeReorder(input: EpicTreeReorderInput!): EpicTreeReorderPayload epicTreeReorder(input: EpicTreeReorderInput!): EpicTreeReorderPayload
issueMoveList(input: IssueMoveListInput!): IssueMoveListPayload
issueSetAssignees(input: IssueSetAssigneesInput!): IssueSetAssigneesPayload issueSetAssignees(input: IssueSetAssigneesInput!): IssueSetAssigneesPayload
issueSetConfidential(input: IssueSetConfidentialInput!): IssueSetConfidentialPayload issueSetConfidential(input: IssueSetConfidentialInput!): IssueSetConfidentialPayload
issueSetDueDate(input: IssueSetDueDateInput!): IssueSetDueDatePayload issueSetDueDate(input: IssueSetDueDateInput!): IssueSetDueDatePayload
......
...@@ -18428,6 +18428,176 @@ ...@@ -18428,6 +18428,176 @@
"enumValues": null, "enumValues": null,
"possibleTypes": null "possibleTypes": null
}, },
{
"kind": "INPUT_OBJECT",
"name": "IssueMoveListInput",
"description": "Autogenerated input type of IssueMoveList",
"fields": null,
"inputFields": [
{
"name": "projectPath",
"description": "Project the issue to mutate is in",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
}
},
"defaultValue": null
},
{
"name": "iid",
"description": "IID of the issue to mutate",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"defaultValue": null
},
{
"name": "boardId",
"description": "Global ID of the board that the issue is in",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
}
},
"defaultValue": null
},
{
"name": "fromListId",
"description": "ID of the board list that the issue will be moved from",
"type": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
},
"defaultValue": null
},
{
"name": "toListId",
"description": "ID of the board list that the issue will be moved to",
"type": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
},
"defaultValue": null
},
{
"name": "moveBeforeId",
"description": "ID of issue before which the current issue will be positioned at",
"type": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
},
"defaultValue": null
},
{
"name": "moveAfterId",
"description": "ID of issue after which the current issue will be positioned at",
"type": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
},
"defaultValue": null
},
{
"name": "clientMutationId",
"description": "A unique identifier for the client performing the mutation.",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null
}
],
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "IssueMoveListPayload",
"description": "Autogenerated return type of IssueMoveList",
"fields": [
{
"name": "clientMutationId",
"description": "A unique identifier for the client performing the mutation.",
"args": [
],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "errors",
"description": "Errors encountered during execution of the mutation.",
"args": [
],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "issue",
"description": "The issue after mutation",
"args": [
],
"type": {
"kind": "OBJECT",
"name": "Issue",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [
],
"enumValues": null,
"possibleTypes": null
},
{ {
"kind": "OBJECT", "kind": "OBJECT",
"name": "IssuePermissions", "name": "IssuePermissions",
...@@ -26040,6 +26210,33 @@ ...@@ -26040,6 +26210,33 @@
"isDeprecated": false, "isDeprecated": false,
"deprecationReason": null "deprecationReason": null
}, },
{
"name": "issueMoveList",
"description": null,
"args": [
{
"name": "input",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "INPUT_OBJECT",
"name": "IssueMoveListInput",
"ofType": null
}
},
"defaultValue": null
}
],
"type": {
"kind": "OBJECT",
"name": "IssueMoveListPayload",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{ {
"name": "issueSetAssignees", "name": "issueSetAssignees",
"description": null, "description": null,
...@@ -995,6 +995,16 @@ Represents a Group Member ...@@ -995,6 +995,16 @@ Represents a Group Member
| `webUrl` | String! | Web URL of the issue | | `webUrl` | String! | Web URL of the issue |
| `weight` | Int | Weight of the issue | | `weight` | Int | Weight of the issue |
## IssueMoveListPayload
Autogenerated return type of IssueMoveList
| Name | Type | Description |
| --- | ---- | ---------- |
| `clientMutationId` | String | A unique identifier for the client performing the mutation. |
| `errors` | String! => Array | Errors encountered during execution of the mutation. |
| `issue` | Issue | The issue after mutation |
## IssuePermissions ## IssuePermissions
Check permissions for the current user on a issue Check permissions for the current user on a issue
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Mutations::Boards::Issues::IssueMoveList do
let_it_be(:group) { create(:group, :public) }
let_it_be(:project) { create(:project, group: group) }
let_it_be(:board) { create(:board, group: group) }
let_it_be(:user) { create(:user) }
let_it_be(:guest) { create(:user) }
let_it_be(:development) { create(:label, project: project, name: 'Development') }
let_it_be(:testing) { create(:label, project: project, name: 'Testing') }
let_it_be(:list1) { create(:list, board: board, label: development, position: 0) }
let_it_be(:list2) { create(:list, board: board, label: testing, position: 1) }
let_it_be(:issue1) { create(:labeled_issue, project: project, labels: [development]) }
let_it_be(:existing_issue1) { create(:labeled_issue, project: project, labels: [testing], relative_position: 10) }
let_it_be(:existing_issue2) { create(:labeled_issue, project: project, labels: [testing], relative_position: 50) }
let(:current_user) { user }
let(:mutation) { described_class.new(object: nil, context: { current_user: current_user }, field: nil) }
let(:params) { { board: board, project_path: project.full_path, iid: issue1.iid } }
let(:move_params) do
{
from_list_id: list1.id,
to_list_id: list2.id,
move_before_id: existing_issue2.id,
move_after_id: existing_issue1.id
}
end
before_all do
group.add_maintainer(user)
group.add_guest(guest)
end
subject do
mutation.resolve(params.merge(move_params))
end
describe '#ready?' do
it 'raises an error if required arguments are missing' do
expect { mutation.ready?(params) }
.to raise_error(Gitlab::Graphql::Errors::ArgumentError, "At least one of the arguments " \
"fromListId, toListId, afterId or beforeId is required")
end
it 'raises an error if only one of fromListId and toListId is present' do
expect { mutation.ready?(params.merge(from_list_id: list1.id)) }
.to raise_error(Gitlab::Graphql::Errors::ArgumentError,
'Both fromListId and toListId must be present'
)
end
end
describe '#resolve' do
context 'when user have access to resources' do
it 'moves and repositions issue' do
subject
expect(issue1.reload.labels).to eq([testing])
expect(issue1.relative_position).to be < existing_issue2.relative_position
expect(issue1.relative_position).to be > existing_issue1.relative_position
end
end
context 'when user have no access to resources' do
shared_examples 'raises a resource not available error' do
it { expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) }
end
context 'when user cannot update issue' do
let(:current_user) { guest }
it_behaves_like 'raises a resource not available error'
end
context 'when user cannot access board' do
let(:board) { create(:board, group: create(:group, :private)) }
it_behaves_like 'raises a resource not available error'
end
context 'when passing board_id as nil' do
let(:board) { nil }
it_behaves_like 'raises a resource not available error'
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Reposition and move issue within board lists' do
include GraphqlHelpers
let_it_be(:group) { create(:group, :private) }
let_it_be(:project) { create(:project, group: group) }
let_it_be(:board) { create(:board, group: group) }
let_it_be(:user) { create(:user) }
let_it_be(:development) { create(:label, project: project, name: 'Development') }
let_it_be(:testing) { create(:label, project: project, name: 'Testing') }
let_it_be(:list1) { create(:list, board: board, label: development, position: 0) }
let_it_be(:list2) { create(:list, board: board, label: testing, position: 1) }
let_it_be(:existing_issue1) { create(:labeled_issue, project: project, labels: [testing], relative_position: 10) }
let_it_be(:existing_issue2) { create(:labeled_issue, project: project, labels: [testing], relative_position: 50) }
let_it_be(:issue1) { create(:labeled_issue, project: project, labels: [development]) }
let(:mutation_class) { Mutations::Boards::Issues::IssueMoveList }
let(:mutation_name) { mutation_class.graphql_name }
let(:mutation_result_identifier) { mutation_name.camelize(:lower) }
let(:current_user) { user }
let(:params) { { board_id: board.to_global_id.to_s, project_path: project.full_path, iid: issue1.iid.to_s } }
let(:issue_move_params) do
{
from_list_id: list1.id,
to_list_id: list2.id
}
end
before_all do
group.add_maintainer(user)
end
shared_examples 'returns an error' do
it 'fails with error' do
message = "The resource that you are attempting to access does not exist or you don't have "\
"permission to perform this action"
post_graphql_mutation(mutation(params), current_user: current_user)
expect(graphql_errors).to include(a_hash_including('message' => message))
end
end
context 'when user has access to resources' do
context 'when repositioning an issue' do
let(:issue_move_params) { { move_after_id: existing_issue1.id, move_before_id: existing_issue2.id } }
it 'repositions an issue' do
post_graphql_mutation(mutation(params), current_user: current_user)
expect(response).to have_gitlab_http_status(:success)
response_issue = json_response['data'][mutation_result_identifier]['issue']
expect(response_issue['iid']).to eq(issue1.iid.to_s)
expect(response_issue['relativePosition']).to be > existing_issue1.relative_position
expect(response_issue['relativePosition']).to be < existing_issue2.relative_position
end
end
context 'when moving an issue to a different list' do
let(:issue_move_params) { { from_list_id: list1.id, to_list_id: list2.id } }
it 'moves issue to a different list' do
post_graphql_mutation(mutation(params), current_user: current_user)
expect(response).to have_gitlab_http_status(:success)
response_issue = json_response['data'][mutation_result_identifier]['issue']
expect(response_issue['iid']).to eq(issue1.iid.to_s)
expect(response_issue['labels']['edges'][0]['node']['title']).to eq(testing.title)
end
end
end
context 'when user has no access to resources' do
context 'the user is not allowed to update the issue' do
let(:current_user) { create(:user) }
it_behaves_like 'returns an error'
end
context 'when the user can not read board' do
let(:board) { create(:board, group: create(:group, :private)) }
it_behaves_like 'returns an error'
end
end
def mutation(additional_params = {})
graphql_mutation(mutation_name, issue_move_params.merge(additional_params),
<<-QL.strip_heredoc
clientMutationId
issue {
iid,
relativePosition
labels {
edges {
node{
title
}
}
}
}
errors
QL
)
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