Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nodeta/321929 to your computer and use it in GitHub Desktop.
Save nodeta/321929 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# This git post-receive hook generates a nice-looking email about
# the recent commits. It can be used to send e-mail to the team's
# Flowdock account, for example.
#
# Just copy it to your git repository's hooks/post-receive and make
# sure you have ruby, rubygems and gem 'grit' installed.
require 'stringio'
require 'rubygems'
require 'grit'
require 'md5'
require 'cgi'
# These two configuration variables need to be set.
DESCRIPTION = "MyApp Frontend"
RECIPIENTS = "myflow@mycompany.flowdock.com"
def generate_email_header
puts <<-END_OF_HEADERS
To: #{$recipients}
Subject: #{$subject_prefix}#{$subject}#{$subject_suffix}
Content-Type: text/html; charset="utf-8"
X-Git-Refname: #{$refname}
X-Git-Oldrev: #{$oldrev}
X-Git-Newrev: #{$newrev}
END_OF_HEADERS
end
def generate_email_body
commits_to_html
end
def commits_to_html
$commits.reverse.each do |c|
short, long = c.message.split(/\n+/, 2)
puts "<div style=\"margin-bottom: 10px\"><div class=\"ui-corner-all\" style=\"background:url(http://gravatar.com/avatar/#{MD5::md5(c.author.email)}?s=30)
no-repeat scroll center;height:30px;width:30px;float:left;margin-right:5px;\">&nbsp;</div>"
puts "<div style=\"padding-left: 35px;\">#{CGI.escapeHTML(short)}<br/>"
if long
long.gsub!(/\n/, '<br />')
puts '<p style="margin:5px 0px; padding: 0 5px; border-left: 3px solid #ccc">' + long + '</p>'
end
puts "<span style=\"font-size: 90%; color: #333\"><code>#{c.id_abbrev}</code> <a href=\"mailto:#{CGI.escapeHTML(c.author.email)}\">#{CGI.escapeHTML(c.aut
hor.name)}</a> on #{c.date.strftime("%b %d, %H:%M")}</span></div></div>"
end
end
repo = Grit::Repo.new(".")
if ARGV.empty?
strio = StringIO.new
$stdout = strio
$oldrev, $newrev, $refname = gets.split()
else
$oldrev, $newrev, $refname = ARGV
end
# Don't do anything if it's not a branch update(?)
exit unless $refname =~ %r{refs/heads/}
branch = $refname.scan(%r{[^/]+$}).to_s
$description = DESCRIPTION
$recipients = RECIPIENTS
$subject = "#{DESCRIPTION}: branch #{branch} updated"
$subject_suffix = " ##{branch} #push"
begin
$commits = repo.commits_between($oldrev, $newrev)
generate_email_header
generate_email_body
if strio
strio.rewind()
email = strio.read()
IO.popen("/usr/sbin/sendmail -t", "w+") do |pipe|
pipe.write email
pipe.close_write
end
end
rescue => e
puts "post-receive script failed: #{e.inspect}"
puts
puts "oldrev: #{$oldrev}"
puts "newrev: #{$newrev}"
puts "refname: #{$refname}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment