Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

single email per push #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
108 changes: 74 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var config = require('./config.json');
var parse = require('diff-parse');
var nodemailer = require('nodemailer');
var escape = require('escape-html');
var async = require('async');

exports.handler = (event, context, callback) => {

Expand All @@ -15,79 +16,93 @@ exports.handler = (event, context, callback) => {
} else {

var commits = event['body-json'].commits;
var email = '<html><body>';
email += 'Branch: ' + event['body-json'].ref + '<br/>';
email += 'Home: <a href="' + event['body-json'].repository.html_url + '">' + event['body-json'].repository.html_url + '</a><br/>';
email += 'Compare: <a href="' + event['body-json'].compare + '">' + event['body-json'].compare + '</a><br/><hr>';

commits.forEach(function(e,i,a){
async.forEach(commits, function(e, callback){
getDiffFromGitHub(e.url)
.then((diff)=>{
var files = parse(diff);
var email = '<html><body>';

email += 'Branch: ' + event['body-json'].ref + '<br/>';
email += 'Home: <a href="' + event['body-json'].repository.html_url + '">' + event['body-json'].repository.html_url + '</a><br/>';
email += 'Commit: <a href="' + e.url + '">' + e.id + '</a><br/>';
email += 'Author: <a href="mailto:' + e.author.email + '">' + e.author.name + '</a><br/>';
email += 'Compare: <a href="' + event['body-json'].compare + '">' + event['body-json'].compare + '</a><br/>';
var commit_msg = '<hr>';
commit_msg += 'Author: <a href="mailto:' + e.author.email + '">' + e.author.name + '</a><br/>';
commit_msg += 'Commit: <a href="' + e.url + '">' + e.id + '</a><br/>';
commit_msg += 'Message: ' + e.message + '<br/>';
commit_msg += 'Date: ' + e.timestamp + '<br/>';

for(var k=0; k < files.length; k++){
var file = files[k];


files.forEach(function(file){

email += '<hr>' + file.to + '</hr>'; //file modified name
email += '<table style="font-family: monospace, \'Courier New\', Courier; font-size: 12px; margin: 0;">';
commit_msg += '<hr>' + file.to; //file modified name
commit_msg += '<table style="font-family: monospace, \'Courier New\', Courier; font-size: 12px; margin: 0;">';

for(var i = 0; i < file.lines.length; i++){
//Build HTML EMAIL
var change = file.lines[i];

if(change.add){
email += '<tr style="background-color:#ddffdd;">';
commit_msg += '<tr style="background-color:#ddffdd;">';
} else if (change.del){
email += '<tr style="background-color:#ffdddd;">';
commit_msg += '<tr style="background-color:#ffdddd;">';
} else if (change.normal){
email += '<tr">';
commit_msg += '<tr">';
}

if(change.add || change.del || change.normal){
email += '<td style="border-right:1px solid lightgray;">';
email += (change.normal) ? change.ln2 : change.ln;
email += '</td>';
commit_msg += '<td style="border-right:1px solid lightgray;">';
commit_msg += (change.normal) ? change.ln2 : change.ln;
commit_msg += '</td>';
//Deal with tabs and spaces for emails
var content = escape(change.content);
content = content.replace(/\t/g,'\u00a0 \u00a0');
content = content.replace(/ /g, '\u00a0');

if(change.add === true){
email += '<td>+' + content + '</td>';
commit_msg += '<td>+' + content + '</td>';
} else if (change.del){
email += '<td>-' + content + '</td>';
commit_msg += '<td>-' + content + '</td>';
} else {
email += '<td>' + content + '</td>';
commit_msg += '<td>' + content + '</td>';
}
email += '</tr>';
commit_msg += '</tr>';
}


};

email+='</table><br/>';
});
commit_msg+='</table><br/>';

email+='</body></html>';
return Promise.resolve({email:email,commit:e});
};
//console.log('commit_msg: ' + commit_msg);
email += commit_msg;
callback();
Copy link
Owner

Choose a reason for hiding this comment

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

@jwu2
If this callback() is called too early, it might prematurely terminate the lambda function. AWS doesn't guarantee immediate termination so the function might still run for a bit.

Copy link
Author

Choose a reason for hiding this comment

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

I think the callback is effective only for this commit. Not sure how it can be called too early. What is recommended way for this?

Copy link
Owner

Choose a reason for hiding this comment

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

@jwu2 This can be moved to the end after sending the email

Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure about it, but have to test it out. The current code works well.

})
.then((c)=>{
var subject = '[' + event['body-json'].repository.full_name + '] ' + c.commit.id.substring(0,6) + ': ' + c.commit.message;
var from = c.commit.author.name + '<' + c.commit.author.email + '>';
sendMail(c.email, subject, from);
.catch((err)=>{
console.log("Create message error:" + err);
callback(err);
});
}, function(err) {
if (err) return next(err);
//console.log('Final email: ' + email);
//console.log('sender.url: ' + event['body-json'].sender.url);
Copy link
Owner

Choose a reason for hiding this comment

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

@jwu2
These debugging lines should be removed.

Copy link
Author

Choose a reason for hiding this comment

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

Removed


getSenderNameFromGitHub(event['body-json'].sender.url)
.then((name)=>{
//console.log('Sender name: ' + name);
var subject = '[' + event['body-json'].repository.full_name + '] ' + name + ' has pushed changes - ' + event['body-json'].head_commit.message.substring(0,60) + '...';
var from = name + '<' + event['body-json'].pusher.email + '>';
email += '</body></html>';
sendMail(email, subject, from);
})
.catch((err)=>{
console.log("Get sender info error:" + err);
callback(err);
});
});

})
}
};


function getDiffFromGitHub(url){
var promise = new Promise((resolve,reject)=>{
https.get(url + '.diff',(res)=>{
Expand All @@ -113,6 +128,31 @@ function getDiffFromGitHub(url){
return promise;
};

function getSenderNameFromGitHub(url){
var promise = new Promise((resolve,reject)=>{
https.get(url,(res)=>{
var data='';
res.on('data',(chunk)=>{
data+=chunk;
});
res.on('end',()=>{
resolve(JSON.parse(data).name);
});

res.on('err',(err)=>{
console.log("HTTP GET ERROR GITHUB URL:" + err);
reject(err);
})
.on('error',(err)=>{
console.log(err);
reject(err);
});
});
});

return promise;
};

function sendMail(email, subject, from){

var auth = {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
"author": "Tony Truong",
"license": "MIT",
"dependencies": {
"async": "^2.0.1",
"diff-parse": "0.0.13",
"escape-html": "^1.0.3",
"nodemailer": "^2.5.0"
"nodemailer": "^2.5.0",
"smtp-connection": "^2.10.0"
}
}