Skip to content

Commit

Permalink
fix(email): fix email parser to not fail on multiple addresses (#545)
Browse files Browse the repository at this point in the history
If a pipeline specified two email addressed in the notification that email notifcation
will not be sent.
This change moves the email address splitting/parsing logic to the least common denominator -
the `send` function since it can be called either by handling an email event directly or
indirectly via pipeline/stage events

Also, adding ability to split on semicolons in email addresses
  • Loading branch information
marchello2000 authored May 10, 2019
1 parent 87d4c46 commit 1f23754
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,11 @@ class EmailNotificationService implements NotificationService {

@Override
EchoResponse.Void handle(Notification notification) {
def expando = { String addresses ->
// could be comma and/or space separated
addresses = addresses.replaceAll(",", " ")
return addresses.split(" ").findAll { !it.isEmpty() }
}

def to = notification.to?.collect {
expando(it)
}?.flatten() as String[]

def cc = notification.cc?.collect {
expando(it)
}?.flatten() as String[]

def subject = notificationTemplateEngine.build(notification, NotificationTemplateEngine.Type.SUBJECT)
def body = notificationTemplateEngine.build(notification, NotificationTemplateEngine.Type.BODY)

try {
send(to, cc, subject, body)
send(notification.to?.toArray(new String[0]), notification.cc?.toArray(new String[0]), subject, body)
} catch (AddressException e) {
throw new InvalidRequestException(e.message)
}
Expand All @@ -85,12 +71,25 @@ class EmailNotificationService implements NotificationService {
MimeMessage message = javaMailSender.createMimeMessage()
MimeMessageHelper helper = new MimeMessageHelper(message, false, "utf-8")

def splitAddresses = { String addresses ->
// Split on space, comma, or semicolon
return addresses.split("[ ,;]").findAll { !it.isEmpty() }
}

if (to) {
helper.setTo(to)
def toParsed = to.collect {
splitAddresses(it)
}?.flatten() as String[]

helper.setTo(toParsed)
}

if (cc) {
helper.setCc(cc)
def ccParsed = cc.collect {
splitAddresses(it)
}?.flatten() as String[]

helper.setCc(ccParsed)
}

if (to || cc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ class EmailNotificationServiceSpec extends Specification {
['receiver@localhost'] | ['some-addr@localhost'] || ['receiver@localhost'] || ['some-addr@localhost']
['receiver@localhost another@localhost'] | ['some-addr@localhost some-other-addr@localhost'] || ['receiver@localhost', 'another@localhost'] || ['some-addr@localhost', 'some-other-addr@localhost']
['receiver@localhost,another@localhost'] | ['some-addr@localhost,some-other-addr@localhost'] || ['receiver@localhost', 'another@localhost'] || ['some-addr@localhost', 'some-other-addr@localhost']
['receiver@localhost, another@localhost'] | ['some-addr@localhost, some-other-addr@localhost'] || ['receiver@localhost', 'another@localhost'] || ['some-addr@localhost', 'some-other-addr@localhost']
['receiver@localhost; another@localhost'] | ['some-addr@localhost, some-other-addr@localhost'] || ['receiver@localhost', 'another@localhost'] || ['some-addr@localhost', 'some-other-addr@localhost']
}
}

0 comments on commit 1f23754

Please sign in to comment.