-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (53 loc) · 1.8 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
/* DIRECTORY SETUP */
const fs = require("fs");
for (let folder of ['clone-dir', 'build-output', 'ssh-keys', 'logs', 'logs/build'])
if (!fs.existsSync('./' + folder))
fs.mkdirSync('./' + folder);
/* LOAD CONFIG */
const config = require("./config");
const deployment = config["deployment-server"];
/* DEFINE WEBHOOK HANDLERS */
const pushHandler = async event => {
const repo = event.payload.repository;
const branch = event.payload.ref.replace('refs/heads/', '');
clearCloneDir();
await require('./github/clone')(repo.clone_url, branch, repo.default_branch);
await require('./build')();
};
const pingHandler = event => {
const repo = event.payload.repository;
const org = event.payload.organization;
if (repo || org)
console.log(
"Successfully received GitHub webhook ping for %s %s (%s).",
org ? 'organization' : 'repository',
(repo || {}).full_name || (org || {}).login,
(repo || {}).description || (org || {}).description
);
else {
console.log('Received unknown webhook ping.');
}
};
const errorHandler = err => {
console.error('Error:', err.message)
};
/* START WEBHOOK */
require('./github/webhook')(pushHandler, pingHandler, errorHandler);
/* OTHER STUFF */
const deleteFolderRecursive = function (path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file, index) {
const curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
function clearCloneDir() {
deleteFolderRecursive('./clone-dir');
fs.mkdirSync('./clone-dir');
}