-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
152 lines (121 loc) · 5.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var https = require('https');
var crypto = require('crypto');
var config = require('./config.json');
var parse = require('diff-parse');
var nodemailer = require('nodemailer');
var escape = require('escape-html');
exports.handler = (event, context, callback) => {
var githubHmac = event.params.header['X-Hub-Signature'].replace('sha1=','');
var lambdaHmac = crypto.createHmac('sha1',config.github.secret).update(JSON.stringify(event['body-json'])).digest('hex');
if(githubHmac !== lambdaHmac){
context.fail("Invalid GitHub Secret",null);
} else {
var commits = event['body-json'].commits;
commits.forEach(function(e,i,a){
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/>';
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;">';
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;">';
} else if (change.del){
email += '<tr style="background-color:#ffdddd;">';
} else if (change.normal){
email += '<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>';
//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>';
} else if (change.del){
email += '<td>-' + content + '</td>';
} else {
email += '<td>' + content + '</td>';
}
email += '</tr>';
}
};
email+='</table><br/>';
});
email+='</body></html>';
return Promise.resolve({email:email,commit:e});
})
.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)=>{
callback(err);
});
});
}
};
function getDiffFromGitHub(url){
var promise = new Promise((resolve,reject)=>{
https.get(url + '.diff',(res)=>{
var data='';
res.on('data',(chunk)=>{
data+=chunk;
});
res.on('end',()=>{
resolve(data);
});
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 = {
user: config.mailConfig.username,
pass: config.mailConfig.password
};
var options = {
host:config.mailConfig.host,
port: config.mailConfig.port,
secure: config.mailConfig.secure,
requireTLS: config.mailConfig.requireTLS
};
if(auth.user.length > 0 && auth.pass.length > 0){
options['auth'] = auth;
options['authMethod'] = config.mailConfig.authMethod;
}
var transporter = nodemailer.createTransport(options);
// setup e-mail data with unicode symbols
var mailOptions = {
from: from,
to: config.mailConfig.to, // can be a command separated list of receivers
subject: subject,
html: email
};
transporter.sendMail(mailOptions, function(error, info){
if(error) console.log(error);
console.log('Message sent: ' + info.response);
transporter.close();
});
};