Sha256: 50175274e5c01d347e3c34c573758fe68b8ff31c7e41c69fe41e294b59f7a04b

Contents?: true

Size: 1.9 KB

Versions: 7

Compression:

Stored size: 1.9 KB

Contents

module Vidar
  class SlackNotification
    SUCCESS_COLOR = "good".freeze
    ERROR_COLOR = "danger".freeze

    def initialize(
      webhook_url:,
      github:,
      revision:,
      revision_name:,
      cluster_label:,
      cluster_url:,
      success_color: nil,
      error_color: nil
    )

      @webhook_url     = webhook_url
      @github          = github
      @revision        = revision
      @revision_name   = revision_name
      @cluster_label   = cluster_label
      @cluster_url     = cluster_url
      @success_color   = success_color || SUCCESS_COLOR
      @error_color     = error_color || ERROR_COLOR
      @connection      = Faraday.new
    end

    def configured?
      !webhook_url.to_s.empty?
    end

    def error
      message = "Failed deploy of #{github_link} to #{cluster_link} :fire: <!channel>"
      perform_with data(message: message, color: ERROR_COLOR)
    end

    def success
      message = "Successful deploy of #{github_link} to #{cluster_link}"
      perform_with data(message: message, color: success_color)
    end

    def perform_with(data)
      connection.post do |req|
        req.url webhook_url
        req.headers['Content-Type'] = 'application/json'
        req.body = data.to_json
      end
    end

    private

    attr_reader :webhook_url, :github,
      :revision, :revision_name,
      :cluster_label, :cluster_url,
      :success_color, :error_color,
      :connection

    def data(message:, color:)
      {
        "attachments": [
          {
            "title": github,
            "title_link": github_url,
            "color": color,
            "text": message,
            "fallback": message
          }
        ]
      }
    end

    def github_url
      "https://github.com/#{github}/commit/#{revision}"
    end

    def github_link
      "<#{github_url}|#{revision_name}>"
    end

    def cluster_link
      "<#{cluster_url}|#{cluster_label}>"
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
vidar-0.4.4 lib/vidar/slack_notification.rb
vidar-0.4.3 lib/vidar/slack_notification.rb
vidar-0.4.1 lib/vidar/slack_notification.rb
vidar-0.4.0 lib/vidar/slack_notification.rb
vidar-0.3.4 lib/vidar/slack_notification.rb
vidar-0.3.3 lib/vidar/slack_notification.rb
vidar-0.3.2 lib/vidar/slack_notification.rb