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

105 lines
2.7 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
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?
2020-05-24 23:13:21 +05:30
unless visibility_allowed?(@snippet, @snippet.visibility_level)
2020-06-23 00:09:42 +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
2020-06-23 00:09:42 +05:30
spam_check(@snippet, current_user, action: :create)
2020-03-13 15:44:24 +05:30
2020-04-22 19:07:51 +05:30
if save_and_commit
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
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-06-23 00:09:42 +05:30
# If the snippet_files param is present
# we need to fill content and file_name from
# the model
def create_params
return params if snippet_files.empty?
params.merge(content: snippet_files[0].content, file_name: snippet_files[0].file_path)
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, ...
2020-04-22 19:07:51 +05:30
log_error(e.message)
2020-04-08 14:13:33 +05:30
# If the commit action failed we need to remove the repository if exists
2020-04-22 19:07:51 +05:30
@snippet.repository.remove 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
2020-04-08 14:13:33 +05:30
commit_attrs = {
branch_name: 'master',
message: 'Initial commit'
}
2020-06-23 00:09:42 +05:30
@snippet.snippet_repository.multi_files_action(current_user, files_to_commit(@snippet), commit_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-03-13 15:44:24 +05:30
end
end