summaryrefslogtreecommitdiff
path: root/app/graphql/mutations/work_items/create.rb
blob: 48f0f4709885cc07aba62e3101f55b5a91473a08 (plain)
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
65
# frozen_string_literal: true

module Mutations
  module WorkItems
    class Create < BaseMutation
      graphql_name 'WorkItemCreate'

      include Mutations::SpamProtection
      include FindsProject

      description "Creates a work item." \
                  " Available only when feature flag `work_items` is enabled. The feature is experimental and is subject to change without notice."

      authorize :create_work_item

      argument :description, GraphQL::Types::String,
               required: false,
               description: copy_field_description(Types::WorkItemType, :description)
      argument :project_path, GraphQL::Types::ID,
               required: true,
               description: 'Full path of the project the work item is associated with.'
      argument :title, GraphQL::Types::String,
               required: true,
               description: copy_field_description(Types::WorkItemType, :title)
      argument :work_item_type_id, ::Types::GlobalIDType[::WorkItems::Type],
               required: true,
               description: 'Global ID of a work item type.'

      field :work_item, Types::WorkItemType,
            null: true,
            description: 'Created work item.'

      def resolve(project_path:, **attributes)
        project = authorized_find!(project_path)

        unless Feature.enabled?(:work_items, project, default_enabled: :yaml)
          return { errors: ['`work_items` feature flag disabled for this project'] }
        end

        params = global_id_compatibility_params(attributes).merge(author_id: current_user.id)

        spam_params = ::Spam::SpamParams.new_from_request(request: context[:request])
        create_result = ::WorkItems::CreateService.new(project: project, current_user: current_user, params: params, spam_params: spam_params).execute

        check_spam_action_response!(create_result[:work_item]) if create_result[:work_item]

        {
          work_item: create_result.success? ? create_result[:work_item] : nil,
          errors: create_result.errors
        }
      end

      private

      def global_id_compatibility_params(params)
        # TODO: remove this line when the compatibility layer is removed
        # See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
        params[:work_item_type_id] = ::Types::GlobalIDType[::WorkItems::Type].coerce_isolated_input(params[:work_item_type_id]) if params[:work_item_type_id]
        params[:work_item_type_id] = params[:work_item_type_id]&.model_id

        params
      end
    end
  end
end