2019-12-13 09:08:01 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Mutations
|
|
|
|
module Snippets
|
|
|
|
class Create < BaseMutation
|
2020-06-02 15:08:24 +00:00
|
|
|
include ResolvesProject
|
2019-12-13 09:08:01 +00:00
|
|
|
|
|
|
|
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 :file_name, GraphQL::STRING_TYPE,
|
|
|
|
required: false,
|
|
|
|
description: 'File name of the snippet'
|
|
|
|
|
|
|
|
argument :content, GraphQL::STRING_TYPE,
|
2020-06-17 21:08:24 +00:00
|
|
|
required: false,
|
2019-12-13 09:08:01 +00:00
|
|
|
description: 'Content 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-18 15:08:15 +00:00
|
|
|
argument :uploaded_files, [GraphQL::STRING_TYPE],
|
|
|
|
required: false,
|
|
|
|
description: 'The paths to files uploaded in the snippet description'
|
|
|
|
|
2020-07-17 18:09:20 +00:00
|
|
|
argument :blob_actions, [Types::Snippets::BlobActionInputType],
|
|
|
|
description: 'Actions to perform over the snippet repository and blobs',
|
2020-06-17 21:08:24 +00:00
|
|
|
required: false
|
|
|
|
|
2019-12-13 09:08:01 +00:00
|
|
|
def resolve(args)
|
|
|
|
project_path = args.delete(:project_path)
|
|
|
|
|
|
|
|
if project_path.present?
|
|
|
|
project = find_project!(project_path: project_path)
|
|
|
|
elsif !can_create_personal_snippet?
|
2020-01-13 15:07:53 +00:00
|
|
|
raise_resource_not_available_error!
|
2019-12-13 09:08:01 +00:00
|
|
|
end
|
|
|
|
|
2020-01-17 00:09:00 +00:00
|
|
|
service_response = ::Snippets::CreateService.new(project,
|
2020-06-17 21:08:24 +00:00
|
|
|
context[:current_user],
|
|
|
|
create_params(args)).execute
|
2020-05-18 15:08:15 +00:00
|
|
|
|
2020-01-17 00:09:00 +00:00
|
|
|
snippet = service_response.payload[:snippet]
|
2019-12-13 09:08:01 +00:00
|
|
|
|
|
|
|
{
|
2020-05-27 12:08:19 +00:00
|
|
|
snippet: service_response.success? ? snippet : nil,
|
2019-12-13 09:08:01 +00:00
|
|
|
errors: errors_on_object(snippet)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_project!(project_path:)
|
|
|
|
authorized_find!(full_path: project_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_object(full_path:)
|
|
|
|
resolve_project(full_path: full_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorized_resource?(project)
|
2020-01-23 12:08:38 +00:00
|
|
|
Ability.allowed?(context[:current_user], :create_snippet, project)
|
2019-12-13 09:08:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_create_personal_snippet?
|
2020-01-23 12:08:38 +00:00
|
|
|
Ability.allowed?(context[:current_user], :create_snippet)
|
2019-12-13 09:08:01 +00:00
|
|
|
end
|
2020-06-17 21:08:24 +00:00
|
|
|
|
|
|
|
def create_params(args)
|
|
|
|
args.tap do |create_args|
|
2020-07-17 18:09:20 +00:00
|
|
|
# We need to rename `blob_actions` into `snippet_actions` because
|
2020-06-17 21:08:24 +00:00
|
|
|
# it's the expected key param
|
2020-07-17 18:09:20 +00:00
|
|
|
create_args[:snippet_actions] = create_args.delete(:blob_actions)&.map(&:to_h)
|
2020-06-17 21:08:24 +00:00
|
|
|
|
|
|
|
# We need to rename `uploaded_files` into `files` because
|
|
|
|
# it's the expected key param
|
|
|
|
create_args[:files] = create_args.delete(:uploaded_files)
|
|
|
|
end
|
|
|
|
end
|
2019-12-13 09:08:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|