Welcome to mirror list, hosted at ThFree Co, Russian Federation.

alert_message.rb « chat_message « project_services « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c89137758439c535ccfaa70a9bf0ba33890defb2 (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
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true

module ChatMessage
  class AlertMessage < BaseMessage
    attr_reader :title
    attr_reader :alert_url
    attr_reader :severity
    attr_reader :events
    attr_reader :status
    attr_reader :started_at

    def initialize(params)
      @project_name = params[:project_name] || params.dig(:project, :path_with_namespace)
      @project_url = params.dig(:project, :web_url) || params[:project_url]
      @title = params.dig(:object_attributes, :title)
      @alert_url = params.dig(:object_attributes, :url)
      @severity = params.dig(:object_attributes, :severity)
      @events = params.dig(:object_attributes, :events)
      @status = params.dig(:object_attributes, :status)
      @started_at = params.dig(:object_attributes, :started_at)
    end

    def attachments
      [{
        title: title,
        title_link: alert_url,
        color: attachment_color,
        fields: attachment_fields
      }]
    end

    def message
      "Alert firing in #{project_name}"
    end

    private

    def attachment_color
      "#C95823"
    end

    def attachment_fields
      [
        {
          title: "Severity",
          value: severity.to_s.humanize,
          short: true
        },
        {
          title: "Events",
          value: events,
          short: true
        },
        {
          title: "Status",
          value: status.to_s.humanize,
          short: true
        },
        {
          title: "Start time",
          value: format_time(started_at),
          short: true
        }
      ]
    end

    # This formats time into the following format
    # April 23rd, 2020 1:06AM UTC
    def format_time(time)
      time = Time.zone.parse(time.to_s)
      time.strftime("%B #{time.day.ordinalize}, %Y %l:%M%p %Z")
    end
  end
end