Created
August 10, 2012 04:54
-
-
Save tkfm-yamaguchi/3311157 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# coding: utf-8 | |
# Please set the authentication | |
# information here to send the mail. | |
# | |
# * address : mail sending server name. | |
# * prot : mail sending server's port number. | |
# * user_name : auth user name to send mail. | |
# * password : auth passwd of user_name. | |
# | |
MailConfig =<<EOC | |
address: "****" | |
port: 587 | |
user_name: "****" | |
password: "****" | |
send_to: | |
- "****@****" | |
EOC | |
# == Summary: | |
# | |
# Send mail which contain new commit information. | |
# | |
# == Requirements: | |
# | |
# * Ruby (> 1.8.7) | |
# * MTA deamon (sendmail, postfix ...etc.) | |
# * mail [RubyGem] | |
# | |
# To install mail gem, run the command: | |
# | |
# $ gem install mail | |
# | |
# You can confirm whether mail was install like this: | |
# | |
# $ gem list | grep mail | |
# mail (X.X.X) | |
# | |
# == Description: | |
# | |
# This script aims to notice push event to common repository | |
# with sending email. | |
# To run this script if repository is pushed, register this | |
# script in "post-update" hook file: | |
# | |
# (at .git/hooks/post-update) | |
# # exec git-update-server-info | |
# SCRIPT_PATH=/path/to/git-repo/hooks/post-update-email.rb | |
# ${SCRIPT_PATH} | |
# | |
# ! Do not forget to add the executable(+x) parmission for post-update | |
# and this file. | |
# | |
# $ chmod +x post-update | |
# $ chmod +x post-update-email.rb | |
# | |
# | |
# == Mail Configuration Test: | |
# | |
# To confirm You can send mail with this script, runs below: | |
# | |
# $ ruby post-update-mail.rb test | |
# | |
# If it is ready to send mail, you would recieve mail like: | |
# from [email protected] | |
# to (address written at configuration) | |
# subject mail sending test was succeeded! | |
# body This mail was send from <server name> to confirm the mail configuration. | |
# | |
# | |
# == TODO: | |
# | |
# * Add repository name to mail. | |
# * More information about commit ... ? | |
# | |
require "rubygems" if RUBY_VERSION < '1.9' | |
require "mail" | |
require "yaml" | |
module GIT_LOG | |
class << self | |
def commiter | |
`git log -1 --pretty=format:"%ae"` | |
end | |
def comment | |
`git log -1 --pretty=format:"%s"` | |
end | |
def description | |
<<-EOD | |
The change set was pushed on: | |
#{_repository_name_} @ #{_server_name_} | |
=============NEWEST COMMIT LOG============= | |
#{`git whatchanged -1 --pretty=format:"commiter: %cn <%ae>%ndate : %cd%n%s%n--------------------%b"`.chomp} | |
=========================================== | |
EOD | |
end | |
def _server_name_ | |
`uname -n`.chomp | |
end | |
def _repository_name_ | |
`pwd`.chomp | |
end | |
end | |
end | |
# Bugger? | |
def switch_to_test! | |
GIT_LOG.class_eval do | |
class << self | |
def commiter; "[email protected]"; end | |
def comment; "mail sending test was succeeded!"; end | |
def description; "This mail was send from '#{_serve_name_}' to confirm the mail configuration."; end | |
end | |
end | |
end | |
if __FILE__ == $0 | |
# Set up for test sending | |
switch_to_test! if ARGV[0] && ARGV[0] == "test" | |
mail_conf = YAML.load(MailConfig) | |
send_to = mail_conf.delete("send_to") | |
mail_conf.merge!("enable_starttls_auto" => false) | |
# Conf! | |
Mail.defaults do | |
delivery_method :smtp, mail_conf | |
end | |
# Mail! | |
Mail.deliver do | |
from GIT_LOG.commiter | |
to send_to | |
subject GIT_LOG.comment | |
body GIT_LOG.description | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment