summaryrefslogtreecommitdiff
path: root/app/services/snippets
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-02-28 18:09:07 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-02-28 18:09:07 +0000
commit1c8fa70f9d0818e2a82089c8643a6e455bca47fd (patch)
treef339f97de0425270bdd909e2f4d378927b6e0a18 /app/services/snippets
parent736d36d8597d0d1ec1b47644e6d091c3f4a78f45 (diff)
downloadgitlab-ce-1c8fa70f9d0818e2a82089c8643a6e455bca47fd.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/snippets')
-rw-r--r--app/services/snippets/create_service.rb51
1 files changed, 42 insertions, 9 deletions
diff --git a/app/services/snippets/create_service.rb b/app/services/snippets/create_service.rb
index 7ded185a6f9..cc645c514b7 100644
--- a/app/services/snippets/create_service.rb
+++ b/app/services/snippets/create_service.rb
@@ -4,6 +4,8 @@ module Snippets
class CreateService < Snippets::BaseService
include SpamCheckMethods
+ CreateRepositoryError = Class.new(StandardError)
+
def execute
filter_spam_check_params
@@ -23,13 +25,7 @@ module Snippets
spam_check(snippet, current_user)
- snippet_saved = snippet.with_transaction_returning_status do
- (snippet.save && snippet.store_mentions!).tap do |saved|
- create_repository_for(snippet, current_user) if saved
- end
- end
-
- if snippet_saved
+ if save_and_commit(snippet)
UserAgentDetailService.new(snippet, @request).create
Gitlab::UsageDataCounters::SnippetCounter.count(:create)
@@ -41,8 +37,45 @@ module Snippets
private
- def create_repository_for(snippet, user)
- snippet.create_repository if Feature.enabled?(:version_snippets, user)
+ def save_and_commit(snippet)
+ snippet.with_transaction_returning_status do
+ (snippet.save && snippet.store_mentions!).tap do |saved|
+ break false unless saved
+
+ if Feature.enabled?(:version_snippets, current_user)
+ create_repository_for(snippet)
+ create_commit(snippet)
+ end
+ end
+ rescue => e # Rescuing all because we can receive Creation exceptions, GRPC exceptions, Git exceptions, ...
+ snippet.errors.add(:base, e.message)
+
+ # If the commit action failed we need to remove the repository if exists
+ if snippet.repository_exists?
+ Repositories::DestroyService.new(snippet.repository).execute
+ end
+
+ false
+ end
+ end
+
+ def create_repository_for(snippet)
+ snippet.create_repository
+
+ raise CreateRepositoryError, 'Repository could not be created' unless snippet.repository_exists?
+ end
+
+ def create_commit(snippet)
+ commit_attrs = {
+ branch_name: 'master',
+ message: 'Initial commit'
+ }
+
+ snippet.snippet_repository.multi_files_action(current_user, snippet_files, commit_attrs)
+ end
+
+ def snippet_files
+ [{ file_path: params[:file_name], content: params[:content] }]
end
end
end