2012-07-31 05:32:49 +00:00
|
|
|
module Notes
|
2017-04-04 22:27:23 +00:00
|
|
|
class CreateService < ::BaseService
|
2012-07-31 05:32:49 +00:00
|
|
|
def execute
|
2016-11-24 14:05:15 +00:00
|
|
|
merge_request_diff_head_sha = params.delete(:merge_request_diff_head_sha)
|
|
|
|
|
2017-03-10 01:29:11 +00:00
|
|
|
note = Notes::BuildService.new(project, current_user, params).execute
|
2017-09-19 10:55:37 +00: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 19:09:01 +00:00
|
|
|
|
2016-06-30 15:34:19 +00: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 05:50:53 +00:00
|
|
|
quick_actions_service = QuickActionsService.new(project, current_user)
|
2016-08-13 01:17:18 +00:00
|
|
|
|
2017-05-31 05:50:53 +00:00
|
|
|
if quick_actions_service.supported?(note)
|
2016-11-24 14:05:15 +00:00
|
|
|
options = { merge_request_diff_head_sha: merge_request_diff_head_sha }
|
2017-05-31 05:50:53 +00:00
|
|
|
content, command_params = quick_actions_service.extract_commands(note, options)
|
2016-06-30 15:34:19 +00:00
|
|
|
|
2016-08-13 16:58:51 +00:00
|
|
|
only_commands = content.empty?
|
|
|
|
|
|
|
|
note.note = content
|
|
|
|
end
|
|
|
|
|
2016-10-13 15:26:44 +00:00
|
|
|
note.run_after_commit do
|
2016-01-27 15:59:16 +00:00
|
|
|
# Finish the harder work in the background
|
2016-10-13 15:26:44 +00:00
|
|
|
NewNoteWorker.perform_async(note.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
if !only_commands && note.save
|
2016-06-30 15:34:19 +00:00
|
|
|
todo_service.new_note(note, current_user)
|
|
|
|
end
|
|
|
|
|
2016-11-19 02:19:04 +00:00
|
|
|
if command_params.present?
|
2017-05-31 05:50:53 +00:00
|
|
|
quick_actions_service.execute(command_params, note)
|
2016-08-13 16:58:51 +00:00
|
|
|
|
|
|
|
# We must add the error after we call #save because errors are reset
|
|
|
|
# when #save is called
|
|
|
|
if only_commands
|
2016-12-15 19:54:43 +00:00
|
|
|
note.errors.add(:commands_only, 'Commands applied')
|
2016-08-13 16:58:51 +00:00
|
|
|
end
|
2016-11-24 06:32:32 +00:00
|
|
|
|
2017-02-28 20:38:19 +00:00
|
|
|
note.commands_changes = command_params
|
2014-06-17 19:09:01 +00:00
|
|
|
end
|
|
|
|
|
2012-07-31 05:32:49 +00:00
|
|
|
note
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|