debian-mirror-gitlab/app/services/notes/create_service.rb

48 lines
1.4 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
module Notes
2017-08-17 22:00:37 +05:30
class CreateService < ::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
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!
slash_commands_service = SlashCommandsService.new(project, current_user)
if slash_commands_service.supported?(note)
2017-08-17 22:00:37 +05:30
options = { merge_request_diff_head_sha: merge_request_diff_head_sha }
content, command_params = slash_commands_service.extract_commands(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
2016-09-13 17:45:13 +05:30
todo_service.new_note(note, current_user)
end
2017-08-17 22:00:37 +05:30
if command_params.present?
2016-09-13 17:45:13 +05:30
slash_commands_service.execute(command_params, note)
# We must add the error after we call #save because errors are reset
# when #save is called
if only_commands
2017-08-17 22:00:37 +05:30
note.errors.add(:commands_only, 'Commands applied')
2016-09-13 17:45:13 +05:30
end
2017-08-17 22:00:37 +05:30
note.commands_changes = command_params
2014-09-02 18:07:02 +05:30
end
note
end
end
end