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

81 lines
2.7 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-03-11 19:13:27 +05:30
include ServiceCompatibility
include CanMutateSpammable
2021-01-03 14:25:43 +05:30
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-03-08 18:12:59 +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,
2021-03-08 18:12:59 +05:30
description: 'Title of the snippet.'
2020-01-01 13:55:28 +05:30
argument :description, GraphQL::STRING_TYPE,
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-03-08 18:12:59 +05:30
description: 'The visibility level of the snippet.',
2020-01-01 13:55:28 +05:30
required: false
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(id:, **args)
snippet = authorized_find!(id: id)
2020-01-01 13:55:28 +05:30
2021-03-11 19:13:27 +05:30
process_args_for_params!(args)
service_response = ::Snippets::UpdateService.new(snippet.project, current_user, args).execute(snippet)
# TODO: DRY this up - From here down, this is all duplicated with Mutations::Snippets::Create#resolve, except for
# `snippet.reset`, which is required in order to return the object in its non-dirty, unmodified, database state
# See issue here: https://gitlab.com/gitlab-org/gitlab/-/issues/300250
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
2021-03-11 19:13:27 +05:30
if !api_user? && service_response.success?
2020-11-24 15:15:51 +05:30
::Gitlab::UsageDataCounters::EditorUniqueCounter.track_snippet_editor_edit_action(author: current_user)
end
2021-03-11 19:13:27 +05:30
snippet = service_response.payload[:snippet]
2021-04-17 20:07:23 +05:30
with_spam_action_response_fields(snippet) do
2021-01-03 14:25:43 +05:30
{
2021-03-11 19:13:27 +05:30
snippet: service_response.success? ? snippet : snippet.reset,
2021-01-03 14:25:43 +05:30
errors: errors_on_object(snippet)
}
end
2020-01-01 13:55:28 +05:30
end
private
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)
2020-07-28 23:09:34 +05:30
2021-03-11 19:13:27 +05:30
if Feature.enabled?(:snippet_spam)
args.merge!(additional_spam_params)
else
args[:disable_spam_action_service] = true
2020-07-28 23:09:34 +05:30
end
2021-03-11 19:13:27 +05:30
# 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
end
def ability_name
'update'
2020-01-01 13:55:28 +05:30
end
end
end
end