From 51e4b3e766b36d62ca31670a400b628a21befa96 Mon Sep 17 00:00:00 2001 From: kyewon Date: Fri, 14 Jul 2017 14:15:18 +0900 Subject: [PATCH] =?UTF-8?q?[APIWAY-204]=20AWS=20SES=EB=A5=BC=20=EC=9D=B4?= =?UTF-8?q?=EC=9A=A9=ED=95=9C=20=EC=9D=B4=EB=A9=94=EC=9D=BC=20=EC=A0=84?= =?UTF-8?q?=EC=86=A1=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apiway-notification.iml | 1 + bin/mqtt | 2 +- config.json | 7 - docker_build.sh | 3 +- lib/AwNotification.js | 13 +- lib/Template.js | 18 ++ lib/ses/emailTemplate.js | 361 +++++++++++++++++++++++++++++++++++++++ lib/ses/format.js | 13 ++ lib/ses/index.js | 149 ++++++++++++++++ package.json | 1 + 10 files changed, 555 insertions(+), 13 deletions(-) delete mode 100644 config.json create mode 100644 lib/Template.js create mode 100644 lib/ses/emailTemplate.js create mode 100644 lib/ses/format.js create mode 100644 lib/ses/index.js diff --git a/apiway-notification.iml b/apiway-notification.iml index 2d83db1..83a2782 100644 --- a/apiway-notification.iml +++ b/apiway-notification.iml @@ -5,5 +5,6 @@ + \ No newline at end of file diff --git a/bin/mqtt b/bin/mqtt index 0364146..0aa49bc 100755 --- a/bin/mqtt +++ b/bin/mqtt @@ -4,7 +4,7 @@ * Module dependencies. */ -var config = require('../config') +var config = require('../secret/config') var bunyan = require('bunyan') var mqtt = require('mqtt') var AwNotification = require('../lib/AwNotification') diff --git a/config.json b/config.json deleted file mode 100644 index dff146e..0000000 --- a/config.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "mqtt": { - "protocol": "mqtt", - "port": "1883", - "host": "apiway-pubsub" - } -} \ No newline at end of file diff --git a/docker_build.sh b/docker_build.sh index 0330166..59ca91e 100755 --- a/docker_build.sh +++ b/docker_build.sh @@ -4,8 +4,7 @@ REPOSITORY="apiway-notification" TAG=$1 AWS_CONTAINER_REGISTRY="539277938309.dkr.ecr.us-west-2.amazonaws.com" - -aws ecr get-login --region us-west-2 > ecr_login.sh +aws ecr get-login --region us-west-2 --no-include-email > ecr_login.sh chmod +x ecr_login.sh ./ecr_login.sh diff --git a/lib/AwNotification.js b/lib/AwNotification.js index 18f77be..1b327c6 100644 --- a/lib/AwNotification.js +++ b/lib/AwNotification.js @@ -4,8 +4,9 @@ * @license * */ -var bunyan = require('bunyan') -var Email = require('./email') +var bunyan = require('bunyan'); +var Email = require('./email'); +var SESEmail = require('./ses'); let log = bunyan.createLogger({name:'email'}) /* eslint valid-jsdoc: ["error", {"requireReturnDescription": false}] */ @@ -22,7 +23,8 @@ class AwNotification { let service = topic.split("/")[1] if (service == 'smtp') { - this.sendEmail(message) + this.sendSESEmail(message); + //this.sendEmail(message); } else if (service == 'webhook') { } else if (service == 'push') { } @@ -36,6 +38,11 @@ class AwNotification { var email = new Email() email.sendEmail(data) } + + sendSESEmail (data) { + var sesEmail = new SESEmail(); + sesEmail.sendSESEmail(data); + } } module.exports = AwNotification; diff --git a/lib/Template.js b/lib/Template.js new file mode 100644 index 0000000..873b047 --- /dev/null +++ b/lib/Template.js @@ -0,0 +1,18 @@ +'use strict'; + +var configFiles = require('./../secret/config.json'); +var CONFIG = {}; + +CONFIG = { + "SESKEY" : configFiles.aws.ses.key, + "SESSELECT" : configFiles.aws.ses.select, + "REGION": configFiles.aws.ses.region, + "IMGBASEPATH": configFiles.emailImg.basePath, + "IMGMAINLOGO": configFiles.emailImg.mainLogo, + "IMGFAILSTATUSLOGO": configFiles.emailImg.failStatusLogo, + "IMGFAILLOGO": configFiles.emailImg.failLogo, + "IMGSUCCESSSTATUSLOGO": configFiles.emailImg.successStatusLogo, + "IMGSUCCESSLOGO": configFiles.emailImg.successLogo +}; + +module.exports.CONFIG = CONFIG; \ No newline at end of file diff --git a/lib/ses/emailTemplate.js b/lib/ses/emailTemplate.js new file mode 100644 index 0000000..9d243e9 --- /dev/null +++ b/lib/ses/emailTemplate.js @@ -0,0 +1,361 @@ +exports.success = function () { + return ` + + + + + {{projectName}} + + + + +

+ + [ApiWay][{{projectResult}}] {{projectName}} +

+ + + + +
+ + + + + + + + + + + + + +
+

+ +

+
+ + + + + + + + +
+ {{projectResult}} +
+ {{projectName}} +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Suites: + {{suite}}
Tests: + {{tests}}
Passes: + {{passes}}
Pending: + {{pending}}
Failures: + {{failures}}
Start: + {{start}}
End: + {{end}}
Duration: + {{duration}} +
PassPercent: + + {{testPercent}} +
PendingPercent: + {{pendingPercent}}
Skipped: + {{skipped}}
PassPercentClass: + {{passPercentClass}} +
PendingPercentClass: + {{pendingPercentClass}} +
+ Show Full Test Report +
+
+ + + + + + + + +
+ apiway.io + + Set Change the Notification +
This + email is lorem ipsum dollor sit amet every time again.
This email is lorem ipsum + dollor sit amet every time again and yolo! +
+
+
+ +`; +}; + +exports.failures = function () { + return ` + + + + + {{projectName}} + + + + +

+ + [ApiWay][{{projectResult}}] {{projectName}} +

+ + + + +
+ + + + + + + + + + + + + +
+

+ +

+
+ + + + + + + + +
+ {{projectResult}} +
+ {{projectName}} +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Suites: + {{suite}}
Tests: + {{tests}}
Passes: + {{passes}}
Pending: + {{pending}}
Failures: + {{failures}}
Start: + {{start}}
End: + {{end}}
Duration: + {{duration}} +
PassPercent: + + {{testPercent}} +
PendingPercent: + {{pendingPercent}}
Skipped: + {{skipped}}
PassPercentClass: + {{passPercentClass}} +
PendingPercentClass: + {{pendingPercentClass}} +
+ Show Full Test Report +
+
+ + + + + + + + +
+ apiway.io + + Set Change the Notification +
This + email is lorem ipsum dollor sit amet every time again.
This email is lorem ipsum + dollor sit amet every time again and yolo! +
+
+
+ +`; +}; \ No newline at end of file diff --git a/lib/ses/format.js b/lib/ses/format.js new file mode 100644 index 0000000..3a46038 --- /dev/null +++ b/lib/ses/format.js @@ -0,0 +1,13 @@ +'use strict'; + +let sesReport = { + from: 'no-reply@apiway.io', + to: '', + //cc: '', + //bcc: [''], + subject: '[ApiWay] Test Report - ', + message: 'html' +}; + + +module.exports.sesReport = sesReport; \ No newline at end of file diff --git a/lib/ses/index.js b/lib/ses/index.js new file mode 100644 index 0000000..0772cb9 --- /dev/null +++ b/lib/ses/index.js @@ -0,0 +1,149 @@ +/** + * @file + * @copyright + * @license + * + */ + +var template = require('../Template'); +var bunyan = require('bunyan'); +var request = require('request'); +var Promise = require('promise'); +var AwNotification = require('../AwNotification'); +var emailFormat = require('./format'); +let log = bunyan.createLogger({name:'email'}); +var Handlebars = require('handlebars'); +var emailTemplate = require('./emailTemplate'); +var emailData = {}; +var imgBasePath = template.CONFIG.IMGBASEPATH; +var imgMainLogo = template.CONFIG.IMGMAINLOGO; +var imgFailStatusLogo = template.CONFIG.IMGFAILSTATUSLOGO; +var imgFailLogo = template.CONFIG.IMGFAILSTATUSLOGO; +var imgSuccessStatusLogo = template.CONFIG.IMGSUCCESSSTATUSLOGO; +var imgSuccessLogo = template.CONFIG.IMGSUCCESSLOGO; +var ses = require('node-ses'); +var sesClient = ses.createClient({ key: template.CONFIG.SESKEY, secret: template.CONFIG.SESSELECT , amazon: template.CONFIG.REGION}); + + +class SESEmail { + constructor() {} + + sendSESEmail (data) { + log.info(data) + let dataObj = JSON.parse(data); + let instance; + let subscriber; + + if (dataObj) { + instance = dataObj.instance; + subscriber = dataObj.subscriber; + } else { + throw `Data error: ${data}` + } + + if (!subscriber || subscriber.length == 0) { + throw 'Recipient is null' + } + + emailFormat.sesReport.to = subscriber.join(','); + + this.getReport(instance.reportJson) + .then((json) => { + let report = JSON.parse(json); + + emailData = { + projectName: instance.project.name, + projectResult: '', + reportHtml: instance.reportHtml, + suites: report.stats.suites, + tests: report.stats.tests, + passes: report.stats.passes, + pending: report.stats.pending, + failures: report.stats.failures, + start: report.stats.start, + end: report.stats.end, + duration: report.stats.duration, + passPercent: report.stats.passPercent, + pendingPercent: report.stats.pendingPercent, + skipped: report.stats.skipped, + passPercentClass: report.stats.passPercentClass, + pendingPercentClass: report.stats.pendingPercentClass, + imgMainLogo: imgBasePath+imgMainLogo, + imgFailLogo: imgBasePath+imgFailLogo, + imgFailStatusLogo: imgBasePath+imgFailStatusLogo, + imgSuccessStatusLogo: imgBasePath+imgSuccessStatusLogo, + imgSuccessLogo: imgBasePath+imgSuccessLogo + }; + + emailFormat.sesReport.message = makeHtmlEmailFormat(report.stats.failures, emailData); + + sesClient.sendEmail(emailFormat.sesReport, (error, info) => { + if (error) { + log.info('Error occurred'); + log.info(error.message); + return; + } + log.info('Message sent successfully!'); + log.info('Server responded with "%s"', info.response); + }); + }) + } + + getReport (url) { + return new Promise((resolve, reject) => { + console.log(`getReport: ${url}`); + // Set the headers + var headers = { + 'Content-Type': 'application/json' + }; + + // Configure the request + var options = { + url: url, + method: 'GET', + headers: headers + }; + + // Start the request + request(options, function (error, response, body) { + //console.log(response) + //console.log(body) + if (!error && response.statusCode == 200) { + // Print out the response body + //console.log('200: '); + console.log(body) + resolve(body) + } else { + console.log(error) + reject(error) + } + }) + }) + } +} + +function makeHtmlEmailFormat(failures, emailData) { + + var statusResult; + var emailHtml; + var template; + + if (failures > 0) { + statusResult = 'FAIL'; + emailData.projectResult = statusResult; + + emailHtml = emailTemplate.failures(); + template = Handlebars.compile(emailHtml); + + } else { + statusResult = 'PASS'; + emailData.projectResult = statusResult; + + emailHtml = emailTemplate.success(); + template = Handlebars.compile(emailHtml); + } + + return template(emailData); +} + +module.exports = SESEmail; diff --git a/package.json b/package.json index ecddcbe..ff64583 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "bunyan": "^1.8.10", "handlebars": "^4.0.10", "mqtt": "^2.8.0", + "node-ses": "^2.0.5", "nodemailer": "^4.0.1", "promise": "^7.1.1", "request": "^2.81.0"