Class: EmailNotifyHook

Inherits:
RubyGitHooks::Hook show all
Defined in:
lib/ruby_git_hooks/email_notify.rb

Overview

This hook sends out email notification of commits to a configurable list of recipients via the Pony gem, which uses the Ruby Mail gem.

Constant Summary collapse

[ "no_send", "via", "via_options", "max_lines", "recipients" ]

Constants inherited from RubyGitHooks::Hook

RubyGitHooks::Hook::HOOK_INFO, RubyGitHooks::Hook::HOOK_TYPE_SETUP

Instance Method Summary collapse

Methods inherited from RubyGitHooks::Hook

get_hooks_to_run, initial_setup, register, run, run_as_specific_githook, #setup, shell!

Constructor Details

#initialize(options = {}) ⇒ EmailNotifyHook

Returns a new instance of EmailNotifyHook.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby_git_hooks/email_notify.rb', line 11

def initialize(options = {})
  bad_opts = options.keys - LEGAL_OPTIONS
  unless bad_opts.empty?
    STDERR.puts "Called EmailNotifyHook with bad options: #{bad_opts.inspect}!"
    exit 1
  end

  @options = options
  @options["max_lines"] ||= 300

  unless @options["recipients"]
    raise "Must specify at least one recipient to EmailNotifyHook!"
  end
end

Instance Method Details

#checkObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruby_git_hooks/email_notify.rb', line 26

def check
  content = file_diffs.flat_map { |path, diff| ["", path, diff] }.join("\n")
  if content.split("\n").size > options["max_lines"]
    content = "Diffs are too big.  Skipping them.\nFiles:\n" +
      file_diffs.keys.join("\n")
  end

  recipients = @options.recipients.split /,|;/

  unless @options["no_send"] || @options["via"] == "no_send"
    require "pony"
    
    recipients.each do |name, email|
      ret = Pony.mail :to => email,
                :from => @options["from"],
                :subject => @options["subject"],
                :body => desc,
                :via => @options["via"],
                :via_options => @options["via_options"]
    end
  end

  true
end