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"