2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2012-07-31 01:32:49 -04:00
|
|
|
module Notes
|
2018-10-16 12:21:16 -04:00
|
|
|
class CreateService < ::Notes::BaseService
|
2012-07-31 01:32:49 -04:00
|
|
|
def execute
|
2016-11-24 09:05:15 -05:00
|
|
|
merge_request_diff_head_sha = params.delete(:merge_request_diff_head_sha)
|
|
|
|
|
2017-03-09 20:29:11 -05:00
|
|
|
note = Notes::BuildService.new(project, current_user, params).execute
|
2017-09-19 06:55:37 -04:00
|
|
|
|
|
|
|
# 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
|
2014-06-17 15:09:01 -04:00
|
|
|
|
2016-06-30 11:34:19 -04:00
|
|
|
# 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-05-31 01:50:53 -04:00
|
|
|
quick_actions_service = QuickActionsService.new(project, current_user)
|
2016-08-12 21:17:18 -04:00
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
if quick_actions_service.supported?(note)
|
2016-11-24 09:05:15 -05:00
|
|
|
options = { merge_request_diff_head_sha: merge_request_diff_head_sha }
|
2019-07-29 18:35:29 -04:00
|
|
|
content, update_params, message = quick_actions_service.execute(note, options)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
2016-08-13 12:58:51 -04:00
|
|
|
only_commands = content.empty?
|
|
|
|
|
|
|
|
note.note = content
|
|
|
|
end
|
|
|
|
|
2016-10-13 11:26:44 -04:00
|
|
|
note.run_after_commit do
|
2016-01-27 10:59:16 -05:00
|
|
|
# Finish the harder work in the background
|
2016-10-13 11:26:44 -04:00
|
|
|
NewNoteWorker.perform_async(note.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
if !only_commands && note.save
|
2019-02-06 05:31:46 -05:00
|
|
|
if note.part_of_discussion? && note.discussion.can_convert_to_discussion?
|
2019-02-06 01:11:33 -05:00
|
|
|
note.discussion.convert_to_discussion!(save: true)
|
2019-02-06 05:31:46 -05:00
|
|
|
end
|
|
|
|
|
2016-06-30 11:34:19 -04:00
|
|
|
todo_service.new_note(note, current_user)
|
2018-10-16 12:21:16 -04:00
|
|
|
clear_noteable_diffs_cache(note)
|
2018-12-13 14:17:19 -05:00
|
|
|
Suggestions::CreateService.new(note).execute
|
2019-08-08 09:18:57 -04:00
|
|
|
increment_usage_counter(note)
|
2016-06-30 11:34:19 -04:00
|
|
|
end
|
|
|
|
|
2019-03-04 04:21:47 -05:00
|
|
|
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-08-13 12:58:51 -04:00
|
|
|
|
|
|
|
# We must add the error after we call #save because errors are reset
|
|
|
|
# when #save is called
|
|
|
|
if only_commands
|
2019-08-12 20:40:39 -04:00
|
|
|
note.errors.add(:commands_only, message.presence || _('Failed to apply commands.'))
|
2016-08-13 12:58:51 -04:00
|
|
|
end
|
2014-06-17 15:09:01 -04:00
|
|
|
end
|
|
|
|
|
2012-07-31 01:32:49 -04:00
|
|
|
note
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|