forked from github/github-services
/
email.rb
68 lines (51 loc) · 1.78 KB
/
email.rb
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
service :email do |data, payload|
name_with_owner = File.join(payload['repository']['owner']['name'], payload['repository']['name'])
# Should be: first_commit = payload['commits'].first
first_commit = payload['commits'].first
first_commit_sha = first_commit['id']
# Shorten the elements of the subject
first_commit_sha = first_commit_sha[0..5]
first_commit_title = first_commit['message'][/^([^\n]+)/, 1]
if first_commit_title.length > 50
first_commit_title = first_commit_title.slice(0,50) << '...'
end
body = <<-EOH
Branch: #{payload['ref']}
Home: #{payload['repository']['url']}
EOH
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] }
changed_paths = (added + removed + modified).sort_by { |(char, file)| file }
changed_paths = changed_paths.collect { |entry| entry * ' ' }.join("\n ")
timestamp = Date.parse(commit['timestamp'])
body << <<-EOH
Commit: #{gitsha}
#{commit['url']}
Author: #{commit['author']['name']} <#{commit['author']['email']}>
Date: #{timestamp} (#{timestamp.strftime('%a, %d %b %Y')})
EOH
if changed_paths.size > 0
body << <<-EOH
Changed paths:
#{changed_paths}
EOH
end
body << <<-EOH
Log Message:
-----------
#{commit['message']}
EOH
end
message = TMail::Mail.new
message.set_content_type('text', 'plain')
message.to = data['address']
message.subject = "[#{name_with_owner}] #{first_commit_sha}: #{first_commit_title}"
message.body = body
message.date = Time.now
Net::SMTP.start('smtp', 25, 'github.com') do |smtp|
smtp.send_message message.to_s, 'noreply@github.com', data['address']
end
end