forked from github/github-services
/
email.rb
273 lines (207 loc) · 5.47 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
class Net::SMTP
def mailfrom(from_addr)
from_addr = from_addr[/<([^>]+)>/, 1] if from_addr.include?('<')
if $SAFE > 0
raise SecurityError, 'tainted from_addr' if from_addr.tainted?
end
getok("MAIL FROM:<#{from_addr}>")
end
end
class Service::Email < Service
string :address, :secret
boolean :send_from_author
def receive_push
configure_mail_defaults unless mail_configured?
addresses.each do |address|
send_message_to address
end
end
def send_message_to(address)
send_mail mail_message(address)
end
def send_mail(mail)
mail.deliver!
end
def configure_mail_defaults
my = self
Mail.defaults do
delivery_method :smtp,
:address => my.smtp_address,
:port => my.smtp_port,
:domain => my.smtp_domain,
:user_name => my.smtp_user_name,
:password => my.smtp_password,
:authentication => my.smtp_authentication,
:enable_starttls_auto => my.smtp_enable_starttls_auto?,
:openssl_verify_mode => my.smtp_openssl_verify_mode
end
mail_configured!
end
def mail_configured?
defined?(@@mail_configured) && @@mail_configured
end
def mail_configured!
@@mail_configured = true
end
def addresses
data['address'].split(' ').slice(0, 2)
end
def mail_message(address)
my = self
Mail.new do
to address
from my.from_address
reply_to my.from_address
subject my.mail_subject
headers my.secret_header
text_part do
content_type 'text/plain; charset=UTF-8'
body my.text_body
end
end
end
def text_body
body = commits.inject(repository_text) do |text, commit|
text << commit_text(commit)
end
body << compare_text unless single_commit?
body
end
def repository_text
align(<<-EOH)
Branch: #{branch_ref}
Home: #{repo_url}
EOH
end
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] }
changed_paths = (added + removed + modified).sort_by { |(char, file)| file }
changed_paths = changed_paths.collect { |entry| entry * ' ' }.join("\n ")
timestamp = Date.parse(commit['timestamp'])
commit_author = "#{commit['author']['name']} <#{commit['author']['email']}>"
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
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
if first_commit
"[#{name_with_owner}] #{first_commit_sha.slice(0, 6)}: #{first_commit_title}"
else
"[#{name_with_owner}]"
end
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
commit = last_commit || {}
commit['author'] || commit['committer'] || payload['pusher']
end
def author_name
author['name']
end
def author_email
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
@smtp_address ||= email_config['address']
end
def smtp_port
@smtp_port ||= (email_config['port'] || 25).to_i
end
def smtp_domain
@smtp_domain ||= email_config['domain'] || 'localhost.localdomain'
end
def smtp_authentication
@smtp_authentication ||= email_config['authentication']
end
def smtp_user_name
@smtp_user_name ||= email_config['user_name']
end
def smtp_password
@smtp_password ||= email_config['password']
end
def smtp_enable_starttls_auto?
@smtp_enable_starttls_auto ||= (email_config['enable_starttls_auto'] && true)
end
def smtp_openssl_verify_mode
@smtp_openssl_verify_mode ||= email_config['openssl_verify_mode']
end
def smtp_logging?
@smtp_logging ||= email_config['enable_logging']
end
def noreply_address
@noreply_address ||= email_config['noreply_address'] || "GitHub <noreply@github.com>"
end
end