create_service.rb 3.5 KB
Newer Older
1 2
# frozen_string_literal: true

D
Dmitriy Zaporozhets 已提交
3
module Notes
4
  class CreateService < ::Notes::BaseService
D
Dmitriy Zaporozhets 已提交
5
    def execute
6
      note = Notes::BuildService.new(project, current_user, params.except(:merge_request_diff_head_sha)).execute
7

8
      # n+1: https://gitlab.com/gitlab-org/gitlab-foss/issues/37440
9
      note_valid = Gitlab::GitalyClient.allow_n_plus_1_calls do
10 11 12
        # 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?
13 14 15
      end

      return note unless note_valid
D
Dmitriy Zaporozhets 已提交
16

17 18 19
      # 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!
D
Douwe Maan 已提交
20

21 22 23 24 25
      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
26

27 28 29
        note_saved = note.with_transaction_returning_status do
          !only_commands && note.save
        end
30

31
        when_saved(note) if note_saved
32 33
      end

34 35
      note
    end
36

37
    private
38

39 40
    def execute_quick_actions(note)
      return yield(false) unless quick_actions_service.supported?(note)
41

42 43 44
      content, update_params, message = quick_actions_service.execute(note, quick_action_options)
      only_commands = content.empty?
      note.note = content
45

46
      yield(only_commands)
47

48 49
      do_commands(note, update_params, message, only_commands)
    end
50

51 52 53 54 55 56 57
    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?
        note.discussion.convert_to_discussion!(save: true)
D
Dmitriy Zaporozhets 已提交
58 59
      end

60 61 62 63 64 65 66 67
      todo_service.new_note(note, current_user)
      clear_noteable_diffs_cache(note)
      Suggestions::CreateService.new(note).execute
      increment_usage_counter(note)

      if Feature.enabled?(:notes_create_service_tracking, project)
        Gitlab::Tracking.event('Notes::CreateService', 'execute', tracking_data_for(note))
      end
68 69 70 71

      if Feature.enabled?(:merge_ref_head_comments, project) && note.for_merge_request? && note.diff_note? && note.start_of_discussion?
        Discussions::CaptureDiffNotePositionService.new(note.noteable, note.diff_file&.paths).execute(note.discussion)
      end
D
Dmitriy Zaporozhets 已提交
72
    end
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    def do_commands(note, update_params, message, only_commands)
      return if quick_actions_service.commands_executed_count.to_i.zero?

      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
90

91 92 93 94 95
    # EE::Notes::CreateService would override this method
    def quick_action_options
      { merge_request_diff_head_sha: params[:merge_request_diff_head_sha] }
    end

96 97 98 99 100 101 102 103
    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
D
Dmitriy Zaporozhets 已提交
104 105
  end
end
106 107

Notes::CreateService.prepend_if_ee('EE::Notes::CreateService')