Skip to content

Commit b66fc3e

Browse files
committed
send emails on public events
1 parent 81e7d69 commit b66fc3e

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

services/email.rb

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ class Service::Email < Service
1515

1616
def receive_push
1717
extend PushEmail
18+
deliver_to_addresses
19+
end
20+
21+
def receive_public
22+
extend PublicEmail
23+
deliver_to_addresses
24+
end
25+
26+
def deliver_to_addresses
1827
configure_mail_defaults unless mail_configured?
1928

2029
addresses.each do |address|
@@ -139,7 +148,27 @@ def noreply_address
139148
@noreply_address ||= email_config['noreply_address'] || "GitHub <noreply@github.com>"
140149
end
141150

151+
module RepositoryEmailHelpers
152+
def name_with_owner
153+
File.join(owner_name, repository_name)
154+
end
155+
156+
def owner_name
157+
payload['repository']['owner']['name']
158+
end
159+
160+
def repository_name
161+
payload['repository']['name']
162+
end
163+
164+
def repo_url
165+
payload['repository']['url']
166+
end
167+
end
168+
142169
module PushEmail
170+
include RepositoryEmailHelpers
171+
143172
# Public
144173
def mail_subject
145174
if first_commit
@@ -250,10 +279,6 @@ def branch_ref
250279
payload['ref']
251280
end
252281

253-
def repo_url
254-
payload['repository']['url']
255-
end
256-
257282
def author_address
258283
"#{author_name} <#{author_email}>"
259284
end
@@ -275,18 +300,6 @@ def last_commit
275300
payload['commits'].last # assume that the last committer is also the pusher
276301
end
277302

278-
def name_with_owner
279-
File.join(owner_name, repository_name)
280-
end
281-
282-
def owner_name
283-
payload['repository']['owner']['name']
284-
end
285-
286-
def repository_name
287-
payload['repository']['name']
288-
end
289-
290303
def first_commit_sha
291304
first_commit['id']
292305
end
@@ -301,4 +314,23 @@ def first_commit
301314
payload['commits'].first
302315
end
303316
end
317+
318+
module PublicEmail
319+
include RepositoryEmailHelpers
320+
321+
# Public
322+
def mail_subject
323+
"#{name_with_owner} has changed from Private to Public"
324+
end
325+
326+
# Public
327+
def mail_body
328+
"#{name_with_owner} has changed from Private to Public\n\n#{repo_url}"
329+
end
330+
331+
# Public
332+
def mail_from
333+
noreply_address
334+
end
335+
end
304336
end

test/email_test.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ def test_push
1919
assert_nil svc.messages.shift
2020
end
2121

22+
def test_public
23+
svc = service({'address' => 'a'}, payload)
24+
25+
svc.receive_public
26+
27+
msg, from, to, subject = svc.messages.shift
28+
assert_match "noreply@github.com", from
29+
assert_equal 'a', to
30+
assert_match "mojombo/grit has changed from Private to Public", subject
31+
assert_match subject, msg
32+
assert_match "github.com/mojombo/grit", msg
33+
34+
assert_nil svc.messages.shift
35+
end
36+
2237
def test_multiple_address
2338
svc = service(
2439
{'address' => ' a b c'},
@@ -78,7 +93,7 @@ def svc.messages
7893
end
7994

8095
def svc.send_mail(mail)
81-
messages << [mail.to_s, mail.from.first, mail.to.first]
96+
messages << [mail.to_s, mail.from.first, mail.to.first, mail.subject]
8297
end
8398
svc
8499
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy