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

89 lines
2.8 KiB
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
module Mutations
module Snippets
class Create < BaseMutation
2021-01-03 14:25:43 +05:30
include SpammableMutationFields
2021-02-22 17:27:13 +05:30
authorize :create_snippet
2020-01-01 13:55:28 +05:30
graphql_name 'CreateSnippet'
field :snippet,
Types::SnippetType,
null: true,
description: 'The snippet after mutation'
argument :title, GraphQL::STRING_TYPE,
required: true,
description: 'Title of the snippet'
argument :description, GraphQL::STRING_TYPE,
required: false,
description: 'Description of the snippet'
argument :visibility_level, Types::VisibilityLevelsEnum,
description: 'The visibility level of the snippet',
required: true
argument :project_path, GraphQL::ID_TYPE,
required: false,
description: 'The project full path the snippet is associated with'
2020-05-24 23:13:21 +05:30
argument :uploaded_files, [GraphQL::STRING_TYPE],
required: false,
description: 'The paths to files uploaded in the snippet description'
2020-10-24 23:57:45 +05:30
argument :blob_actions, [Types::Snippets::BlobActionInputType],
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
2020-03-13 15:44:24 +05:30
service_response = ::Snippets::CreateService.new(project,
2021-02-22 17:27:13 +05:30
current_user,
2020-07-28 23:09:34 +05:30
create_params(args)).execute
2020-05-24 23:13:21 +05:30
2020-03-13 15:44:24 +05:30
snippet = service_response.payload[:snippet]
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?
::Gitlab::UsageDataCounters::EditorUniqueCounter.track_snippet_editor_edit_action(author: current_user)
end
2021-01-03 14:25:43 +05:30
with_spam_fields(snippet) do
{
snippet: service_response.success? ? snippet : nil,
errors: errors_on_object(snippet)
}
end
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
def create_params(args)
2021-01-03 14:25:43 +05:30
with_spam_params do
args.tap do |create_args|
# We need to rename `blob_actions` into `snippet_actions` because
# it's the expected key param
create_args[:snippet_actions] = create_args.delete(:blob_actions)&.map(&:to_h)
# We need to rename `uploaded_files` into `files` because
# it's the expected key param
create_args[:files] = create_args.delete(:uploaded_files)
end
2020-07-28 23:09:34 +05:30
end
end
2020-01-01 13:55:28 +05:30
end
end
end