Skip to content

Commit

Permalink
Merge pull request #16 from joshpuetz/migration-announcement
Browse files Browse the repository at this point in the history
Migration announcement
  • Loading branch information
Justin McNally committed May 8, 2014
2 parents 97b818d + 8a666fb commit 5514c68
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions lib/capistrano/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ def self.extended(configuration)
configuration.load do

before 'deploy', 'slack:starting'
before 'deploy:migrations', 'slack:starting'
before 'deploy:migrations', 'slack:configure_for_migrations', 'slack:starting'
after 'deploy', 'slack:finished'
after 'deploy:migrations', 'slack:finished'
after 'deploy:migrations', 'slack:configure_for_migrations', 'slack:finished'

set :deployer do
ENV['GIT_AUTHOR_NAME'] || `git config user.name`.chomp
end
set :slack_with_migrations, false


namespace :slack do
task :configure_for_migrations do
set :slack_with_migrations, true
end

task :starting do
slack_token = fetch(:slack_token)
slack_room = fetch(:slack_room)
Expand All @@ -32,11 +37,13 @@ def self.extended(configuration)
announced_stage = fetch(:stage, 'production')

announcement = if fetch(:branch, nil)
"#{announced_deployer} is deploying #{slack_application}'s #{branch} to #{announced_stage}"
"#{announced_deployer} is deploying #{slack_application}'s #{branch}"
else
"#{announced_deployer} is deploying #{slack_application} to #{announced_stage}"
"#{announced_deployer} is deploying #{slack_application}"
end

announcement << " with migrations" if slack_with_migrations
announcement << " to #{announced_stage}"


# Parse the API url and create an SSL connection
uri = URI.parse("https://#{slack_subdomain}.slack.com/services/hooks/incoming-webhook?token=#{slack_token}")
Expand All @@ -60,17 +67,24 @@ def self.extended(configuration)
slack_token = fetch(:slack_token)
slack_room = fetch(:slack_room)
slack_emoji = fetch(:slack_emoji) || ":ghost:"
slack_username = fetch(:slack_username) || "deploybot"
slack_application = fetch(:slack_application) || application
slack_username = fetch(:slack_username) || "deploybot"
slack_application = fetch(:slack_application) || application
slack_subdomain = fetch(:slack_subdomain)
return if slack_token.nil?
announced_deployer = fetch(:deployer)
announced_stage = fetch(:stage, 'production')
end_time = Time.now
start_time = fetch(:start_time)
elapsed = end_time.to_i - start_time.to_i

msg = "#{announced_deployer} deployed #{slack_application} successfully in #{elapsed} seconds."


msg = if fetch(:branch, nil)
"#{announced_deployer} deployed #{slack_application}'s #{branch}"
else
"#{announced_deployer} deployed #{slack_application}"
end
msg << " with migrations" if slack_with_migrations
msg << " to #{announced_stage} successfully in #{elapsed} seconds"

# Parse the URI and handle the https connection
uri = URI.parse("https://#{slack_subdomain}.slack.com/services/hooks/incoming-webhook?token=#{slack_token}")
http = Net::HTTP.new(uri.host, uri.port)
Expand All @@ -80,7 +94,7 @@ def self.extended(configuration)
# Create the post request and setup the form data
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(:payload => {'channel' => slack_room, 'username' => slack_username, 'text' => msg, "icon_emoji" => slack_emoji}.to_json)

# Make the actual request to the API
response = http.request(request)

Expand All @@ -98,4 +112,3 @@ def self.extended(configuration)
if Capistrano::Configuration.instance
Capistrano::Configuration.instance.extend(Capistrano::Slack)
end

0 comments on commit 5514c68

Please sign in to comment.