public
Description: Send Github Commit Emails
Homepage:
Clone URL: git://github.com/adamhjk/github-commit-email.git
github-commit-email / github-commit-email.rb
100644 100 lines (85 sloc) 2.823 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Author:: Adam Jacob (<adam@hjksolutions.com>)
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
# License:: GNU General Public License version 2 or later
#
# This program and entire repository is free software; you can
# redistribute it and/or modify it under the terms of the GNU
# General Public License as published by the Free Software
# Foundation; either version 2 of the License, or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
 
require 'rubygems'
require 'json'
require 'open-uri'
 
Merb::Config.use { |c|
  c[:project] = "Example",
  c[:mailto] = "<noreply@example.com>",
  c[:mailfrom] = "<noreply@example.com>",
  c[:framework] = {},
  c[:session_store] = 'none',
  c[:exception_details] = true
}
 
require "merb-mailer"
 
Merb::Mailer.config = {
   :host => 'localhost',
   :port => '25',
   :domain => "commitbot" # the HELO domain provided by the client to the server
}
 
Merb::Router.prepare do |r|
  r.resources :commit
  r.match('/').to(:controller => 'commit', :action =>'index')
end
 
class Commit < Merb::Controller
  
  def index
    results = "I accept github post-commits for #{Merb::Config[:project]}"
    results << " and relay them to #{Merb::Config[:mailto]}. POST to /commit."
    results
  end
  
  def create
    ch = JSON.parse(params[:payload])
    ch['commits'].each do |gitsha, commit|
      subject = commit['message'].split("\n")[0]
      body = <<-EOH
Repository Name: #{ch['repository']['name']}
Owner: #{ch['repository']['owner']['name']} (#{ch['repository']['owner']['email']})
URL: #{ch['repository']['url']}
Ref: #{ch['ref']}
 
EOH
      body << <<-EOH
#{gitsha}
Author: #{commit['author']['name']} (#{commit['author']['email']})
URL: #{commit['url']}
Timestamp: #{commit['timestamp']}
Commit Message:
 
#{commit['message']}
 
Diff:
 
EOH
      begin
        open(commit['url'] + ".diff") do |f|
          f.each do |line|
            body << line
          end
        end
      rescue
        body << "Cannot fetch diff!\n\n#{$!}"
        Merb.logger.debug("Exception: #{$!}")
      end
      m = Merb::Mailer.new(
            :to => Merb::Config[:mailto],
            :from => "Commit in #{ch['repository']['owner']['name']} #{ch['repository']['name']} #{Merb::Config[:mailfrom]}",
            :subject => subject,
            :text => body
          )
      m.deliver!
    end
    "Commit Sent"
  end
end