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

88 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
include SpamCheckMethods
2020-04-08 14:13:33 +05:30
CreateRepositoryError = Class.new(StandardError)
2020-03-13 15:44:24 +05:30
def execute
filter_spam_check_params
2020-04-22 19:07:51 +05:30
@snippet = if project
project.snippets.build(params)
else
PersonalSnippet.new(params)
end
2020-03-13 15:44:24 +05:30
2020-04-22 19:07:51 +05:30
unless Gitlab::VisibilityLevel.allowed_for?(current_user, @snippet.visibility_level)
deny_visibility_level(@snippet)
2020-03-13 15:44:24 +05:30
2020-04-22 19:07:51 +05:30
return snippet_error_response(@snippet, 403)
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-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-04-22 19:07:51 +05:30
def save_and_commit
snippet_saved = @snippet.save
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
if snippet_saved && Feature.enabled?(:version_snippets, current_user)
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
@snippet.errors.add(:base, e.message)
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
end
end