Sha256: 1de7175b3615aafe5249997520eefed4d85402d98885422a47f17a226e7eb2c8

Contents?: true

Size: 1.53 KB

Versions: 15

Compression:

Stored size: 1.53 KB

Contents

require 'slack-notifier'

class SlackNotifier
  def initialize(webhook_url, deploy_info)
    @notifier = Slack::Notifier.new webhook_url
    @notifier.username = 'Tape'
    @deploy_info = deploy_info
  end

  def update(status)
    @status = status
    @notifier.ping(
      "",
      # TODO: Fill in real icon url
      icon_url: 'https://image.freepik.com/free-icon/adhesive-tape_318-42276.png',
      attachments: attachments
    )
  end

  private

  def attachments
    a = {}
    a[:text] = message
    a[:color] = color
    a[:fields] = fields unless @status == :start
    [a]
  end

  def fields
    [
      {
        title: "Project",
        value: project_link,
        short: true
      },
      {
        title: "Hosts/Env",
        value: @deploy_info[:hosts],
        short: true
      },
      {
        title: "Author",
        value: @deploy_info[:user],
        short: true
      }
    ]
  end

  def color
    case @status
    when :start   then "#a9a9a9"
    when :success then "good"
    when :fail    then "danger"
    end
  end

  def gh_link_base
    @deploy_info[:repo].sub(/^git@github.com:/, 'http://github.com/').sub(/.git$/, '')
  end

  def project_link
    "<#{gh_link_base}|#{@deploy_info[:app_name]}>"
  end

  def message
    case @status
    when :start
      user  = @deploy_info[:user]
      app   = @deploy_info[:app_name]
      hosts = @deploy_info[:hosts]
      "#{user} started deploying #{app} to #{hosts}"
    when :success
      "The deploy was successful!"
    when :fail
      "The deploy failed!"
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
taperole-1.6.0 lib/tape/notifiers/slack.rb
taperole-1.5.5 lib/tape/notifiers/slack.rb
taperole-1.5.4 lib/tape/notifiers/slack.rb
taperole-1.5.3 lib/tape/notifiers/slack.rb
taperole-1.5.2 lib/tape/notifiers/slack.rb
taperole-1.5.1 lib/tape/notifiers/slack.rb
taperole-1.5.0 lib/tape/notifiers/slack.rb
taperole-1.4.3 lib/tape/notifiers/slack.rb
taperole-1.4.2 lib/tape/notifiers/slack.rb
taperole-1.4.1 lib/tape/notifiers/slack.rb
taperole-1.4.0 lib/tape/notifiers/slack.rb
taperole-1.3.6 lib/tape/notifiers/slack.rb
taperole-1.3.5 lib/tape/notifiers/slack.rb
taperole-1.3.4 lib/tape/notifiers/slack.rb
taperole-1.3.3 lib/tape/notifiers/slack.rb