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
|
2020-09-10 17:08:28 -04:00
|
|
|
include IncidentManagement::UsageData
|
|
|
|
|
2012-07-31 01:32:49 -04:00
|
|
|
def execute
|
2019-12-20 07:07:40 -05:00
|
|
|
note = Notes::BuildService.new(project, current_user, params.except(:merge_request_diff_head_sha)).execute
|
2017-09-19 06:55:37 -04:00
|
|
|
|
2019-09-18 10:02:45 -04:00
|
|
|
# n+1: https://gitlab.com/gitlab-org/gitlab-foss/issues/37440
|
2017-09-19 06:55:37 -04:00
|
|
|
note_valid = Gitlab::GitalyClient.allow_n_plus_1_calls do
|
2019-12-12 19:08:05 -05:00
|
|
|
# We may set errors manually in Notes::BuildService for this reason
|
|
|
|
# we also need to check for already existing errors.
|
|
|
|
note.errors.empty? && note.valid?
|
2017-09-19 06:55:37 -04:00
|
|
|
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!
|
2016-08-12 21:17:18 -04:00
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
execute_quick_actions(note) do |only_commands|
|
|
|
|
note.run_after_commit do
|
|
|
|
# Finish the harder work in the background
|
|
|
|
NewNoteWorker.perform_async(note.id)
|
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
note_saved = note.with_transaction_returning_status do
|
|
|
|
!only_commands && note.save
|
|
|
|
end
|
2016-08-13 12:58:51 -04:00
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
when_saved(note) if note_saved
|
2016-08-13 12:58:51 -04:00
|
|
|
end
|
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
note
|
|
|
|
end
|
2016-10-13 11:26:44 -04:00
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
private
|
2019-12-10 10:07:52 -05:00
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
def execute_quick_actions(note)
|
|
|
|
return yield(false) unless quick_actions_service.supported?(note)
|
2019-02-06 05:31:46 -05:00
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
content, update_params, message = quick_actions_service.execute(note, quick_action_options)
|
|
|
|
only_commands = content.empty?
|
|
|
|
note.note = content
|
2019-10-31 17:06:28 -04:00
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
yield(only_commands)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
do_commands(note, update_params, message, only_commands)
|
|
|
|
end
|
2016-08-13 12:58:51 -04:00
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
def quick_actions_service
|
|
|
|
@quick_actions_service ||= QuickActionsService.new(project, current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def when_saved(note)
|
|
|
|
if note.part_of_discussion? && note.discussion.can_convert_to_discussion?
|
2020-08-18 17:09:57 -04:00
|
|
|
note.discussion.convert_to_discussion!.save
|
|
|
|
note.clear_memoization(:discussion)
|
2014-06-17 15:09:01 -04:00
|
|
|
end
|
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
todo_service.new_note(note, current_user)
|
|
|
|
clear_noteable_diffs_cache(note)
|
|
|
|
Suggestions::CreateService.new(note).execute
|
|
|
|
increment_usage_counter(note)
|
2020-09-10 17:08:28 -04:00
|
|
|
track_event(note, current_user)
|
2020-03-23 08:09:47 -04:00
|
|
|
|
|
|
|
if Feature.enabled?(:notes_create_service_tracking, project)
|
2020-12-09 22:09:53 -05:00
|
|
|
Gitlab::Tracking.event('Notes::CreateService', 'execute', **tracking_data_for(note))
|
2020-03-23 08:09:47 -04:00
|
|
|
end
|
2020-04-15 08:09:18 -04:00
|
|
|
|
2020-10-12 11:08:32 -04:00
|
|
|
if note.for_merge_request? && note.diff_note? && note.start_of_discussion?
|
2020-04-15 08:09:18 -04:00
|
|
|
Discussions::CaptureDiffNotePositionService.new(note.noteable, note.diff_file&.paths).execute(note.discussion)
|
|
|
|
end
|
2020-10-23 05:08:41 -04:00
|
|
|
|
|
|
|
track_note_creation_usage_for_issues(note) if note.for_issue?
|
2021-01-11 04:10:46 -05:00
|
|
|
track_note_creation_usage_for_merge_requests(note) if note.for_merge_request?
|
2012-07-31 01:32:49 -04:00
|
|
|
end
|
2019-10-31 17:06:28 -04:00
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
def do_commands(note, update_params, message, only_commands)
|
2020-08-12 02:09:53 -04:00
|
|
|
return if quick_actions_service.commands_executed_count.to_i == 0
|
2020-03-23 08:09:47 -04:00
|
|
|
|
|
|
|
if update_params.present?
|
|
|
|
quick_actions_service.apply_updates(update_params, note)
|
|
|
|
note.commands_changes = update_params
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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, message.presence || _('Failed to apply commands.'))
|
|
|
|
# Allow consumers to detect problems applying commands
|
|
|
|
note.errors.add(:commands, _('Failed to apply commands.')) unless message.present?
|
|
|
|
end
|
|
|
|
end
|
2019-10-31 17:06:28 -04:00
|
|
|
|
2019-12-20 07:07:40 -05:00
|
|
|
def quick_action_options
|
2020-05-27 14:08:14 -04:00
|
|
|
{
|
|
|
|
merge_request_diff_head_sha: params[:merge_request_diff_head_sha],
|
|
|
|
review_id: params[:review_id]
|
|
|
|
}
|
2019-12-20 07:07:40 -05:00
|
|
|
end
|
|
|
|
|
2019-10-31 17:06:28 -04:00
|
|
|
def tracking_data_for(note)
|
|
|
|
label = Gitlab.ee? && note.author == User.visual_review_bot ? 'anonymous_visual_review_note' : 'note'
|
|
|
|
|
|
|
|
{
|
|
|
|
label: label,
|
|
|
|
value: note.id
|
|
|
|
}
|
|
|
|
end
|
2020-09-10 17:08:28 -04:00
|
|
|
|
|
|
|
def track_event(note, user)
|
|
|
|
return unless note.noteable.is_a?(Issue) && note.noteable.incident?
|
|
|
|
|
2020-09-17 23:09:28 -04:00
|
|
|
track_usage_event(:incident_management_incident_comment, user.id)
|
2020-09-10 17:08:28 -04:00
|
|
|
end
|
2020-10-23 05:08:41 -04:00
|
|
|
|
|
|
|
def track_note_creation_usage_for_issues(note)
|
|
|
|
Gitlab::UsageDataCounters::IssueActivityUniqueCounter.track_issue_comment_added_action(author: note.author)
|
|
|
|
end
|
2021-01-11 04:10:46 -05:00
|
|
|
|
|
|
|
def track_note_creation_usage_for_merge_requests(note)
|
2021-01-14 16:10:37 -05:00
|
|
|
Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter.track_create_comment_action(note: note)
|
2021-01-11 04:10:46 -05:00
|
|
|
end
|
2012-07-31 01:32:49 -04:00
|
|
|
end
|
|
|
|
end
|