-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) => { | ||
|
||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jwu2 This can be moved to the end after sending the email There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jwu2 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)=>{ | ||
|
@@ -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 = { | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?