summaryrefslogtreecommitdiff
path: root/lib/gitlab/incident_management/pager_duty/incident_issue_description.rb
blob: 768c8bb4cbb4f0cbc7729dfb2e53fd122fa9a637 (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
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true

module Gitlab
  module IncidentManagement
    module PagerDuty
      class IncidentIssueDescription
        def initialize(incident_payload)
          @incident_payload = incident_payload
        end

        def to_s
          markdown_line_break = "  \n"

          [
            "**Incident:** #{markdown_incident}",
            "**Incident number:** #{incident_payload['incident_number']}",
            "**Urgency:** #{incident_payload['urgency']}",
            "**Status:** #{incident_payload['status']}",
            "**Incident key:** #{incident_payload['incident_key']}",
            "**Created at:** #{markdown_incident_created_at}",
            "**Assignees:** #{markdown_assignees.join(', ')}",
            "**Impacted services:** #{markdown_impacted_services.join(', ')}"
          ].join(markdown_line_break)
        end

        private

        attr_reader :incident_payload

        def markdown_incident
          markdown_link(incident_payload['title'], incident_payload['url'])
        end

        def incident_created_at
          Time.zone.parse(incident_payload['created_at'])
        rescue
          Time.current.utc # PagerDuty provides time in UTC
        end

        def markdown_incident_created_at
          incident_created_at.strftime('%d %B %Y, %-l:%M%p (%Z)')
        end

        def markdown_assignees
          Array(incident_payload['assignees']).map do |assignee|
            markdown_link(assignee['summary'], assignee['url'])
          end
        end

        def markdown_impacted_services
          Array(incident_payload['impacted_services']).map do |is|
            markdown_link(is['summary'], is['url'])
          end
        end

        def markdown_link(label, url)
          return label if url.blank?

          "[#{label}](#{url})"
        end
      end
    end
  end
end