-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
75 lines (64 loc) · 2.19 KB
/
server.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
const express = require('express');
const bodyParser = require('body-parser');
const { execSync } = require('child_process');
const config = require('./config.json');
const Build = require('github-build');
const server = express();
server.use(bodyParser.json());
server.post('/build/:id/:secret', (req, res) => {
if (config[req.params.id] !== req.params.secret)
return res.send({ status: 403, body: 'Forbidden.'});
if (req.headers['x-github-event'] !== 'push')
return res.send({ status: 204, body: 'Untracked event.'});
const branch = req.body.ref.match(/refs\/heads\/(.+)/)[1];
if (branch !== 'master')
return res.send({ status: 204, body: 'Untracked branch.'});
if (req.body.before === req.body.after)
return res.send({ status: 204, body: 'No changes.'});
const data = {
repo: 'yamdbf/core',
sha: req.body.after,
token: config.token,
label: 'YAMDBF Prebuilt Build',
description: 'Building YAMDBF...',
url: `https://github.com/yamdbf/core/tree/indev`
}
const build = new Build(data);
build.start().then(() => {
try
{
let result;
let opts = { cwd: config['indev'] };
console.log(`Starting YAMDBF prebuilt build as of yamdbf/master#${req.body.after}`);
execSync('git clean -df && git checkout .', opts);
execSync('git pull', opts);
try { execSync('rm -rf node_modules', opts); } catch (err) {}
execSync('yarn && gulp gh-prebuild', opts)
let gitStatus = execSync(`cd ../yamdbf-prebuilt && git status`, opts).toString();
if (gitStatus.includes('nothing to commit'))
{
data.description = 'No code changes.';
build.pass();
}
else
{
result = execSync(
`cd ../yamdbf-prebuilt && git add --all && git commit -m "Build YAMDBF prebuilt: ${req.body.after}" && git push`,
opts).toString();
console.log(result);
data.description = 'Successfully built YAMDBF.';
build.pass();
}
return res.send({ status: 200, body: 'Successfully built YAMDBF.'});
}
catch (err)
{
console.error(err);
data.description = 'YAMDBF build failed.';
build.fail();
return res.send({ status: 500, body: 'Failed building YAMDBF.'});
}
})
.catch(console.error);
});
server.listen(config.port, () => console.log('Build server started'));