-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess.js
497 lines (388 loc) · 12.6 KB
/
process.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/**
* Process requested actions to generate PDFs of template and upload to chat app
*/
const debug = process.env.DEBUG === 'true';
const uploader = require('./integrations/' + process.env.INTEGRATION + '/uploader');
const queryString = require('query-string');
const fs = require('fs');
const unzip = require('unzip');
const emailVcs = require('email-vcs');
const wkhtmltopdf = require('wkhtmltopdf');
const octokit = require('@octokit/rest')({
debug: debug,
auth: `token ${process.env.GITHUB_API_TOKEN}`
});
//set path for lambda
if (process.env['LAMBDA_TASK_ROOT']) {
process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'];
}
//set path for travis
if (process.env['TRAVIS_BUILD_DIR']) {
process.env['PATH'] = process.env['PATH'] + ':' + process.env['TRAVIS_BUILD_DIR'];
}
module.exports = (function(){
const bot = {
octokit: octokit,
uploader: uploader,
/**
* Get the Mandrill template name from either a template name or a GH file path
*
* @param {String} templateName or filename
* @return {String} templateName
*/
getTemplateName: async function(templateOrFile=''){
if (!templateOrFile || templateOrFile.indexOf('/') === -1) {
return templateOrFile;
}
return emailVcs.getMandrillFilename(templateOrFile);
},
/**
* Get the Github filename from a Mandrill template name
*
* @param {String} templateName
* @return {String} filename
*/
getGithubFilenameFromTemplateName: async function(templateName=''){
if (!templateName || templateName.indexOf('/') !== -1) {
return templateName;
}
const files = await emailVcs.getAllFilesFromGithub();
return files.find(function(filename){
return emailVcs.getMandrillFilename(filename) === templateName;
});
},
/**
* Get the url to a file in Github
*
* @param {String} filename
* @return {String} url
*/
getGithubLink: async function(filename){
const fileContents = await emailVcs.getFileContentsFromGithub(filename, false);
return fileContents.data.html_url;
},
/**
* Get the contents of a file across all branches
*
* @param {String} filename
* @return {String} contents
*/
getFileContentsAcrossBranches: async function(filename){
let files = [];
const branches = await bot.getAllGithubBranches();
for (let i=0; i<branches.length; i++) {
const fileContents = await octokit.repos.getContents({
owner: process.env.GITHUB_OWNER,
repo: process.env.GITHUB_TEMPLATE_REPO,
ref: branches[i],
path: filename,
headers: {
'Accept': 'application/vnd.github.v3.raw'
}
});
files.push({
branch: branches[i],
contents: fileContents.data
});
}
return files;
},
/**
* Get an array of all branches in Github repo
*
* @return {Array} branch name
*/
getAllGithubBranches: async function(){
const branches = await bot.octokit.repos.getBranches({
owner: process.env.GITHUB_OWNER,
repo: process.env.GITHUB_TEMPLATE_REPO
});
return branches.data.map(function(branch){
return branch.name;
});
},
/**
* Get a PDF of a template
*
* @param {String} templateName
* @param {String} filename
* @return {fs.ReadStream} PDF ReadStream
*/
getPdfFromTemplate: async function(templateName, filename){
const fileContents = await emailVcs.getFileContentsFromGithub(filename);
const parsedFile = emailVcs.parseMarkdown(fileContents.data);
const html = bot.getTemplateHtml({
'Name': templateName,
'Filename': filename,
'From Email': parsedFile['From Email'],
'From Name': parsedFile['From Name'],
'Labels': parsedFile['Labels'],
'Subject': parsedFile['Subject'],
'Html': parsedFile['Html'],
'Text': parsedFile['Text']
});
const pdf = await bot.getPdfFromHtml(html);
return pdf;
},
/**
* Get a PDF of a template versions across all branches
*
* @param {String} templateName
* @param {String} filename
* @return {fs.ReadStream} PDF ReadStream
*/
getPdfCompareAcrossBranchesFromTemplate: async function(templateName, filename){
const files = await bot.getFileContentsAcrossBranches(filename);
//format, add branch name, and add page breaks
const html = files.map(function(file){
const parsedFile = emailVcs.parseMarkdown(file.contents);
return `<h1>Branch: ${file.branch}</h1><br />` + bot.getTemplateHtml({
'Name': templateName,
'Filename': filename,
'From Email': parsedFile['From Email'],
'From Name': parsedFile['From Name'],
'Labels': parsedFile['Labels'],
'Subject': parsedFile['Subject'],
'Html': parsedFile['Html'],
'Text': parsedFile['Text']
});
}).join('<p style="page-break-before: always">');
const pdf = await bot.getPdfFromHtml(html);
return pdf;
},
/**
* Parse files from a zip
*
* @param {String} zipPath
* @return {Array} files
*/
readFilesFromZip: async function(zipPath){
return new Promise(function(resolve, reject){
let files = [];
//extract the zip
fs.createReadStream(zipPath)
.pipe(unzip.Parse())
.on('entry', function(entry){
//read each file
let contents = '';
entry
.on('data', function(chunk){
contents += chunk;
})
.on('end', function(){
const filename = entry.path;
//only return markdown files
if (filename.indexOf('.md') === -1) {
return;
}
//remove README
if (filename.indexOf('README.md') !== -1) {
return;
}
//remove hidden files
if (filename.split('/').pop().charAt(0) === '.') {
return;
}
//remove zip name from path if needed
const path = (function(path){
path = path.split('/');
if (path.length > 1) {
path.shift();
}
return path.join('/');
})(filename);
//append to files array
files.push({
name: path,
contents: contents
});
});
}).on('close', function(){
resolve(files);
});
});
},
/**
* Get a PDF of all templates
*
* @return {fs.ReadStream} PDF ReadStream
*/
getPdfOfAllTemplates: async function(){
//download zip archive from github
const archive = await bot.octokit.repos.getArchiveLink({
owner: process.env.GITHUB_OWNER,
repo: process.env.GITHUB_TEMPLATE_REPO,
archive_format: 'zipball',
ref: process.env.GITHUB_SYNC_BRANCH
});
//save the zip
const tmp = '/tmp/' + Math.random().toString(36).slice(2) + '.zip';
fs.writeFileSync(tmp, archive.data, 'binary');
//read the files from the zip
const files = await bot.readFilesFromZip(tmp);
//format, add data, and add page breaks
const html = files.map(function(file){
const parsedFile = emailVcs.parseMarkdown(file.contents);
const templateName = emailVcs.getMandrillFilename(file.name);
return bot.getTemplateHtml({
'Name': templateName,
'Filename': file.name,
'From Email': parsedFile['From Email'],
'From Name': parsedFile['From Name'],
'Labels': parsedFile['Labels'],
'Subject': parsedFile['Subject'],
'Html': parsedFile['Html'],
'Text': parsedFile['Text']
});
}).join('<p style="page-break-before: always">');
//generate pdf
const pdf = await bot.getPdfFromHtml(html);
return pdf;
},
/**
* Get template as formatted HTML
*
* @param {Object} template data
* @return {String} Template HTML
*/
getTemplateHtml: function(template){
return `
<h1>Template: ${template['Name']}</h1><br />
<h1>Path: ${template['Filename']}</h1><br />
<h2>From Email: ${template['From Email']}</h2><br />
<h2>From Name: ${template['From Name']}</h2><br />
<h2>Labels:</h2>
<ul>
${template.Labels.map(function(label){
return '<li>' + label + '</li>';
}).join('')}
</ul><br />
<h2>Subject: ${template.Subject}</h2><br />
<h2>HTML:</h2><br />
${template.Html}
<h2>Text:</h2><br />
${template.Text}
`;
},
/**
* Generate a PDF from HTML
*
* @param {String} html
* @return {fs.ReadStream} PDF ReadStream
*/
getPdfFromHtml: function(html){
return new Promise(function(resolve, reject){
//prepend some style
html = `<style type="text/css">
html {
font-family: sans-serif;
}
br {
content: "";
margin: 2em;
display: block;
font-size: 24%;
}
</style>
${html}`;
const tmp = '/tmp/' + Math.random().toString(36).slice(2) + '.pdf';
const writeStream = fs.createWriteStream(tmp);
wkhtmltopdf(html, {
disableSmartShrinking: true,
dpi: 196
}, function(){}).pipe(writeStream);
writeStream.on('finish', function(){
resolve(fs.createReadStream(tmp));
});
});
},
/**
* Handle request to process action
*
* @param {Object} event
* @param {Object} context
* @param {Function} callback
* @return {Function} callback
*/
handler: async function(event, context, callback){
let upload = {ok: false};
let successMsg = '';
const body = (function(event){
if (typeof event.body === 'string') {
try {
return JSON.parse(event.body);
}
catch(e) {
return queryString.parse(event.body);
}
}
return event.body;
})(event);
const token = process.env.PROCESS_ACCESS_TOKEN;
const action = body.action;
const channelId = body.channel_id;
const templateFile = await bot.getGithubFilenameFromTemplateName(body.template_name);
const templateName = await bot.getTemplateName(body.template_name);
if (body.token !== token) {
const errMsg = 'Invalid request token';
return callback(null, {
statusCode: 401,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text: errMsg
})
});
}
//read single template command
if (action === 'template') {
const filename = templateName + '.pdf';
successMsg = 'Generating PDF for ' + templateName;
const pdf = await bot.getPdfFromTemplate(templateName, templateFile);
try {
upload = await bot.uploader.uploadPdf(channelId, filename, pdf);
}
catch(e){}
}
//read template compare command
if (action === 'compare') {
const filename = templateName + '-compare.pdf';
successMsg = 'Generating PDF for ' + templateName + ' across branches';
const pdf = await bot.getPdfCompareAcrossBranchesFromTemplate(templateName, templateFile);
try {
upload = await bot.uploader.uploadPdf(channelId, filename, pdf);
}
catch(e){}
}
//read all templates
if (action === 'all') {
const filename = 'all-templates.pdf';
successMsg = 'Generating PDF for all templates';
const pdf = await bot.getPdfOfAllTemplates();
try {
upload = await bot.uploader.uploadPdf(channelId, filename, pdf);
}
catch(e){}
}
//success
if (upload.ok) {
return callback(null, {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text: successMsg
})
});
}
//fail
const errMsg = upload.error || 'No action requested';
return callback(null, {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text: errMsg
})
});
}
};
return bot;
})();