-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
205 lines (184 loc) · 7.42 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* GitHub to S3 Pusher: an AWS Lambda function, triggered by Github Webhook via SNS, to
* sync changes on commit to an S3 bucket.
*
* Author: Matt Boggie, New York Times R&D Lab
* Copyright: The New York Times Company
* Version: 0.01, November 2015
*/
// dependencies
var async = require('async');
var AWS = require('aws-sdk');
var util = require('util');
var fs = require('fs');
var GitHubApi = require('github');
var mime = require('mime');
var secretfile = "./githubtoken.secret"
// This should be a file containing the github Personal Access Token, encrypted using AWS-KMS and base 64 decoded.
// See the README for more detail.
var github = new GitHubApi({
version: '3.0.0',
debug: false,
protocol: "https",
host: "api.github.com"
});
// get reference to S3 client
var s3client = new AWS.S3();
var s3bucket = "internal.nytlabs.com";
// This handler is called by the AWS Lambda controller when a new SNS message arrives.
exports.handler = function(event, context) {
// get the incoming message
var githubEvent = event.Records[0].Sns.Message;
var mesgattr = event.Records[0].Sns.MessageAttributes;
if ((mesgattr.hasOwnProperty('X-Github-Event')) && (mesgattr['X-Github-Event'].Value == "push")) {
var eventObj = JSON.parse(githubEvent);
var re = new RegExp(/([^\/]*)/);
var found = re.exec(eventObj.repository.full_name);
var user = found[0];
var repo = eventObj.repository.name;
var sha = eventObj.head_commit.id;
var repostring = "/repos/"+user+"/"+repo+"/commits/"+sha
console.log("DEFINITELY Got a push message. Will get code from: ", repostring);
// solution borrowed from http://stackoverflow.com/questions/29372278/aws-lambda-how-to-store-secret-to-external-api
var encryptedSecret = fs.readFileSync(secretfile);
var token = null;
// get key management client for decrypting secrets
var kms = new AWS.KMS({region:'us-east-1'});
var params = {
CiphertextBlob: encryptedSecret
};
kms.decrypt(params, function(err, data) {
if (err) console.log(err, err.stack);
else {
token = data['Plaintext'].toString();
}
if (!token){
context.fail("Couldn't retrieve github token. Exiting.");
}
else{
// Authenticate to pull code
github.authenticate({
type: 'oauth',
token: token
});
var gitmsg = {"user": user, "repo": repo, "sha": sha};
github.repos.getCommit({"user": user, "repo": repo, "sha": sha}, function(err, result){
console.log("result:", result);
if(err) {
context.fail("Failed to get commit: ", err);
}
else
{
parseCommit(result, user, repo, function(err){
if(err) {
context.fail("Parsing the commit failed: " + err);
}
else
{
console.log("Commit parsed and synced (mostly?) successfully.")
context.succeed();
}
});
}
}); //end github callback
} //end token else
}); //end decrypt callback
} //end if github message
else {
console.log("Message was not a github push message. Exiting.");
context.succeed();
}
}; //end index handler
function parseCommit(resobj, user, repo, callback){
/*
* Brief note:
* "callback" gets called when the whole commit is parsed
* "eachcb" gets called for each file as it completes processing by the iterator,
* "wfcb" gets called by each step of the "waterfall" so that actions happen in the right order
*
* Two of these (eachcb and wfcb) are passed, if appropriate, to the S3 calls so that they can
* report their own completion.
*/
if((resobj.files) && (resobj.files.length >0)) {
async.each(resobj.files, function(file, eachcb){
if(file.status == "removed") {
s3delete(file.filename, eachcb);
}
else {
if(file.status == "renamed") {
async.waterfall([
function calldeleter(wfcb) {
s3delete(file.previous_filename, wfcb);
},
function callputter(wfcb) {
s3put(file, user, repo, wfcb);
}], function done(err) {
eachcb(err);
});
}
else
{
s3put(file, user, repo, eachcb);
}
}
}, function(err){
console.log("I should be all done now. Here's what error says: ", err)
callback(err); //
});
}
else{
console.log("Commit at " + resobj.html_url + " had no files. Exiting.");
callback(new Error('No files in commit object'));
}
}
function s3delete(filename, cb){
console.log("Deleting ", filename);
var params = { Bucket: s3bucket, Key: filename };
async.waterfall([
function callDelete(callback){
s3client.deleteObject(params, callback);
}
], function done(err){
if(err) {
console.log("Couldn't delete " + filename + ": " + err);
}
else {
console.log("Deleted " + filename + " from " + s3bucket);
}
cb(); //not passing err here because I don't want to short circuit processing the rest of the array
//also not calling cb until we're actually done, i.e. in the completion step of the waterfall
}
);
}
function s3put(file, user, repo, cb){
console.log("Storing " + file.filename);
async.waterfall([
function download(callback){
//call github for file contents
console.log("downloading " + file.filename);
var params = { user: user, repo: repo, sha: file.sha};
github.gitdata.getBlob(params, callback);
},
function store(result, callback){
//get contents from returned object
blob = new Buffer(result.content, 'base64');
mimetype = mime.lookup(file.filename);
isText = (mime.charsets.lookup(mimetype) == 'UTF-8');
if(isText){
blob = blob.toString('utf-8');
}
console.log("putting " + file.filename + " of type " + mimetype);
var putparams = { Bucket: s3bucket, Key: file.filename, Body: blob, ContentType: mimetype};
s3client.putObject(putparams, callback);
}
], function done(err){
if (err){
console.log("Couldn't store " + file.filename + " in bucket " + s3bucket + "; " + err);
}
else {
console.log("Saved " + file.filename + " to " + s3bucket + " successfully.");
}
cb(); //not passing err here because I don't want to short circuit processing the rest of the array
}
);
}