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

121 lines
3.4 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
module Snippets
class CreateService < Snippets::BaseService
2021-09-30 23:02:18 +05:30
# NOTE: For Issues::CreateService, we require the spam_params and do not default it to nil, because
# spam_checking is likely to be necessary.
2023-03-04 22:38:38 +05:30
def initialize(project:, spam_params:, current_user: nil, params: {})
2021-09-30 23:02:18 +05:30
super(project: project, current_user: current_user, params: params)
@spam_params = spam_params
end
2021-03-11 19:13:27 +05:30
2021-09-30 23:02:18 +05:30
def execute
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-09-04 01:27:46 +05:30
unless visibility_allowed?(snippet.visibility_level)
2021-03-11 19:13:27 +05:30
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-09-30 23:02:18 +05:30
Spam::SpamActionService.new(
spammable: @snippet,
spam_params: spam_params,
user: current_user,
2022-10-11 01:57:18 +05:30
action: :create,
extra_features: { files: file_paths_to_commit }
2021-09-30 23:02:18 +05:30
).execute
2020-03-13 15:44:24 +05:30
2020-04-22 19:07:51 +05:30
if save_and_commit
2021-09-30 23:02:18 +05:30
UserAgentDetailService.new(spammable: @snippet, spam_params: spam_params).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
2023-01-13 00:05:48 +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-09-30 23:02:18 +05:30
attr_reader :snippet, :spam_params
2021-03-11 19:13:27 +05:30
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
2021-06-08 01:23:25 +05:30
rescue StandardError => 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