debian-mirror-gitlab/app/graphql/mutations/issues/create.rb

122 lines
4.6 KiB
Ruby
Raw Normal View History

2021-01-03 14:25:43 +05:30
# frozen_string_literal: true
module Mutations
module Issues
class Create < BaseMutation
2022-04-04 11:22:00 +05:30
graphql_name 'CreateIssue'
2021-12-11 22:18:48 +05:30
include Mutations::SpamProtection
2021-03-11 19:13:27 +05:30
include FindsProject
2021-12-11 22:18:48 +05:30
include CommonMutationArguments
2021-01-03 14:25:43 +05:30
authorize :create_issue
2021-10-27 15:23:28 +05:30
argument :project_path, GraphQL::Types::ID,
2021-01-03 14:25:43 +05:30
required: true,
2021-03-08 18:12:59 +05:30
description: 'Project full path the issue is associated with.'
2021-01-03 14:25:43 +05:30
2021-10-27 15:23:28 +05:30
argument :iid, GraphQL::Types::Int,
2021-01-03 14:25:43 +05:30
required: false,
2021-10-27 15:23:28 +05:30
description: 'IID (internal ID) of a project issue. Only admins and project owners can modify.'
2021-01-03 14:25:43 +05:30
2021-10-27 15:23:28 +05:30
argument :title, GraphQL::Types::String,
2021-01-03 14:25:43 +05:30
required: true,
description: copy_field_description(Types::IssueType, :title)
argument :milestone_id, ::Types::GlobalIDType[::Milestone],
required: false,
2021-10-27 15:23:28 +05:30
description: 'ID of the milestone to assign to the issue. On update milestone will be removed if set to null.'
2021-01-03 14:25:43 +05:30
2021-10-27 15:23:28 +05:30
argument :labels, [GraphQL::Types::String],
2021-01-03 14:25:43 +05:30
required: false,
description: copy_field_description(Types::IssueType, :labels)
argument :label_ids, [::Types::GlobalIDType[::Label]],
required: false,
2021-10-27 15:23:28 +05:30
description: 'IDs of labels to be added to the issue.'
2021-01-03 14:25:43 +05:30
argument :created_at, Types::TimeType,
required: false,
2021-03-08 18:12:59 +05:30
description: 'Timestamp when the issue was created. Available only for admins and project owners.'
2021-01-03 14:25:43 +05:30
argument :merge_request_to_resolve_discussions_of, ::Types::GlobalIDType[::MergeRequest],
required: false,
2021-10-27 15:23:28 +05:30
description: 'IID of a merge request for which to resolve discussions.'
2021-01-03 14:25:43 +05:30
2021-10-27 15:23:28 +05:30
argument :discussion_to_resolve, GraphQL::Types::String,
2021-01-03 14:25:43 +05:30
required: false,
2021-10-27 15:23:28 +05:30
description: 'ID of a discussion to resolve. Also pass `merge_request_to_resolve_discussions_of`.'
2021-01-03 14:25:43 +05:30
argument :assignee_ids, [::Types::GlobalIDType[::User]],
required: false,
2021-10-27 15:23:28 +05:30
description: 'Array of user IDs to assign to the issue.'
2021-01-03 14:25:43 +05:30
2022-04-04 11:22:00 +05:30
argument :move_before_id, ::Types::GlobalIDType[::Issue],
required: false,
description: 'Global ID of issue that should be placed before the current issue.'
argument :move_after_id, ::Types::GlobalIDType[::Issue],
required: false,
description: 'Global ID of issue that should be placed after the current issue.'
2021-01-03 14:25:43 +05:30
field :issue,
Types::IssueType,
null: true,
2021-10-27 15:23:28 +05:30
description: 'Issue after mutation.'
2021-01-03 14:25:43 +05:30
def ready?(**args)
if args.slice(*mutually_exclusive_label_args).size > 1
arg_str = mutually_exclusive_label_args.map { |x| x.to_s.camelize(:lower) }.join(' or ')
raise Gitlab::Graphql::Errors::ArgumentError, "one and only one of #{arg_str} is required."
end
if args[:discussion_to_resolve].present? && args[:merge_request_to_resolve_discussions_of].blank?
raise Gitlab::Graphql::Errors::ArgumentError,
'to resolve a discussion please also provide `merge_request_to_resolve_discussions_of` parameter'
end
super
end
def resolve(project_path:, **attributes)
2021-03-11 19:13:27 +05:30
project = authorized_find!(project_path)
2021-11-18 22:05:49 +05:30
params = build_create_issue_params(attributes.merge(author_id: current_user.id), project)
2021-01-03 14:25:43 +05:30
2021-09-30 23:02:18 +05:30
spam_params = ::Spam::SpamParams.new_from_request(request: context[:request])
2023-04-23 21:23:45 +05:30
result = ::Issues::CreateService.new(container: project, current_user: current_user, params: params, spam_params: spam_params).execute
2021-01-03 14:25:43 +05:30
2022-11-25 23:54:43 +05:30
check_spam_action_response!(result[:issue]) if result[:issue]
2021-01-03 14:25:43 +05:30
{
2022-11-25 23:54:43 +05:30
issue: result.success? ? result[:issue] : nil,
errors: result.errors
2021-01-03 14:25:43 +05:30
}
end
private
2021-11-18 22:05:49 +05:30
# _project argument is unused here, but it is necessary on the EE version of the method
def build_create_issue_params(params, _project)
2021-01-03 14:25:43 +05:30
params[:milestone_id] &&= params[:milestone_id]&.model_id
params[:assignee_ids] &&= params[:assignee_ids].map { |assignee_id| assignee_id&.model_id }
params[:label_ids] &&= params[:label_ids].map { |label_id| label_id&.model_id }
2022-04-04 11:22:00 +05:30
if params[:move_before_id].present? || params[:move_after_id].present?
params[:move_between_ids] = [
params.delete(:move_before_id)&.model_id,
params.delete(:move_after_id)&.model_id
]
end
2021-01-03 14:25:43 +05:30
params
end
def mutually_exclusive_label_args
[:labels, :label_ids]
end
end
end
end
2021-06-08 01:23:25 +05:30
Mutations::Issues::Create.prepend_mod_with('Mutations::Issues::Create')