class CruisecontrolrbToSlack::Runner

Constants

FAILED_EMOJIS

Attributes

config[R]
current_activities[RW]
previous_statuses[RW]

Public Class Methods

new(config) click to toggle source
# File lib/cruisecontrolrb-to-slack.rb, line 21
def initialize(config)
  @config = config
  @previous_statuses = {}
  @current_activities = {}
 end
run(config) click to toggle source
# File lib/cruisecontrolrb-to-slack.rb, line 17
def self.run(config)
  new(config).execute
end

Public Instance Methods

build_end_message(status_hash) click to toggle source
# File lib/cruisecontrolrb-to-slack.rb, line 64
def build_end_message(status_hash)
  name = status_hash[:name]
  url = build_url_for_status(status_hash)

  project_details = fetch_details(name)

  color = (status_hash[:status] == "Success") ? "good" : "danger"
  emoji = (status_hash[:status] == "Success") ? ":tada:" : FAILED_EMOJIS[rand(3)]

  message = (status_hash[:status] == "Success") ? "<#{url}|Success!> #{name} is looking good. You are a stud! :D" : "You are a failure! #{name} is broken. <a href=\"#{url}\">See details</a> and fix it now! >:-(" 
  attachment = {
                fallback: project_details[:commit_comment],
                color: color,
                mrkdwn_in: ["text", "title", "fields", "fallback"],
                fields: [{title: 'Commit Msg:', value: project_details[:commit_comment]},
                          {title: 'Commit', value: "<#{project_details[:commit_url]}|#{project_details[:commit]}>", short: 'true'}, 
                          {title: 'Committer', value: project_details[:committer], short: 'true'}]
            }

  options = {icon_emoji: emoji, color: color, attachments: [attachment]}
  [message, options]
end
build_start_message(status_hash) click to toggle source
# File lib/cruisecontrolrb-to-slack.rb, line 58
def build_start_message(status_hash)
  msg = "CruiseControl has started to build project #{status_hash[:name]}. <#{build_url_for_status(status_hash)}|See details>"

  [msg,{icon_emoji: ':mega:'}]
end
build_url_for_status(status_hash) click to toggle source
# File lib/cruisecontrolrb-to-slack.rb, line 87
def build_url_for_status(status_hash)
  File.join(status_hash[:web_url].gsub("projects", "builds"), status_hash[:build_label])
end
cc_client() click to toggle source
# File lib/cruisecontrolrb-to-slack.rb, line 104
def cc_client
  @cc_client ||= Cruisecontrolrb.new(config[:cc_host], config[:cc_username], config[:cc_password])
end
check_statuses() click to toggle source
# File lib/cruisecontrolrb-to-slack.rb, line 37
def check_statuses
  statuses = cc_client.fetch_statuses
  unless statuses.empty?     

    statuses.each do |status_hash|   
      name = status_hash[:name]

      if status_hash[:activity] == "Building" and current_activities[name] != "Building"
        message, options = build_start_message(status_hash)
        submit_message(message, options)
        current_activities[name] = "Building"
      elsif (current_activities[name] == "Building" and status_hash[:activity] != "Building")
        current_activities[name] = status_hash[:activity]
        message, options = build_end_message(status_hash)
        submit_message(message, options)
      end

    end
  end
end
execute() click to toggle source
# File lib/cruisecontrolrb-to-slack.rb, line 27
def execute
  scheduler = Rufus::Scheduler.new(:blocking => true, :overlap => false)
  
  scheduler.every("#{config[:polling_interval]}s") do  
    check_statuses
  end

  scheduler.join
end
fetch_details(name) click to toggle source
# File lib/cruisecontrolrb-to-slack.rb, line 95
def fetch_details(name)
  cc_client.fetch_project_details(name)
end
slack_client() click to toggle source
# File lib/cruisecontrolrb-to-slack.rb, line 99
def slack_client
  @slack_client ||= SlackClient.new(config[:slack_webhook_url],
                            channel: config[:slack_channel], user: config[:slack_user])
end
submit_message(message, options) click to toggle source
# File lib/cruisecontrolrb-to-slack.rb, line 91
def submit_message(message, options)
  slack_client.send_message message, options
end