2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
module Notes
|
2018-12-13 13:39:08 +05:30
|
|
|
class CreateService < ::Notes::BaseService
|
2014-09-02 18:07:02 +05:30
|
|
|
def execute
|
2017-08-17 22:00:37 +05:30
|
|
|
merge_request_diff_head_sha = params.delete(:merge_request_diff_head_sha)
|
|
|
|
|
|
|
|
note = Notes::BuildService.new(project, current_user, params).execute
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
# n+1: https://gitlab.com/gitlab-org/gitlab-ce/issues/37440
|
|
|
|
note_valid = Gitlab::GitalyClient.allow_n_plus_1_calls do
|
|
|
|
note.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
return note unless note_valid
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
# 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!
|
2017-09-10 17:25:29 +05:30
|
|
|
quick_actions_service = QuickActionsService.new(project, current_user)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
if quick_actions_service.supported?(note)
|
2017-08-17 22:00:37 +05:30
|
|
|
options = { merge_request_diff_head_sha: merge_request_diff_head_sha }
|
2019-10-12 21:52:04 +05:30
|
|
|
content, update_params, message = quick_actions_service.execute(note, options)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
only_commands = content.empty?
|
|
|
|
|
|
|
|
note.note = content
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
note.run_after_commit do
|
2016-04-02 18:10:28 +05:30
|
|
|
# Finish the harder work in the background
|
2017-08-17 22:00:37 +05:30
|
|
|
NewNoteWorker.perform_async(note.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
if !only_commands && note.save
|
2019-03-02 22:35:43 +05:30
|
|
|
if note.part_of_discussion? && note.discussion.can_convert_to_discussion?
|
2019-07-07 11:18:12 +05:30
|
|
|
note.discussion.convert_to_discussion!(save: true)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
todo_service.new_note(note, current_user)
|
2018-12-13 13:39:08 +05:30
|
|
|
clear_noteable_diffs_cache(note)
|
2019-02-15 15:39:39 +05:30
|
|
|
Suggestions::CreateService.new(note).execute
|
2019-10-12 21:52:04 +05:30
|
|
|
increment_usage_counter(note)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
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
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
# We must add the error after we call #save because errors are reset
|
|
|
|
# when #save is called
|
|
|
|
if only_commands
|
2019-10-12 21:52:04 +05:30
|
|
|
note.errors.add(:commands_only, message.presence || _('Failed to apply commands.'))
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
note
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|