summaryrefslogtreecommitdiff
path: root/app/graphql/mutations/incident_management/timeline_event/promote_from_note.rb
blob: bb1da9278ffa1875d59591b87f9bf4e8cb94100f (plain)
1
2
3
4
5
6
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
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true

module Mutations
  module IncidentManagement
    module TimelineEvent
      class PromoteFromNote < Base
        graphql_name 'TimelineEventPromoteFromNote'

        include NotesHelper

        argument :note_id, Types::GlobalIDType[::Note],
                 required: true,
                 description: 'Note ID from which the timeline event promoted.'

        def resolve(note_id:)
          note = find_object(id: note_id)
          incident = note&.noteable

          authorize!(incident)

          response ::IncidentManagement::TimelineEvents::CreateService.new(
            incident,
            current_user,
            promoted_from_note: note,
            note: build_note_string(note),
            occurred_at: note.created_at,
            editable: true
          ).execute
        end

        private

        def find_object(id:)
          GitlabSchema.object_from_id(id, expected_type: ::Note).sync
        end

        def authorize!(object)
          raise_noteable_not_incident! if object && !object.try(:incident?)

          super
        end

        def build_note_string(note)
          commented = _('commented')
          "@#{note.author.username} [#{commented}](#{noteable_note_url(note)}): '#{note.note}'"
        end

        def raise_noteable_not_incident!
          raise_resource_not_available_error! 'Note does not belong to an incident'
        end
      end
    end
  end
end