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

69 lines
2.1 KiB
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
module Mutations
module Snippets
class Update < Base
2021-01-03 14:25:43 +05:30
include SpammableMutationFields
2020-01-01 13:55:28 +05:30
graphql_name 'UpdateSnippet'
2021-01-29 00:20:46 +05:30
argument :id, ::Types::GlobalIDType[::Snippet],
2020-01-01 13:55:28 +05:30
required: true,
2021-02-22 17:27:13 +05:30
description: 'The global ID of the snippet to update'
2020-01-01 13:55:28 +05:30
argument :title, GraphQL::STRING_TYPE,
required: false,
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: false
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(id:, **args)
snippet = authorized_find!(id: id)
2020-01-01 13:55:28 +05:30
2020-03-13 15:44:24 +05:30
result = ::Snippets::UpdateService.new(snippet.project,
2021-02-22 17:27:13 +05:30
current_user,
2020-07-28 23:09:34 +05:30
update_params(args)).execute(snippet)
2020-03-13 15:44:24 +05:30
snippet = result.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? && result.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: result.success? ? snippet : snippet.reset,
errors: errors_on_object(snippet)
}
end
2020-01-01 13:55:28 +05:30
end
private
def ability_name
2020-07-28 23:09:34 +05:30
'update'
end
def update_params(args)
2021-01-03 14:25:43 +05:30
with_spam_params do
args.tap do |update_args|
# We need to rename `blob_actions` into `snippet_actions` because
# it's the expected key param
update_args[:snippet_actions] = update_args.delete(:blob_actions)&.map(&:to_h)
end
2020-07-28 23:09:34 +05:30
end
2020-01-01 13:55:28 +05:30
end
end
end
end