Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-03-04 18:08:09 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-03-04 18:08:09 +0300
commitd3fc3be040a4fed2328e23ef28696dd8bd8238b4 (patch)
treef1874ea5e6e3c50c6a3c2ca2900af4ae73a53119 /app/services/snippets
parentc6c7437861bff9572747674095c4dfbdfbea4988 (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/snippets')
-rw-r--r--app/services/snippets/update_service.rb58
1 files changed, 53 insertions, 5 deletions
diff --git a/app/services/snippets/update_service.rb b/app/services/snippets/update_service.rb
index c0c0aec2050..c2949ebadbf 100644
--- a/app/services/snippets/update_service.rb
+++ b/app/services/snippets/update_service.rb
@@ -4,6 +4,9 @@ module Snippets
class UpdateService < Snippets::BaseService
include SpamCheckMethods
+ UpdateError = Class.new(StandardError)
+ CreateRepositoryError = Class.new(StandardError)
+
def execute(snippet)
# check that user is allowed to set specified visibility_level
new_visibility = visibility_level
@@ -20,11 +23,7 @@ module Snippets
snippet.assign_attributes(params)
spam_check(snippet, current_user)
- snippet_saved = snippet.with_transaction_returning_status do
- snippet.save
- end
-
- if snippet_saved
+ if save_and_commit(snippet)
Gitlab::UsageDataCounters::SnippetCounter.count(:update)
ServiceResponse.success(payload: { snippet: snippet } )
@@ -32,5 +31,54 @@ module Snippets
snippet_error_response(snippet, 400)
end
end
+
+ private
+
+ def save_and_commit(snippet)
+ snippet.with_transaction_returning_status do
+ snippet.save.tap do |saved|
+ break false unless saved
+
+ # In order to avoid non migrated snippets scenarios,
+ # if the snippet does not have a repository we created it
+ # We don't need to check if the repository exists
+ # because `create_repository` already handles it
+ if Feature.enabled?(:version_snippets, current_user)
+ create_repository_for(snippet)
+ end
+
+ # If the snippet repository exists we commit always
+ # the changes
+ create_commit(snippet) if snippet.repository_exists?
+ end
+ rescue
+ snippet.errors.add(:base, 'Error updating the snippet')
+
+ 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)
+ raise UpdateError unless snippet.snippet_repository
+
+ commit_attrs = {
+ branch_name: 'master',
+ message: 'Update snippet'
+ }
+
+ snippet.snippet_repository.multi_files_action(current_user, snippet_files(snippet), commit_attrs)
+ end
+
+ def snippet_files(snippet)
+ [{ previous_path: snippet.blobs.first&.path,
+ file_path: params[:file_name],
+ content: params[:content] }]
+ end
end
end