diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-27 03:08:09 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-27 03:08:09 +0300 |
commit | 6348b76e4b4dd4e398915c3150c1d02aafa3f13b (patch) | |
tree | c8c53d4adac92d4576d73d33ebeeaa62a2626b12 /app/services/snippets | |
parent | 03a70b84edfb86bc27f9234e1f1d157d3a33c555 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/snippets')
-rw-r--r-- | app/services/snippets/create_service.rb | 59 |
1 files changed, 32 insertions, 27 deletions
diff --git a/app/services/snippets/create_service.rb b/app/services/snippets/create_service.rb index 389d4819c68..0b74bd77e28 100644 --- a/app/services/snippets/create_service.rb +++ b/app/services/snippets/create_service.rb @@ -9,72 +9,77 @@ module Snippets def execute filter_spam_check_params - snippet = if project - project.snippets.build(params) - else - PersonalSnippet.new(params) - end + @snippet = if project + project.snippets.build(params) + else + PersonalSnippet.new(params) + end - unless Gitlab::VisibilityLevel.allowed_for?(current_user, snippet.visibility_level) - deny_visibility_level(snippet) + unless Gitlab::VisibilityLevel.allowed_for?(current_user, @snippet.visibility_level) + deny_visibility_level(@snippet) - return snippet_error_response(snippet, 403) + return snippet_error_response(@snippet, 403) end - snippet.author = current_user + @snippet.author = current_user - spam_check(snippet, current_user) + spam_check(@snippet, current_user) - if save_and_commit(snippet) - UserAgentDetailService.new(snippet, @request).create + if save_and_commit + UserAgentDetailService.new(@snippet, @request).create Gitlab::UsageDataCounters::SnippetCounter.count(:create) - ServiceResponse.success(payload: { snippet: snippet } ) + ServiceResponse.success(payload: { snippet: @snippet } ) else - snippet_error_response(snippet, 400) + snippet_error_response(@snippet, 400) end end private - def save_and_commit(snippet) - snippet_saved = snippet.with_transaction_returning_status do - snippet.save && snippet.store_mentions! + def save_and_commit + snippet_saved = @snippet.with_transaction_returning_status do + @snippet.save && @snippet.store_mentions! end if snippet_saved && Feature.enabled?(:version_snippets, current_user) - create_repository_for(snippet) - create_commit(snippet) + create_repository + create_commit end snippet_saved rescue => e # Rescuing all because we can receive Creation exceptions, GRPC exceptions, Git exceptions, ... - snippet.errors.add(:base, e.message) log_error(e.message) # If the commit action failed we need to remove the repository if exists - snippet.repository.remove if snippet.repository_exists? + @snippet.repository.remove if @snippet.repository_exists? # If the snippet was created, we need to remove it as we # would do like if it had had any validation error - snippet.delete if snippet.persisted? + # 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) false end - def create_repository_for(snippet) - snippet.create_repository + def create_repository + @snippet.create_repository - raise CreateRepositoryError, 'Repository could not be created' unless snippet.repository_exists? + raise CreateRepositoryError, 'Repository could not be created' unless @snippet.repository_exists? end - def create_commit(snippet) + def create_commit commit_attrs = { branch_name: 'master', message: 'Initial commit' } - snippet.snippet_repository.multi_files_action(current_user, snippet_files, commit_attrs) + @snippet.snippet_repository.multi_files_action(current_user, snippet_files, commit_attrs) end def snippet_files |