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

94 lines
2.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
2020-05-24 23:13:21 +05:30
@snippet = build_from_params
2020-03-13 15:44:24 +05:30
2020-05-24 23:13:21 +05:30
unless visibility_allowed?(@snippet, @snippet.visibility_level)
return error_forbidden_visibility(@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-04-22 19:07:51 +05:30
spam_check(@snippet, current_user)
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
project.snippets.build(params)
else
PersonalSnippet.new(params)
end
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-04-22 19:07:51 +05:30
@snippet.snippet_repository.multi_files_action(current_user, snippet_files, commit_attrs)
2020-04-08 14:13:33 +05:30
end
def snippet_files
[{ file_path: params[:file_name], content: params[:content] }]
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)
uploaded_files.each do |file|
FileMover.new(file, from_model: current_user, to_model: @snippet).execute
end
end
2020-03-13 15:44:24 +05:30
end
end