Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apiway-notification.iml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="apiway-notification node_modules" level="project" />
<orderEntry type="library" name="apiway-notification node_modules" level="project" />
</component>
</module>
2 changes: 1 addition & 1 deletion bin/mqtt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies.
*/

var config = require('../config')
var config = require('../secret/config')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

secret 폴더를 궂이 만드실 필요는 없을것 같아요...(다른 public한 정보들도 다 같이 들어가니까요..)

그냥 ../config를 유지 하면 좋겠습니다.

var bunyan = require('bunyan')
var mqtt = require('mqtt')
var AwNotification = require('../lib/AwNotification')
Expand Down
7 changes: 0 additions & 7 deletions config.json

This file was deleted.

3 changes: 1 addition & 2 deletions docker_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 10 additions & 3 deletions lib/AwNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}] */
Expand All @@ -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') {
}
Expand All @@ -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;
18 changes: 18 additions & 0 deletions lib/Template.js
Original file line number Diff line number Diff line change
@@ -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;
361 changes: 361 additions & 0 deletions lib/ses/emailTemplate.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions lib/ses/format.js
Original file line number Diff line number Diff line change
@@ -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;
149 changes: 149 additions & 0 deletions lib/ses/index.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down