Class: Notes::CreateService

Inherits:
BaseService show all
Includes:
IncidentManagement::UsageData
Defined in:
app/services/notes/create_service.rb

Direct Known Subclasses

Import::Github::Notes::CreateService

Constant Summary

Constants included from Gitlab::Utils::UsageData

Gitlab::Utils::UsageData::DISTRIBUTED_HLL_FALLBACK, Gitlab::Utils::UsageData::FALLBACK, Gitlab::Utils::UsageData::HISTOGRAM_FALLBACK, Gitlab::Utils::UsageData::MAX_BUCKET_SIZE

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods included from IncidentManagement::UsageData

#track_incident_action

Methods included from Gitlab::Utils::UsageData

#add, #add_metric, #alt_usage_data, #count, #distinct_count, #epics_deepest_relationship_level, #estimate_batch_distinct_count, #histogram, #jira_integration_data, #maximum_id, #measure_duration, #minimum_id, #redis_usage_data, #sum, #track_usage_event, #with_duration, #with_finished_at, #with_prometheus_client

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods inherited from BaseService

#clear_noteable_diffs_cache, #increment_usage_counter

Methods inherited from BaseService

#initialize

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#execute(skip_capture_diff_note_position: false) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/notes/create_service.rb', line 7

def execute(skip_capture_diff_note_position: false)
  note = Notes::BuildService.new(project, current_user, params.except(:merge_request_diff_head_sha)).execute

  # n+1: https://gitlab.com/gitlab-org/gitlab-foss/issues/37440
  note_valid = Gitlab::GitalyClient.allow_n_plus_1_calls do
    # 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?
  end

  return note unless note_valid

  # 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!

  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

    note_saved = note.with_transaction_returning_status do
      break false if only_commands

      note.save.tap do
        update_discussions(note)
      end
    end

    when_saved(note, skip_capture_diff_note_position: skip_capture_diff_note_position) if note_saved
  end

  note
end