-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
94 lines (94 loc) · 2.99 KB
/
index.js
File metadata and controls
94 lines (94 loc) · 2.99 KB
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
var https=require("https"),
http=require("http"),
querystring=require("querystring"),
nodemailer=require("nodemailer"),
parse=require("url").parse,
transporter=nodemailer.createTransport({
service:"Gmail",
auth:{
user:"<GMAIL-ID>@gmail.com",
pass:"<GMAIL-PASSWORD>"
}
}),
mailOptions={
from:"Bhaskar Tiwari <[email protected]>",
to:"[email protected]",
};
if(!process.argv[2] || !process.argv[3]){
console.log("Syntax : node[js] index.js <Port> <GitHub OAuth Token> OR node[js] index.js <Port> <User-ID> <Password>");
process.exit(1);
}
if(process.argv[4]) process.argv[3]+=":"+process.argv[4];
https.get({host:"api.github.com",path:"/user",headers:{"user-agent":"bot"},auth:process.argv[3]},function(res){
var data="";
res.setEncoding("ascii");
res.on("readable",function(){
data+=res.read();
}).on("end",function(){
data=JSON.parse(data);
if(data.message){
console.log("Login Failed : "+data.message);
process.exit(1);
}
console.log("Logged In : "+data.name+" ("+data.login+")");
data.repos_url=parse(data.repos_url);
https.get({host:data.repos_url.host,path:data.repos_url.path,headers:{"user-agent":"bot"}},function(res){
res.setEncoding("ascii");
var data="";
res.on("readable",function(){
data+=res.read();
}).on("end",function(){
http.get("http://api.ipify.org?format=json",function(res){
var ip="";
res.on("readable",function(){
ip+=res.read();
}).on("end",function(){
ip=JSON.parse(ip).ip;
console.log("Callback URL : http://"+ip+":"+process.argv[2]);
JSON.parse(data).forEach(function(repo){
console.log("Found : "+repo.name+" ("+repo.html_url+")")
var data=querystring.stringify({
"hub.mode":"subscribe",
"hub.topic":repo.html_url+"/events/push",
"hub.callback":"http://"+ip+":"+process.argv[2]
}),
req=https.request({host:"api.github.com",method:"POST",path:"/hub",headers:{"user-agent":"bot"},auth:process.argv[3]},function(res){
var data="";
res.setEncoding("ascii");
res.on("readable",function(){
data+=res.read();
}).on("end",function(){
if(data.trim()=="") console.log("Subscribed : "+repo.name+" ("+repo.html_url+")");
});
}.bind(repo));
req.write(data);
req.end();
});
});
});
});
});
});
});
http.createServer(function(req,res){
req.setEncoding("ascii");
var data="";
req.on("readable",function(){
data+=req.read();
}).on("end",function(){
res.end();
data=JSON.parse(querystring.parse(data).payload);
if(data.commits){
console.log("Triggered : "+data.commits[0].url);
mailOptions.subject="pushNotifier: "+data.repository.name+" ("+data.commits[0].committer.name+")";
mailOptions.text=JSON.stringify(data.commits[0]);
transporter.sendMail(mailOptions,function(err,msg){
if(err){
console.log("Error : "+err);
} else {
console.log("Email Sent : "+mailOptions.to);
}
});
}
});
}).listen(process.argv[2]);