require 'flowdock' require 'grit' require 'digest/md5' require 'cgi' Capistrano::Configuration.instance(:must_exist).load do namespace :flowdock do task :read_current_deployed_branch do current_branch = capture("cat #{current_path}/BRANCH").chomp rescue "master" set :current_branch, current_branch end task :save_deployed_branch do run "echo '#{source.head}' > #{current_path}/BRANCH" end task :set_flowdock_api do set :rails_env, variables.include?(:stage) ? stage : ENV['RAILS_ENV'] set :repo, Grit::Repo.new(".") config = Grit::Config.new(repo) set :flowdock_api, Flowdock::Flow.new(:api_token => flowdock_api_token, :source => "Capistrano deployment", :project => flowdock_project_name, :from => {:name => config["user.name"], :address => config["user.email"]}) end task :notify_deploy_finished do # send message to the flow flowdock_api.send_message(:format => "html", :subject => "#{flowdock_project_name} deployed with branch #{branch} on ##{rails_env}", :content => notification_message, :tags => ["deploy", "#{rails_env}"] | flowdock_deploy_tags) end def notification_message if branch == current_branch message = "
The following changes were just deployed to #{rails_env}:
" commits = repo.commits_between(previous_revision, current_revision).reverse unless commits.empty? commits.each do |c| short, long = c.message.split(/\n+/, 2) message << "\n' + long + '
' end message << "#{c.id_abbrev}
#{CGI.escapeHTML(c.author.name)} on #{c.authored_date.strftime("%b %d, %H:%M")}