2019-12-13 04:08:01 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Mutations
|
|
|
|
module Snippets
|
|
|
|
class Create < BaseMutation
|
2021-01-28 19:09:17 -05:00
|
|
|
include ServiceCompatibility
|
|
|
|
include CanMutateSpammable
|
2021-04-09 14:09:24 -04:00
|
|
|
include Mutations::SpamProtection
|
2020-12-14 16:09:47 -05:00
|
|
|
|
|
|
|
authorize :create_snippet
|
2019-12-13 04:08:01 -05:00
|
|
|
|
|
|
|
graphql_name 'CreateSnippet'
|
|
|
|
|
|
|
|
field :snippet,
|
|
|
|
Types::SnippetType,
|
|
|
|
null: true,
|
2021-01-07 07:10:24 -05:00
|
|
|
description: 'The snippet after mutation.'
|
2019-12-13 04:08:01 -05:00
|
|
|
|
|
|
|
argument :title, GraphQL::STRING_TYPE,
|
|
|
|
required: true,
|
2021-01-07 07:10:24 -05:00
|
|
|
description: 'Title of the snippet.'
|
2019-12-13 04:08:01 -05:00
|
|
|
|
|
|
|
argument :description, GraphQL::STRING_TYPE,
|
|
|
|
required: false,
|
2021-01-07 07:10:24 -05:00
|
|
|
description: 'Description of the snippet.'
|
2019-12-13 04:08:01 -05:00
|
|
|
|
|
|
|
argument :visibility_level, Types::VisibilityLevelsEnum,
|
2021-01-07 07:10:24 -05:00
|
|
|
description: 'The visibility level of the snippet.',
|
2019-12-13 04:08:01 -05:00
|
|
|
required: true
|
|
|
|
|
|
|
|
argument :project_path, GraphQL::ID_TYPE,
|
|
|
|
required: false,
|
2021-01-07 07:10:24 -05:00
|
|
|
description: 'The project full path the snippet is associated with.'
|
2019-12-13 04:08:01 -05:00
|
|
|
|
2020-05-18 11:08:15 -04:00
|
|
|
argument :uploaded_files, [GraphQL::STRING_TYPE],
|
|
|
|
required: false,
|
2021-01-07 07:10:24 -05:00
|
|
|
description: 'The paths to files uploaded in the snippet description.'
|
2020-05-18 11:08:15 -04:00
|
|
|
|
2020-07-17 14:09:20 -04:00
|
|
|
argument :blob_actions, [Types::Snippets::BlobActionInputType],
|
2021-01-07 07:10:24 -05:00
|
|
|
description: 'Actions to perform over the snippet repository and blobs.',
|
2020-06-17 17:08:24 -04:00
|
|
|
required: false
|
|
|
|
|
2020-12-14 16:09:47 -05:00
|
|
|
def resolve(project_path: nil, **args)
|
2019-12-13 04:08:01 -05:00
|
|
|
if project_path.present?
|
2020-12-14 16:09:47 -05:00
|
|
|
project = authorized_find!(project_path)
|
|
|
|
else
|
|
|
|
authorize!(:global)
|
2019-12-13 04:08:01 -05:00
|
|
|
end
|
|
|
|
|
2021-01-28 19:09:17 -05:00
|
|
|
process_args_for_params!(args)
|
2020-05-18 11:08:15 -04:00
|
|
|
|
2021-06-11 05:09:58 -04:00
|
|
|
service_response = ::Snippets::CreateService.new(project: project, current_user: current_user, params: args).execute
|
2019-12-13 04:08:01 -05:00
|
|
|
|
2020-08-31 11:10:41 -04:00
|
|
|
# Only when the user is not an api user and the operation was successful
|
|
|
|
if !api_user? && service_response.success?
|
|
|
|
::Gitlab::UsageDataCounters::EditorUniqueCounter.track_snippet_editor_edit_action(author: current_user)
|
|
|
|
end
|
|
|
|
|
2021-01-28 19:09:17 -05:00
|
|
|
snippet = service_response.payload[:snippet]
|
2021-04-09 14:09:24 -04:00
|
|
|
check_spam_action_response!(snippet)
|
|
|
|
|
|
|
|
{
|
|
|
|
snippet: service_response.success? ? snippet : nil,
|
|
|
|
errors: errors_on_object(snippet)
|
|
|
|
}
|
2019-12-13 04:08:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-12-14 16:09:47 -05:00
|
|
|
def find_object(full_path)
|
|
|
|
Project.find_by_full_path(full_path)
|
2019-12-13 04:08:01 -05:00
|
|
|
end
|
2020-06-17 17:08:24 -04:00
|
|
|
|
2021-01-28 19:09:17 -05:00
|
|
|
# process_args_for_params!(args) -> nil
|
|
|
|
#
|
|
|
|
# Modifies/adds/deletes mutation resolve args as necessary to be passed as params to service layer.
|
|
|
|
def process_args_for_params!(args)
|
|
|
|
convert_blob_actions_to_snippet_actions!(args)
|
|
|
|
|
|
|
|
# We need to rename `uploaded_files` into `files` because
|
|
|
|
# it's the expected key param
|
|
|
|
args[:files] = args.delete(:uploaded_files)
|
|
|
|
|
|
|
|
if Feature.enabled?(:snippet_spam)
|
|
|
|
args.merge!(additional_spam_params)
|
|
|
|
else
|
|
|
|
args[:disable_spam_action_service] = true
|
2020-06-17 17:08:24 -04:00
|
|
|
end
|
2021-01-28 19:09:17 -05:00
|
|
|
|
|
|
|
# Return nil to make it explicit that this method is mutating the args parameter, and that
|
|
|
|
# the return value is not relevant and is not to be used.
|
|
|
|
nil
|
2020-06-17 17:08:24 -04:00
|
|
|
end
|
2019-12-13 04:08:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|