2020-01-16 19:09:00 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Snippets
|
|
|
|
class CreateService < Snippets::BaseService
|
|
|
|
def execute
|
2020-05-19 17:08:05 -04:00
|
|
|
@snippet = build_from_params
|
2020-01-16 19:09:00 -05:00
|
|
|
|
2020-05-22 05:08:09 -04:00
|
|
|
return invalid_params_error(@snippet) unless valid_params?
|
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
unless visibility_allowed?(@snippet, @snippet.visibility_level)
|
2020-05-22 05:08:09 -04:00
|
|
|
return forbidden_visibility_error(@snippet)
|
2020-01-16 19:09:00 -05:00
|
|
|
end
|
|
|
|
|
2020-03-26 20:08:09 -04:00
|
|
|
@snippet.author = current_user
|
2020-01-16 19:09:00 -05:00
|
|
|
|
2020-06-08 02:08:19 -04:00
|
|
|
spam_check(@snippet, current_user, action: :create)
|
2020-01-16 19:09:00 -05:00
|
|
|
|
2020-03-26 20:08:09 -04:00
|
|
|
if save_and_commit
|
|
|
|
UserAgentDetailService.new(@snippet, @request).create
|
2020-01-16 19:09:00 -05:00
|
|
|
Gitlab::UsageDataCounters::SnippetCounter.count(:create)
|
|
|
|
|
2020-05-18 02:08:14 -04:00
|
|
|
move_temporary_files
|
|
|
|
|
2020-03-26 20:08:09 -04:00
|
|
|
ServiceResponse.success(payload: { snippet: @snippet } )
|
2020-01-16 19:09:00 -05:00
|
|
|
else
|
2020-03-26 20:08:09 -04:00
|
|
|
snippet_error_response(@snippet, 400)
|
2020-01-16 19:09:00 -05:00
|
|
|
end
|
|
|
|
end
|
2020-02-17 07:09:20 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def build_from_params
|
|
|
|
if project
|
2020-05-22 05:08:09 -04:00
|
|
|
project.snippets.build(create_params)
|
2020-05-19 17:08:05 -04:00
|
|
|
else
|
2020-05-22 05:08:09 -04:00
|
|
|
PersonalSnippet.new(create_params)
|
2020-05-19 17:08:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-15 05:09:34 -04:00
|
|
|
# If the snippet_actions param is present
|
2020-05-22 05:08:09 -04:00
|
|
|
# we need to fill content and file_name from
|
|
|
|
# the model
|
|
|
|
def create_params
|
2020-07-15 05:09:34 -04:00
|
|
|
return params if snippet_actions.empty?
|
2020-05-22 05:08:09 -04:00
|
|
|
|
2020-07-15 05:09:34 -04:00
|
|
|
params.merge(content: snippet_actions[0].content, file_name: snippet_actions[0].file_path)
|
2020-05-22 05:08:09 -04:00
|
|
|
end
|
|
|
|
|
2020-03-26 20:08:09 -04:00
|
|
|
def save_and_commit
|
2020-04-21 11:21:10 -04:00
|
|
|
snippet_saved = @snippet.save
|
2020-02-28 13:09:07 -05:00
|
|
|
|
2020-04-23 14:09:46 -04:00
|
|
|
if snippet_saved
|
2020-03-26 20:08:09 -04:00
|
|
|
create_repository
|
|
|
|
create_commit
|
2020-03-23 08:09:47 -04:00
|
|
|
end
|
2020-02-28 13:09:07 -05:00
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
snippet_saved
|
2020-03-10 08:08:16 -04:00
|
|
|
rescue => e # Rescuing all because we can receive Creation exceptions, GRPC exceptions, Git exceptions, ...
|
2020-03-23 17:09:46 -04:00
|
|
|
log_error(e.message)
|
2020-03-10 08:08:16 -04:00
|
|
|
|
|
|
|
# If the commit action failed we need to remove the repository if exists
|
2020-03-26 20:08:09 -04:00
|
|
|
@snippet.repository.remove if @snippet.repository_exists?
|
2020-03-10 08:08:16 -04:00
|
|
|
|
|
|
|
# If the snippet was created, we need to remove it as we
|
|
|
|
# would do like if it had had any validation error
|
2020-03-26 20:08:09 -04:00
|
|
|
# and reassign a dupe so we don't return the deleted snippet
|
|
|
|
if @snippet.persisted?
|
|
|
|
@snippet.delete
|
|
|
|
@snippet = @snippet.dup
|
|
|
|
end
|
|
|
|
|
2020-05-13 17:08:55 -04:00
|
|
|
add_snippet_repository_error(snippet: @snippet, error: e)
|
2020-03-10 08:08:16 -04:00
|
|
|
|
|
|
|
false
|
2020-02-28 13:09:07 -05:00
|
|
|
end
|
|
|
|
|
2020-03-26 20:08:09 -04:00
|
|
|
def create_repository
|
|
|
|
@snippet.create_repository
|
2020-02-28 13:09:07 -05:00
|
|
|
|
2020-03-26 20:08:09 -04:00
|
|
|
raise CreateRepositoryError, 'Repository could not be created' unless @snippet.repository_exists?
|
2020-02-28 13:09:07 -05:00
|
|
|
end
|
|
|
|
|
2020-03-26 20:08:09 -04:00
|
|
|
def create_commit
|
2020-02-28 13:09:07 -05:00
|
|
|
commit_attrs = {
|
|
|
|
branch_name: 'master',
|
|
|
|
message: 'Initial commit'
|
|
|
|
}
|
|
|
|
|
2020-05-26 14:08:20 -04:00
|
|
|
@snippet.snippet_repository.multi_files_action(current_user, files_to_commit(@snippet), commit_attrs)
|
2020-02-17 07:09:20 -05:00
|
|
|
end
|
2020-05-18 02:08:14 -04:00
|
|
|
|
|
|
|
def move_temporary_files
|
|
|
|
return unless @snippet.is_a?(PersonalSnippet)
|
|
|
|
|
2020-05-22 05:08:09 -04:00
|
|
|
uploaded_assets.each do |file|
|
2020-05-18 02:08:14 -04:00
|
|
|
FileMover.new(file, from_model: current_user, to_model: @snippet).execute
|
|
|
|
end
|
|
|
|
end
|
2020-05-22 05:08:09 -04:00
|
|
|
|
2020-05-26 14:08:20 -04:00
|
|
|
def build_actions_from_params(_snippet)
|
2020-05-22 05:08:09 -04:00
|
|
|
[{ file_path: params[:file_name], content: params[:content] }]
|
|
|
|
end
|
2020-06-17 17:08:24 -04:00
|
|
|
|
|
|
|
def restricted_files_actions
|
|
|
|
:create
|
|
|
|
end
|
2020-01-16 19:09:00 -05:00
|
|
|
end
|
|
|
|
end
|