debian-mirror-gitlab/app/services/snippets/create_service.rb

120 lines
3.3 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
module Snippets
class CreateService < Snippets::BaseService
def execute
2021-03-11 19:13:27 +05:30
# NOTE: disable_spam_action_service can be removed when the ':snippet_spam' feature flag is removed.
disable_spam_action_service = params.delete(:disable_spam_action_service) == true
@request = params.delete(:request)
@spam_params = Spam::SpamActionService.filter_spam_params!(params)
2020-05-24 23:13:21 +05:30
@snippet = build_from_params
2020-03-13 15:44:24 +05:30
2020-06-23 00:09:42 +05:30
return invalid_params_error(@snippet) unless valid_params?
2021-03-11 19:13:27 +05:30
unless visibility_allowed?(snippet, snippet.visibility_level)
return forbidden_visibility_error(snippet)
2020-03-13 15:44:24 +05:30
end
2020-04-22 19:07:51 +05:30
@snippet.author = current_user
2020-03-13 15:44:24 +05:30
2021-03-11 19:13:27 +05:30
unless disable_spam_action_service
Spam::SpamActionService.new(
spammable: @snippet,
request: request,
user: current_user,
action: :create
).execute(spam_params: spam_params)
end
2020-03-13 15:44:24 +05:30
2020-04-22 19:07:51 +05:30
if save_and_commit
2021-03-11 19:13:27 +05:30
UserAgentDetailService.new(@snippet, request).create
2020-03-13 15:44:24 +05:30
Gitlab::UsageDataCounters::SnippetCounter.count(:create)
2020-05-24 23:13:21 +05:30
move_temporary_files
2020-04-22 19:07:51 +05:30
ServiceResponse.success(payload: { snippet: @snippet } )
2020-03-13 15:44:24 +05:30
else
2020-04-22 19:07:51 +05:30
snippet_error_response(@snippet, 400)
2020-03-13 15:44:24 +05:30
end
end
private
2021-03-11 19:13:27 +05:30
attr_reader :snippet, :request, :spam_params
2020-05-24 23:13:21 +05:30
def build_from_params
if project
2020-06-23 00:09:42 +05:30
project.snippets.build(create_params)
2020-05-24 23:13:21 +05:30
else
2020-06-23 00:09:42 +05:30
PersonalSnippet.new(create_params)
2020-05-24 23:13:21 +05:30
end
end
2020-07-28 23:09:34 +05:30
# If the snippet_actions param is present
2020-06-23 00:09:42 +05:30
# we need to fill content and file_name from
# the model
def create_params
2020-07-28 23:09:34 +05:30
return params if snippet_actions.empty?
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
params.merge(content: snippet_actions[0].content, file_name: snippet_actions[0].file_path)
2020-06-23 00:09:42 +05:30
end
2020-04-22 19:07:51 +05:30
def save_and_commit
snippet_saved = @snippet.save
2020-04-08 14:13:33 +05:30
2020-05-24 23:13:21 +05:30
if snippet_saved
2020-04-22 19:07:51 +05:30
create_repository
create_commit
2020-04-08 14:13:33 +05:30
end
2020-04-22 19:07:51 +05:30
snippet_saved
2020-04-08 14:13:33 +05:30
rescue => e # Rescuing all because we can receive Creation exceptions, GRPC exceptions, Git exceptions, ...
2021-01-29 00:20:46 +05:30
Gitlab::ErrorTracking.log_exception(e, service: 'Snippets::CreateService')
2020-04-08 14:13:33 +05:30
# If the commit action failed we need to remove the repository if exists
2021-01-03 14:25:43 +05:30
delete_repository(@snippet) if @snippet.repository_exists?
2020-04-08 14:13:33 +05:30
# If the snippet was created, we need to remove it as we
# would do like if it had had any validation error
2020-04-22 19:07:51 +05:30
# and reassign a dupe so we don't return the deleted snippet
if @snippet.persisted?
@snippet.delete
@snippet = @snippet.dup
end
2020-05-24 23:13:21 +05:30
add_snippet_repository_error(snippet: @snippet, error: e)
2020-04-08 14:13:33 +05:30
false
end
2020-04-22 19:07:51 +05:30
def create_repository
@snippet.create_repository
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
raise CreateRepositoryError, 'Repository could not be created' unless @snippet.repository_exists?
2020-04-08 14:13:33 +05:30
end
2020-04-22 19:07:51 +05:30
def create_commit
2021-01-03 14:25:43 +05:30
attrs = commit_attrs(@snippet, INITIAL_COMMIT_MSG)
2020-04-08 14:13:33 +05:30
2021-01-03 14:25:43 +05:30
@snippet.snippet_repository.multi_files_action(current_user, files_to_commit(@snippet), **attrs)
2020-03-13 15:44:24 +05:30
end
2020-05-24 23:13:21 +05:30
def move_temporary_files
return unless @snippet.is_a?(PersonalSnippet)
2020-06-23 00:09:42 +05:30
uploaded_assets.each do |file|
2020-05-24 23:13:21 +05:30
FileMover.new(file, from_model: current_user, to_model: @snippet).execute
end
end
2020-06-23 00:09:42 +05:30
def build_actions_from_params(_snippet)
[{ file_path: params[:file_name], content: params[:content] }]
end
2020-07-28 23:09:34 +05:30
def restricted_files_actions
:create
end
2020-03-13 15:44:24 +05:30
end
end