Sha256: c12494b1bb1d69a45bacdf35e606f77e88821e787502a6ad375f00971a7f1c98

Contents?: true

Size: 1.73 KB

Versions: 14

Compression:

Stored size: 1.73 KB

Contents

module Vidar
  class SlackNotification
    def initialize(github:, revision:, revision_name:, deploy_config:)
      @github          = github
      @revision        = revision
      @revision_name   = revision_name
      @deploy_name     = deploy_config.name
      @deploy_url      = deploy_config.url
      @success_color   = deploy_config.success_color
      @failure_color   = deploy_config.failure_color
      @webhook_url     = deploy_config.slack_webhook_url
      @connection      = Faraday.new
    end

    def configured?
      !webhook_url.to_s.empty?
    end

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

    def success
      message = "Successful deploy of #{github_link} to #{deploy_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 :github, :revision, :revision_name,
      :deploy_name, :deploy_url, :webhook_url,
      :success_color, :failure_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 deploy_link
      "<#{deploy_url}|#{deploy_name}>"
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
vidar-0.12.1 lib/vidar/slack_notification.rb
vidar-0.12.0 lib/vidar/slack_notification.rb
vidar-0.11.0 lib/vidar/slack_notification.rb
vidar-0.10.2 lib/vidar/slack_notification.rb
vidar-0.10.1 lib/vidar/slack_notification.rb
vidar-0.10.0 lib/vidar/slack_notification.rb
vidar-0.9.0 lib/vidar/slack_notification.rb
vidar-0.8.2 lib/vidar/slack_notification.rb
vidar-0.7.2 lib/vidar/slack_notification.rb
vidar-0.7.1 lib/vidar/slack_notification.rb
vidar-0.7.0 lib/vidar/slack_notification.rb
vidar-0.6.0 lib/vidar/slack_notification.rb
vidar-0.5.1 lib/vidar/slack_notification.rb
vidar-0.5.0 lib/vidar/slack_notification.rb