summaryrefslogtreecommitdiff
path: root/app/services/notes/create_service.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-03-23 12:09:47 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-03-23 12:09:47 +0000
commit8f9beefac3774b30e911fb00a68f4c7a5244cf27 (patch)
tree919c3a043f8c10bc3f78f3f6e029acfb6b972556 /app/services/notes/create_service.rb
parente4bf776a8829e5186a0f63603c0be627b891d80e (diff)
downloadgitlab-ce-8f9beefac3774b30e911fb00a68f4c7a5244cf27.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/notes/create_service.rb')
-rw-r--r--app/services/notes/create_service.rb87
1 files changed, 51 insertions, 36 deletions
diff --git a/app/services/notes/create_service.rb b/app/services/notes/create_service.rb
index 4a0d85038ee..80bc4485988 100644
--- a/app/services/notes/create_service.rb
+++ b/app/services/notes/create_service.rb
@@ -17,57 +17,72 @@ module Notes
# We execute commands (extracted from `params[:note]`) on the noteable
# **before** we save the note because if the note consists of commands
# only, there is no need be create a note!
- quick_actions_service = QuickActionsService.new(project, current_user)
- if quick_actions_service.supported?(note)
- content, update_params, message = quick_actions_service.execute(note, quick_action_options)
+ execute_quick_actions(note) do |only_commands|
+ note.run_after_commit do
+ # Finish the harder work in the background
+ NewNoteWorker.perform_async(note.id)
+ end
- only_commands = content.empty?
+ note_saved = note.with_transaction_returning_status do
+ !only_commands && note.save
+ end
- note.note = content
+ when_saved(note) if note_saved
end
- note.run_after_commit do
- # Finish the harder work in the background
- NewNoteWorker.perform_async(note.id)
- end
+ note
+ end
- note_saved = note.with_transaction_returning_status do
- !only_commands && note.save
- end
+ private
- if note_saved
- if note.part_of_discussion? && note.discussion.can_convert_to_discussion?
- note.discussion.convert_to_discussion!(save: true)
- end
+ def execute_quick_actions(note)
+ return yield(false) unless quick_actions_service.supported?(note)
- todo_service.new_note(note, current_user)
- clear_noteable_diffs_cache(note)
- Suggestions::CreateService.new(note).execute
- increment_usage_counter(note)
+ content, update_params, message = quick_actions_service.execute(note, quick_action_options)
+ only_commands = content.empty?
+ note.note = content
- if Feature.enabled?(:notes_create_service_tracking, project)
- Gitlab::Tracking.event('Notes::CreateService', 'execute', tracking_data_for(note))
- end
- end
+ yield(only_commands)
- if quick_actions_service.commands_executed_count.to_i > 0
- if update_params.present?
- quick_actions_service.apply_updates(update_params, note)
- note.commands_changes = update_params
- end
+ do_commands(note, update_params, message, only_commands)
+ end
- # We must add the error after we call #save because errors are reset
- # when #save is called
- if only_commands
- note.errors.add(:commands_only, message.presence || _('Failed to apply commands.'))
- end
+ def quick_actions_service
+ @quick_actions_service ||= QuickActionsService.new(project, current_user)
+ end
+
+ def when_saved(note)
+ if note.part_of_discussion? && note.discussion.can_convert_to_discussion?
+ note.discussion.convert_to_discussion!(save: true)
end
- note
+ todo_service.new_note(note, current_user)
+ clear_noteable_diffs_cache(note)
+ Suggestions::CreateService.new(note).execute
+ increment_usage_counter(note)
+
+ if Feature.enabled?(:notes_create_service_tracking, project)
+ Gitlab::Tracking.event('Notes::CreateService', 'execute', tracking_data_for(note))
+ end
end
- private
+ def do_commands(note, update_params, message, only_commands)
+ return if quick_actions_service.commands_executed_count.to_i.zero?
+
+ if update_params.present?
+ quick_actions_service.apply_updates(update_params, note)
+ note.commands_changes = update_params
+ end
+
+ # We must add the error after we call #save because errors are reset
+ # when #save is called
+ if only_commands
+ note.errors.add(:commands_only, message.presence || _('Failed to apply commands.'))
+ # Allow consumers to detect problems applying commands
+ note.errors.add(:commands, _('Failed to apply commands.')) unless message.present?
+ end
+ end
# EE::Notes::CreateService would override this method
def quick_action_options