summaryrefslogtreecommitdiff
path: root/app/services/system_notes/incidents_service.rb
blob: d5da684a2d880501a7de6930fa8e6acb6803529c (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
# frozen_string_literal: true

module SystemNotes
  class IncidentsService < ::SystemNotes::BaseService
    CHANGED_TEXT = {
      occurred_at: 'the event time/date on ',
      note: 'the text on ',
      occurred_at_and_note: 'the event time/date and text on '
    }.freeze

    def initialize(noteable:)
      @noteable = noteable
      @project = noteable.project
    end

    def add_timeline_event(timeline_event)
      author = timeline_event.author
      anchor = "timeline_event_#{timeline_event.id}"
      path = url_helpers.project_issues_incident_path(project, noteable, anchor: anchor)
      body = "added an [incident timeline event](#{path})"

      create_note(NoteSummary.new(noteable, project, author, body, action: 'timeline_event'))
    end

    def edit_timeline_event(timeline_event, author, was_changed:)
      anchor = "timeline_event_#{timeline_event.id}"
      path = url_helpers.project_issues_incident_path(project, noteable, anchor: anchor)
      changed_text = CHANGED_TEXT.fetch(was_changed, '')
      body = "edited #{changed_text}[incident timeline event](#{path})"

      create_note(NoteSummary.new(noteable, project, author, body, action: 'timeline_event'))
    end

    def delete_timeline_event(author)
      body = 'deleted an incident timeline event'

      create_note(NoteSummary.new(noteable, project, author, body, action: 'timeline_event'))
    end
  end
end