Permalink
Browse files

Use Mail instead of TMail in Service::Email.

  • Loading branch information...
1 parent af241e0 commit 601f00d94732642200921844d13b5874152f0715 @benburkert benburkert committed Jan 5, 2012
Showing with 158 additions and 62 deletions.
  1. +158 −62 services/email.rb
View
@@ -3,87 +3,179 @@ class Service::Email < Service
boolean :send_from_author
def receive_push
- name_with_owner = File.join(payload['repository']['owner']['name'], payload['repository']['name'])
+ addresses.each do |address|
+ send_message_to address
+ end
+ end
+
+ def addresses
+ data['address'].split(' ').slice(0, 2)
+ end
- # Should be: first_commit = payload['commits'].first
- first_commit = payload['commits'].first
- return if first_commit.nil?
+ def send_message_to(address)
+ send_message mail_message(address), from_address, address
+ end
- first_commit_sha = first_commit['id']
+ def mail_message(address)
+ my = self
- # Shorten the elements of the subject
- first_commit_sha = first_commit_sha[0..5]
+ Mail.new do
+ to address
+ from my.from_address
+ reply_to my.from_address
+ subject my.mail_subject
+ headers my.secret_header
- first_commit_title = first_commit['message'][/^([^\n]+)/, 1] || ''
- if first_commit_title.length > 50
- first_commit_title = first_commit_title.slice(0,50) << '...'
+ text_part do
+ content_type 'text/plain; charset=UTF-8'
+ body my.text_body
+ end
end
+ end
- body = <<-EOH
- Branch: #{payload['ref']}
- Home: #{payload['repository']['url']}
- EOH
+ def text_body
+ body = commits.inject(repository_text) do |text, commit|
+ text << commit_text(commit)
+ end
- payload['commits'].each do |commit|
- gitsha = commit['id']
- added = commit['added'].map { |f| ['A', f] }
- removed = commit['removed'].map { |f| ['R', f] }
- modified = commit['modified'].map { |f| ['M', f] }
+ body << compare_text unless single_commit?
- changed_paths = (added + removed + modified).sort_by { |(char, file)| file }
- changed_paths = changed_paths.collect { |entry| entry * ' ' }.join("\n ")
+ body
+ end
- timestamp = Date.parse(commit['timestamp'])
+ def repository_text
+ align(<<-EOH)
+ Branch: #{branch_ref}
+ Home: #{repo_url}
+ EOH
+ end
- body << <<-EOH
- Commit: #{gitsha}
- #{commit['url']}
- Author: #{commit['author']['name']} <#{commit['author']['email']}>
- Date: #{timestamp} (#{timestamp.strftime('%a, %d %b %Y')})
+ def commit_text(commit)
+ gitsha = commit['id']
+ added = commit['added'].map { |f| ['A', f] }
+ removed = commit['removed'].map { |f| ['R', f] }
+ modified = commit['modified'].map { |f| ['M', f] }
- EOH
+ changed_paths = (added + removed + modified).sort_by { |(char, file)| file }
+ changed_paths = changed_paths.collect { |entry| entry * ' ' }.join("\n ")
- if changed_paths.size > 0
- body << <<-EOH
- Changed paths:
- #{changed_paths}
+ timestamp = Date.parse(commit['timestamp'])
- EOH
- end
+ commit_author = "#{commit['author']['name']} <#{commit['author']['email']}>"
- body << <<-EOH
- Log Message:
- -----------
- #{commit['message']}
+ text = align(<<-EOH)
+ Commit: #{gitsha}
+ #{commit['url']}
+ Author: #{commit_author}
+ Date: #{timestamp} (#{timestamp.strftime('%a, %d %b %Y')})
+ EOH
+ if changed_paths.size > 0
+ text << align(<<-EOH)
+ Changed paths:
+ #{changed_paths}
- EOH
+ EOH
end
- body << "Compare: #{payload['compare']}" if payload['commits'].size > 1
- commit = payload['commits'].last # assume that the last committer is also the pusher
-
- begin
- data['address'].split(' ').slice(0, 2).each do |address|
- message = TMail::Mail.new
- message.set_content_type('text', 'plain', {:charset => 'UTF-8'})
- message.from = "#{commit['author']['name']} <#{commit['author']['email']}>" if data['send_from_author']
- message.reply_to = "#{commit['author']['name']} <#{commit['author']['email']}>" if data['send_from_author']
- message.to = address
- message.subject = "[#{name_with_owner}] #{first_commit_sha}: #{first_commit_title}"
- message.body = body
- message.date = Time.now
-
- message['Approved'] = data['secret'] if data['secret'].to_s.size > 0
-
- if data['send_from_author']
- send_message message, "#{commit['author']['name']} <#{commit['author']['email']}>", address
- else
- send_message message, "GitHub <noreply@github.com>", address
- end
- end
- end
+ text << align(<<-EOH)
+ Log Message:
+ -----------
+ #{commit['message']}
+
+
+ EOH
+
+ text
+ end
+
+ def compare_text
+ "Compare: #{payload['compare']}"
+ end
+
+ def single_commit?
+ first_commit == last_commit
+ end
+
+ def branch_ref
+ payload['ref']
+ end
+
+ def repo_url
+ payload['repository']['url']
+ end
+
+ def mail_subject
+ "[#{name_with_owner}] #{first_commit_sha}: #{first_commit_title}"
+ end
+
+ def secret_header
+ secret ? {'Approved' => secret} : {}
+ end
+
+ def from_address
+ send_from_author? ? author_address : noreply_address
+ end
+
+ def send_from_author?
+ data['send_from_author']
+ end
+
+ def author_address
+ "#{author_name} <#{author_email}>"
+ end
+
+ def author_name
+ last_commit['author']['name']
+ end
+
+ def author_email
+ last_commit['author']['email']
+ end
+
+ def last_commit
+ payload['commits'].last # assume that the last committer is also the pusher
+ end
+
+ def secret
+ data['secret'] if data['secret'].to_s.size > 0
+ end
+
+ def name_with_owner
+ File.join(owner_name, repository_name)
+ end
+
+ def owner_name
+ payload['repository']['owner']['name']
+ end
+
+ def repository_name
+ payload['repository']['name']
+ end
+
+ def first_commit_sha
+ first_commit[:id]
+ end
+
+ def first_commit_title(limit = 50)
+ title_line = first_commit['message'][/\A[^\n]+/] || ''
+
+ title_line.length > limit ? shorten(title_line, limit) : title_line
+ end
+
+ def shorten(text, limit)
+ text.slice(0, limit) << '...'
+ end
+
+ def first_commit
+ payload['commits'].first
+ end
+
+ def align(text, indent = ' ')
+ margin = text[/\A\s+/].size
+
+ text.gsub(/^\s{#{margin}}/, indent)
end
def smtp_address
@@ -122,6 +214,10 @@ def smtp_logging?
@smtp_logging ||= email_config['enable_logging']
end
+ def noreply_address
+ @noreply_address ||= email_config['noreply_address'] || "GitHub <noreply@github.com>"
+ end
+
def smtp_settings
settings = [smtp_domain]

0 comments on commit 601f00d

Please sign in to comment.