2020-01-01 13:55:28 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Mutations
|
|
|
|
module Snippets
|
|
|
|
class Create < BaseMutation
|
2022-04-04 11:22:00 +05:30
|
|
|
graphql_name 'CreateSnippet'
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
include ServiceCompatibility
|
2021-04-29 21:17:54 +05:30
|
|
|
include Mutations::SpamProtection
|
2021-02-22 17:27:13 +05:30
|
|
|
|
|
|
|
authorize :create_snippet
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
field :snippet,
|
|
|
|
Types::SnippetType,
|
|
|
|
null: true,
|
2021-10-27 15:23:28 +05:30
|
|
|
description: 'Snippet after mutation.'
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
argument :title, GraphQL::Types::String,
|
2020-01-01 13:55:28 +05:30
|
|
|
required: true,
|
2021-03-08 18:12:59 +05:30
|
|
|
description: 'Title of the snippet.'
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
argument :description, GraphQL::Types::String,
|
2020-01-01 13:55:28 +05:30
|
|
|
required: false,
|
2021-03-08 18:12:59 +05:30
|
|
|
description: 'Description of the snippet.'
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
argument :visibility_level, Types::VisibilityLevelsEnum,
|
2021-10-27 15:23:28 +05:30
|
|
|
description: 'Visibility level of the snippet.',
|
2020-01-01 13:55:28 +05:30
|
|
|
required: true
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
argument :project_path, GraphQL::Types::ID,
|
2020-01-01 13:55:28 +05:30
|
|
|
required: false,
|
2021-10-27 15:23:28 +05:30
|
|
|
description: 'Full path of the project the snippet is associated with.'
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
argument :uploaded_files, [GraphQL::Types::String],
|
2020-05-24 23:13:21 +05:30
|
|
|
required: false,
|
2021-10-27 15:23:28 +05:30
|
|
|
description: 'Paths to files uploaded in the snippet description.'
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
argument :blob_actions, [Types::Snippets::BlobActionInputType],
|
2021-03-08 18:12:59 +05:30
|
|
|
description: 'Actions to perform over the snippet repository and blobs.',
|
2020-07-28 23:09:34 +05:30
|
|
|
required: false
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def resolve(project_path: nil, **args)
|
2020-01-01 13:55:28 +05:30
|
|
|
if project_path.present?
|
2021-02-22 17:27:13 +05:30
|
|
|
project = authorized_find!(project_path)
|
|
|
|
else
|
|
|
|
authorize!(:global)
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
process_args_for_params!(args)
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
spam_params = ::Spam::SpamParams.new_from_request(request: context[:request])
|
|
|
|
service = ::Snippets::CreateService.new(project: project, current_user: current_user, params: args, spam_params: spam_params)
|
|
|
|
service_response = service.execute
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
# Only when the user is not an api user and the operation was successful
|
|
|
|
if !api_user? && service_response.success?
|
2022-08-13 15:12:31 +05:30
|
|
|
::Gitlab::UsageDataCounters::EditorUniqueCounter.track_snippet_editor_edit_action(author: current_user, project: project)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
snippet = service_response.payload[:snippet]
|
2021-04-29 21:17:54 +05:30
|
|
|
check_spam_action_response!(snippet)
|
|
|
|
|
|
|
|
{
|
|
|
|
snippet: service_response.success? ? snippet : nil,
|
|
|
|
errors: errors_on_object(snippet)
|
|
|
|
}
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def find_object(full_path)
|
|
|
|
Project.find_by_full_path(full_path)
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
# 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)
|
|
|
|
|
|
|
|
# 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-07-28 23:09:34 +05:30
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|