-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
140 lines (119 loc) · 4.12 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
const async = require('async');
const bufferEq = require('buffer-equal-constant-time');
const bodyParser = require('body-parser');
const childProcess = require('child_process');
const crypto = require('crypto');
const express = require('express');
const fs = require('fs');
const PromiseQueue = require('promise-queue');
require('./tokens');
const config = require('./config');
const bumpAframeDist = require('./lib/bumpAframeDist').bumpAframeDist;
const bumpAframeDocs = require('./lib/bumpAframeDocs').bumpAframeDocs;
const bumpAframeRegistry = require('./lib/bumpAframeRegistry').bumpAframeRegistry;
const cherryPickDocCommit = require('./lib/cherryPickDocCommit').cherryPickDocCommit;
const deployAframeSite = require('./lib/deployAframeSite').deployAframeSite;
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const WEBHOOK_SECRET = process.env.SECRET_TOKEN;
// Only run one Git job at a time.
const QUEUE = new PromiseQueue(1, Infinity);
module.exports.QUEUE = QUEUE;
// Git config.
if (process.env.AFROBOT_ENV !== 'test') {
childProcess.execSync(`git config --global user.email ${config.userEmail}`);
childProcess.execSync(`git config --global user.name ${config.userName}`);
}
initExpressApp();
/**
* Express app.
*/
function initExpressApp () {
console.log('A-frobot config:', JSON.stringify(config));
const app = express();
app.set('port', process.env.AFROBOT_ENV === 'staging' ? 5001 : 5000);
app.use(bodyParser.json());
app.get('/', function (req, res) {
res.send('AFRO');
});
// Webhook handler.
app.post('/postreceive', function handler (req, res) {
res.send(postHandler(req.body, req.headers['x-hub-signature']));
});
// Express listen.
app.listen(app.get('port'), function () {
cloneRepositories();
console.log('Node app is running on port', app.get('port'));
});
}
/**
* Handle webhook.
*/
function postHandler (data, githubSignature) {
// Validate payload.
if (!bufferEq(new Buffer.from(computeSignature(data)), new Buffer.from(githubSignature))) {
console.log('Received invalid GitHub webhook signature. Check SECRET_TOKEN.');
return 403;
}
if (data.commits) {
console.log(`Received commit ${data.after} for ${data.repository.full_name}.`);
// Check that the commit is not from the bot.
if (data.head_commit.committer.email === config.userEmail ||
data.head_commit.committer.username === config.userName) {
console.log('Commit is from a-frobot, returning.');
return 204;
}
// A-Frame repository.
if (data.repository.full_name === config.repo) {
QUEUE.add(() => bumpAframeDist(data));
QUEUE.add(() => bumpAframeDocs(data));
}
// A-Frame Registry repository.
if (data.repository.full_name === config.repoRegistry) {
QUEUE.add(() => bumpAframeRegistry(data));
}
// A-Frame Site repository.
if (data.repository.full_name === config.repoSite) {
QUEUE.add(() => deployAframeSite(data));
}
}
if (data.action === 'created' && data.comment) {
console.log(`Received comment ${data.comment.body}.`);
QUEUE.add(() => cherryPickDocCommit(data));
}
return 200;
}
module.exports.postHandler = postHandler;
/**
* Clone repositories.
*/
function cloneRepositories () {
if (process.env.AFROBOT_ENV === 'test') { return; }
async.series([
clone('aframe', config.repo),
clone('aframe-registry', config.repoRegistry),
clone('aframe-site', config.repoSite),
clone('aframevr.github.io', config.repoSitePages)
], function () {
console.log('All repositories cloned.');
});
function clone (dir, repo) {
return cb => {
if (fs.existsSync(dir)) { return cb(); }
childProcess.spawn('git', ['clone', `https://${GITHUB_TOKEN}@github.com/${repo}.git`], {
stdio: 'inherit'
}).on('close', function () {
console.log(`${dir} cloned`);
this.kill('SIGINT');
cb();
});
};
}
}
/**
* Compute signature using secret token for validation.
*/
function computeSignature (data) {
data = JSON.stringify(data);
return `sha1=${crypto.createHmac('sha1', WEBHOOK_SECRET).update(data).digest('hex')}`;
}
module.exports.computeSignature = computeSignature;