1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
# frozen_string_literal: true
module Mutations
module WorkItems
class Convert < BaseMutation
graphql_name 'WorkItemConvert'
description "Converts the work item to a new type"
include Mutations::SpamProtection
authorize :update_work_item
argument :id, ::Types::GlobalIDType[::WorkItem],
required: true,
description: 'Global ID of the work item.'
argument :work_item_type_id, ::Types::GlobalIDType[::WorkItems::Type],
required: true,
description: 'Global ID of the new work item type.'
field :work_item, Types::WorkItemType,
null: true,
description: 'Updated work item.'
def resolve(attributes)
work_item = authorized_find!(id: attributes[:id])
work_item_type = find_work_item_type!(attributes[:work_item_type_id])
authorize_work_item_type!(work_item, work_item_type)
update_result = ::WorkItems::UpdateService.new(
container: work_item.project,
current_user: current_user,
params: { work_item_type: work_item_type, issue_type: work_item_type.base_type },
perform_spam_check: true
).execute(work_item)
check_spam_action_response!(work_item)
{
work_item: (update_result[:work_item] if update_result[:status] == :success),
errors: Array.wrap(update_result[:message])
}
end
private
def find_work_item_type!(gid)
work_item_type = ::WorkItems::Type.find_by_id(gid.model_id)
return work_item_type if work_item_type.present?
message = format(_('Work Item type with id %{id} was not found'), id: gid.model_id)
raise_resource_not_available_error! message
end
def authorize_work_item_type!(work_item, work_item_type)
return if current_user.can?(:"create_#{work_item_type.base_type}", work_item)
message = format(_('You are not allowed to change the Work Item type to %{name}.'), name: work_item_type.name)
raise_resource_not_available_error! message
end
end
end
end
|