require 'flowdock' 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 begin run "echo '#{source.head.chomp}' > #{current_path}/BRANCH" rescue => e puts "Flowdock: error in saving deployed branch information: #{e.to_s}" end end task :set_flowdock_api do set :flowdock_deploy_env, fetch(:stage, fetch(:rails_env, ENV["RAILS_ENV"] || "production")) begin require 'grit' set :repo, Grit::Repo.new(".") config = Grit::Config.new(repo) rescue LoadError => e puts "Flowdock: you need to have Grit gem installed: #{e.to_s}" rescue => e puts "Flowdock: error in fetching your git repository information: #{e.to_s}" end begin flows = Array(flowdock_api_token).map do |api_token| Flowdock::Flow.new(:api_token => api_token, :source => "Capistrano deployment", :project => flowdock_project_name, :from => {:name => config["user.name"], :address => config["user.email"]}) end set :flowdock_api, flows rescue => e puts "Flowdock: error in configuring Flowdock API: #{e.to_s}" end end task :notify_deploy_finished do # send message to the flow begin flowdock_api.each do |flow| flow.push_to_team_inbox(:format => "html", :subject => "#{flowdock_project_name} deployed with branch #{branch} on ##{flowdock_deploy_env}", :content => notification_message, :tags => ["deploy", "#{flowdock_deploy_env}"] | flowdock_deploy_tags) end rescue => e puts "Flowdock: error in sending notification to your flow: #{e.to_s}" end end def notification_message if branch == current_branch message = "
The following changes were just deployed to #{flowdock_deploy_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")}