Skip to content

Commit

Permalink
feat(hipchat/email): allow custom messages for hipchat/email notifica…
Browse files Browse the repository at this point in the history
…tions (spinnaker#192)
  • Loading branch information
anotherchrisberry authored Oct 27, 2017
1 parent ce92da8 commit c1f7d0f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class EmailNotificationAgent extends AbstractEventNotificationAgent {

@Override
void sendNotifications(Map preference, String application, Event event, Map config, String status) {
Map context = event.content?.context ?: [:]
String buildInfo = ''

if (config.type == 'pipeline' || config.type == 'stage') {
Expand All @@ -55,12 +56,10 @@ class EmailNotificationAgent extends AbstractEventNotificationAgent {
}
}

String subject = '[Spinnaker] '

def customMessage = preference.message?."$status"
String subject

if (config.type == 'stage') {
subject = """Stage ${event.content.context.stageDetails.name} for ${
subject = """Stage ${context.stageDetails.name} for ${
application
}'s ${event.content?.execution?.name} pipeline ${buildInfo}"""
} else if (config.type == 'pipeline') {
Expand All @@ -75,6 +74,8 @@ class EmailNotificationAgent extends AbstractEventNotificationAgent {
status == 'complete' ? 'completed successfully' : status
}"""

subject = context.customSubject ?: subject

log.info('Sending email {} for {} {} {} {}', kv('address', preference.address), kv('application', application), kv('type', config.type), kv('status', status), kv('executionId', event.content?.execution?.id))

sendMessage(
Expand All @@ -85,7 +86,8 @@ class EmailNotificationAgent extends AbstractEventNotificationAgent {
config.type,
status,
config.link,
preference.message?."$config.type.$status"?.text
preference.message?."$config.type.$status"?.text,
context.customBody
)
}

Expand All @@ -94,24 +96,29 @@ class EmailNotificationAgent extends AbstractEventNotificationAgent {
'email'
}

private void sendMessage(String[] email, String[] cc, Event event, String title, String type, String status, String link, String customMessage) {
Template template = configuration.getTemplate(type == 'stage' ? 'stage.ftl' : 'pipeline.ftl', "UTF-8")
def body = FreeMarkerTemplateUtils.processTemplateIntoString(
template,
[
event : prettyPrint(toJson(event.content)),
url : spinnakerUrl,
htmlToText : new HtmlToPlainTextFormatter(),
markdownToHtml : new MarkdownToHtmlFormatter(),
application : event.details?.application,
executionId : event.content?.execution?.id,
type : type,
status : status,
link : link,
name : event.content?.execution?.name ?: event.content?.execution?.description,
message : customMessage
]
)
private void sendMessage(String[] email, String[] cc, Event event, String title, String type, String status, String link, String customMessage, String customBody) {
String body
if (customBody) {
body = new MarkdownToHtmlFormatter().convert(customBody)
} else {
Template template = configuration.getTemplate(type == 'stage' ? 'stage.ftl' : 'pipeline.ftl', "UTF-8")
body = FreeMarkerTemplateUtils.processTemplateIntoString(
template,
[
event : prettyPrint(toJson(event.content)),
url : spinnakerUrl,
htmlToText : new HtmlToPlainTextFormatter(),
markdownToHtml: new MarkdownToHtmlFormatter(),
application : event.details?.application,
executionId : event.content?.execution?.id,
type : type,
status : status,
link : link,
name : event.content?.execution?.name ?: event.content?.execution?.description,
message : customMessage
]
)
}

mailService.send(email, cc, title, body)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,22 @@ class HipchatNotificationAgent extends AbstractEventNotificationAgent {
message = """Stage ${event.content?.context?.stageDetails.name} for """
}

String link = "${spinnakerUrl}/#/applications/${application}/${config.type == 'stage' ? 'executions/details' : config.link }/${event.content?.execution?.id}"

message +=
"""${WordUtils.capitalize(application)}'s <a href="${
spinnakerUrl
}/#/applications/${application}/${
config.type == 'stage' ? 'executions/details' : config.link
}/${event.content?.execution?.id}">${
"""${WordUtils.capitalize(application)}'s <a href="${link}">${
event.content?.execution?.name ?: event.content?.execution?.description
}</a> ${buildInfo} ${config.type == 'task' ? 'task' : 'pipeline'} ${status == 'starting' ? 'is' : 'has'} ${
status == 'complete' ? 'completed successfully' : status
}"""

String customMessage = event.content?.context?.customMessage
if (customMessage && event.content?.execution?.id) {
message = customMessage
.replace("{{executionId}}", (String) event.content.execution.id)
.replace("{{link}}", link)
}

hipchatService.sendMessage(
token,
preference.address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,42 @@ class EmailNotificationAgentSpec extends Specification {
}
}

def "favors custom subject and body"() {
given:
def email = new BlockingVariables()
mailService.send(*_) >> { to, cc, subject, text ->
email.to = to
email.cc = cc
email.subject = subject
email.text = text
}

when:
agent.sendNotifications(
[address: address],
application,
event,
[type: "stage"],
status
)

then:
email.subject == customSubject
email.text == "<p>A <strong>custom</strong> body</p>\n"

and:
0 * _

where:
customSubject = "A custom subject"
customBody = "A **custom** body"
application = "whatever"
address = "[email protected]"
status = "complete"
pipelineName = "foo-pipeline"
stageName = "foo-stage"
event = new Event(content: [context: [customSubject: customSubject,
customBody: customBody,
stageDetails: [name: "foo-stage"]], execution: [name: "foo-pipeline"]])
}
}

0 comments on commit c1f7d0f

Please sign in to comment.