class Vidar::SlackNotification

Attributes

build_url[R]
connection[R]
default_color[R]
deploy_name[R]
deploy_url[R]
failure_color[R]
github[R]
revision[R]
revision_name[R]
success_color[R]
webhook_url[R]

Public Class Methods

new(github:, revision:, revision_name:, deploy_config:, build_url: nil, connection: Faraday.new) click to toggle source
# File lib/vidar/slack_notification.rb, line 3
def initialize(github:, revision:, revision_name:, deploy_config:, build_url: nil, connection: Faraday.new)
  @github = github
  @revision = revision
  @revision_name = revision_name
  @build_url = build_url
  @deploy_name = deploy_config.name
  @deploy_url = deploy_config.url
  @default_color = deploy_config.default_color
  @success_color = deploy_config.success_color
  @failure_color = deploy_config.failure_color
  @webhook_url = deploy_config.slack_webhook_url
  @connection = connection
end

Public Instance Methods

configured?() click to toggle source
# File lib/vidar/slack_notification.rb, line 17
def configured?
  !webhook_url.to_s.empty?
end
deliver(message:, color: default_color) click to toggle source
# File lib/vidar/slack_notification.rb, line 38
def deliver(message:, color: default_color)
  perform_with data(message: message, color: color)
end
failure() click to toggle source
# File lib/vidar/slack_notification.rb, line 21
def failure
  message = [
    "Failed deploy of #{github_link} to #{deploy_link}.",
    ":fire: <!channel>",
    build_link
  ]
  perform_with data(message: message, color: failure_color)
end
perform_with(data) click to toggle source
# File lib/vidar/slack_notification.rb, line 42
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
success() click to toggle source
# File lib/vidar/slack_notification.rb, line 30
def success
  message = [
    "Successful deploy of #{github_link} to #{deploy_link}.",
    build_link
  ]
  perform_with data(message: message, color: success_color)
end

Private Instance Methods

data(message:, color:) click to toggle source
# File lib/vidar/slack_notification.rb, line 57
def data(message:, color:)
  text = [message].flatten.compact.join("\n")
  {
    "attachments": [
      {
        "title": github,
        "title_link": github_url,
        "color": color,
        "text": text,
        "fallback": text,
      }
    ]
  }
end
github_url() click to toggle source
# File lib/vidar/slack_notification.rb, line 72
def github_url
  "https://github.com/#{github}/commit/#{revision}"
end