gitlab-org--gitlab-foss/app/services/notes/create_service.rb

58 lines
1.7 KiB
Ruby
Raw Normal View History

2012-07-31 05:32:49 +00:00
module Notes
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-01-05 13:36:06 +00:00
note = Note.new(params)
note.project = project
note.author = current_user
note.system = false
2014-06-17 19:09:01 +00:00
2016-04-16 19:09:08 +00:00
if note.award_emoji?
2016-05-17 17:28:17 +00:00
noteable = note.noteable
if noteable.user_can_award?(current_user, note.award_emoji_name)
todo_service.new_award_emoji(noteable, current_user)
return noteable.create_award_emoji(note.award_emoji_name, current_user)
end
2016-04-16 19:09:08 +00:00
end
# 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)
2016-08-13 01:17:18 +00:00
if slash_commands_service.supported?(note)
2016-11-24 14:05:15 +00:00
options = { merge_request_diff_head_sha: merge_request_diff_head_sha }
content, command_params = slash_commands_service.extract_commands(note, options)
only_commands = content.empty?
note.note = content
end
note.run_after_commit do
# Finish the harder work in the background
NewNoteWorker.perform_async(note.id)
end
if !only_commands && note.save
todo_service.new_note(note, current_user)
end
if command_params.present?
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
note.errors.add(:commands_only, 'Commands applied')
end
2016-11-24 06:32:32 +00:00
note.commands_changes = command_params.keys
2014-06-17 19:09:01 +00:00
end
2012-07-31 05:32:49 +00:00
note
end
end
end