2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
module Notes
|
2018-12-13 13:39:08 +05:30
|
|
|
class CreateService < ::Notes::BaseService
|
2020-11-24 15:15:51 +05:30
|
|
|
include IncidentManagement::UsageData
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
def execute(skip_capture_diff_note_position: false, skip_merge_status_trigger: false)
|
2020-03-13 15:44:24 +05:30
|
|
|
note = Notes::BuildService.new(project, current_user, params.except(:merge_request_diff_head_sha)).execute
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
# n+1: https://gitlab.com/gitlab-org/gitlab-foss/issues/37440
|
2018-03-17 18:26:18 +05:30
|
|
|
note_valid = Gitlab::GitalyClient.allow_n_plus_1_calls do
|
2020-01-01 13:55:28 +05:30
|
|
|
# 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?
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
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!
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
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-09-13 17:45:13 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
note_saved = note.with_transaction_returning_status do
|
2021-03-11 19:13:27 +05:30
|
|
|
break false if only_commands
|
|
|
|
|
|
|
|
note.save.tap do
|
|
|
|
update_discussions(note)
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
if note_saved
|
|
|
|
when_saved(
|
|
|
|
note,
|
|
|
|
skip_capture_diff_note_position: skip_capture_diff_note_position,
|
|
|
|
skip_merge_status_trigger: skip_merge_status_trigger
|
|
|
|
)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
note
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
private
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def execute_quick_actions(note)
|
2021-12-11 22:18:48 +05:30
|
|
|
return yield(false) unless quick_actions_supported?(note)
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
content, update_params, message, command_names = quick_actions_service.execute(note, quick_action_options)
|
2020-04-22 19:07:51 +05:30
|
|
|
only_commands = content.empty?
|
|
|
|
note.note = content
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
yield(only_commands)
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
do_commands(note, update_params, message, command_names, only_commands)
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
def quick_actions_supported?(note)
|
|
|
|
quick_actions_service.supported?(note)
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
def quick_actions_service
|
|
|
|
@quick_actions_service ||= QuickActionsService.new(project, current_user)
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
def update_discussions(note)
|
|
|
|
# Ensure that individual notes that are promoted into discussions are
|
|
|
|
# updated in a transaction with the note creation to avoid inconsistencies:
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/301237
|
2020-04-22 19:07:51 +05:30
|
|
|
if note.part_of_discussion? && note.discussion.can_convert_to_discussion?
|
2020-10-24 23:57:45 +05:30
|
|
|
note.discussion.convert_to_discussion!.save
|
|
|
|
note.clear_memoization(:discussion)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
def when_saved(note, skip_capture_diff_note_position: false, skip_merge_status_trigger: false)
|
2020-04-22 19:07:51 +05:30
|
|
|
todo_service.new_note(note, current_user)
|
|
|
|
clear_noteable_diffs_cache(note)
|
|
|
|
Suggestions::CreateService.new(note).execute
|
|
|
|
increment_usage_counter(note)
|
2020-11-24 15:15:51 +05:30
|
|
|
track_event(note, current_user)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
if note.for_merge_request? && note.start_of_discussion?
|
|
|
|
if !skip_capture_diff_note_position && note.diff_note?
|
|
|
|
Discussions::CaptureDiffNotePositionService.new(note.noteable, note.diff_file&.paths).execute(note.discussion)
|
|
|
|
end
|
|
|
|
|
|
|
|
if !skip_merge_status_trigger && note.to_be_resolved?
|
|
|
|
GraphqlTriggers.merge_request_merge_status_updated(note.noteable)
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
def do_commands(note, update_params, message, command_names, only_commands)
|
2020-10-24 23:57:45 +05:30
|
|
|
return if quick_actions_service.commands_executed_count.to_i == 0
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
if update_params.present?
|
2022-11-25 23:54:43 +05:30
|
|
|
invalid_message = validate_commands(note, update_params)
|
|
|
|
|
|
|
|
if invalid_message
|
|
|
|
note.errors.add(:validation, invalid_message)
|
|
|
|
message = invalid_message
|
|
|
|
else
|
2022-08-27 11:52:29 +05:30
|
|
|
quick_actions_service.apply_updates(update_params, note)
|
|
|
|
note.commands_changes = update_params
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
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.'))
|
2022-07-16 23:28:13 +05:30
|
|
|
note.errors.add(:command_names, command_names.flatten)
|
2020-04-22 19:07:51 +05:30
|
|
|
# Allow consumers to detect problems applying commands
|
|
|
|
note.errors.add(:commands, _('Failed to apply commands.')) unless message.present?
|
|
|
|
end
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def quick_action_options
|
2020-06-23 00:09:42 +05:30
|
|
|
{
|
|
|
|
merge_request_diff_head_sha: params[:merge_request_diff_head_sha],
|
|
|
|
review_id: params[:review_id]
|
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
def validate_commands(note, update_params)
|
|
|
|
if invalid_reviewers?(update_params)
|
|
|
|
"Reviewers #{note.noteable.class.max_number_of_assignees_or_reviewers_message}"
|
|
|
|
elsif invalid_assignees?(update_params)
|
|
|
|
"Assignees #{note.noteable.class.max_number_of_assignees_or_reviewers_message}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_reviewers?(update_params)
|
|
|
|
return false unless Feature.enabled?(:limit_reviewer_and_assignee_size)
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
if update_params.key?(:reviewer_ids)
|
|
|
|
possible_reviewers = update_params[:reviewer_ids]&.uniq&.size
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
possible_reviewers > ::Issuable::MAX_NUMBER_OF_ASSIGNEES_OR_REVIEWERS
|
|
|
|
else
|
|
|
|
false
|
2022-08-27 11:52:29 +05:30
|
|
|
end
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def invalid_assignees?(update_params)
|
|
|
|
if update_params.key?(:assignee_ids)
|
|
|
|
possible_assignees = update_params[:assignee_ids]&.uniq&.size
|
|
|
|
|
|
|
|
possible_assignees > ::Issuable::MAX_NUMBER_OF_ASSIGNEES_OR_REVIEWERS
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2022-08-27 11:52:29 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def track_event(note, user)
|
|
|
|
track_note_creation_usage_for_issues(note) if note.for_issue?
|
|
|
|
track_note_creation_usage_for_merge_requests(note) if note.for_merge_request?
|
2022-06-21 17:19:12 +05:30
|
|
|
track_incident_action(user, note.noteable, 'incident_comment') if note.for_issue?
|
2022-07-16 23:28:13 +05:30
|
|
|
track_note_creation_in_ipynb(note)
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
if Feature.enabled?(:notes_create_service_tracking, project)
|
|
|
|
Gitlab::Tracking.event('Notes::CreateService', 'execute', **tracking_data_for(note))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
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-11-24 15:15:51 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
def track_note_creation_usage_for_issues(note)
|
2022-08-27 11:52:29 +05:30
|
|
|
Gitlab::UsageDataCounters::IssueActivityUniqueCounter.track_issue_comment_added_action(author: note.author,
|
|
|
|
project: project)
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
2021-03-08 18:12:59 +05:30
|
|
|
|
|
|
|
def track_note_creation_usage_for_merge_requests(note)
|
|
|
|
Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter.track_create_comment_action(note: note)
|
|
|
|
end
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
def should_track_ipynb_notes?(note)
|
|
|
|
Feature.enabled?(:ipynbdiff_notes_tracker) && note.respond_to?(:diff_file) && note.diff_file&.ipynb?
|
|
|
|
end
|
|
|
|
|
|
|
|
def track_note_creation_in_ipynb(note)
|
|
|
|
return unless should_track_ipynb_notes?(note)
|
|
|
|
|
|
|
|
Gitlab::UsageDataCounters::IpynbDiffActivityCounter.note_created(note)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
Notes::CreateService.prepend_mod_with('Notes::CreateService')
|