-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
131 lines (105 loc) · 4.39 KB
/
app.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
const Promise = require('bluebird');
const helpers = require('./src/helpers.js');
const githubhook = require('githubhook');
const nodegit = require('nodegit');
const fse = require('fs-extra');
const removeAll = Promise.promisify(require('fs-extra').remove);
const removeAllNp = require('fs-extra').remove;
const ensureDir = Promise.promisify(require('fs-extra').ensureDir);
const walk = require('walk').walk;
const path = require('path');
const config = require('./server-config.js');
const resultServer = require('./src/ci-results.js');
const logFolder = path.join(config.rootDir, 'logs');
const updateStatus = Promise.promisify(helpers.updateStatus);
const github = githubhook(config.gitHubHookConfig);
if (!fse.existsSync(config.rootDir)) {
fse.mkdirSync(config.rootDir);
}
global.appRoot = path.resolve(__dirname);
github.on('push', (repo, ref, data) => {
const reqConfig = helpers.parseRequestConfig(ref, repo, data, config);
//todo add checks against config ignore/accept lists
//if(config.acceptedBranches.length > 0 && (config.acceptedBranches.indexOf()))
const logger = helpers.setupLogging(reqConfig, logFolder);
const targetUrlHost = config.resultsProtocol + data.request.headers.host.split(':')[0] // this needs to pull from the config
.concat(':')
.concat(config.resultsPort)
.concat('/results')
.concat(`?repo=${repo}`)
.concat(`&branch=${reqConfig.branchName}`)
.concat(`&commit=${reqConfig.sha1}`);
logger.info(`Received push from ${repo}:${ref}`);
logger.info(`Target URL: ${targetUrlHost}`);
logger.info('Request config loaded: ', reqConfig);
logger.profile(reqConfig.sha1);
updateStatus(reqConfig.postUrl, 'pending', 'Running tasks', null).then((updateResponse) => {
logger.info(`Update status complete: ${updateResponse.statusCode} ${updateResponse.statusMessage}`);
logger.info(`Removing existing folder @ ${reqConfig.branchFullPath}`);
return removeAll(reqConfig.branchFullPath);
})
.then((removeError) => {
if (removeError) throw removeError;
logger.info(`Creating clone directory @ ${reqConfig.baseBranchPath}`);
return ensureDir(reqConfig.baseBranchPath);
})
.then((createdPath) => { // eslint-disable-line no-unused-vars
logger.info(`Cloning from ${reqConfig.repoUrl} - checkout branch is ${reqConfig.branchName}`);
var cloneOptions = new nodegit.CloneOptions();
cloneOptions.checkoutBranch = reqConfig.branchName;
return nodegit.Clone(reqConfig.repoUrl, reqConfig.branchFullPath, cloneOptions);
})
.then(repo2 => new Promise((resolve, reject) => { // eslint-disable-line no-unused-vars
const items = [];
const walker = walk(reqConfig.branchFullPath, {
followLinks: false,
filters: config.ignoreDirs,
});
walker.on('file', (root, fileStat, next) => {
if (fileStat.type === 'file' && fileStat.name.toLowerCase() === config.configFileName) {
fse.readFile(`${root}\\${fileStat.name}`, (err, data2) => {
if (err) throw err;
logger.info(`Found config @ ${root}`);
items.push({
path: root,
name: fileStat.name,
config: JSON.parse(data2),
});
});
logger.info("Configs found: ${items}")
}
next();
});
walker.on('errors', (root, nodeStatsArray, next) => {
next();
});
walker.on('end', () => {
resolve(items);
});
}))
.then(configFiles =>
Promise.reduce(configFiles, (tot, curConfig) =>
Promise.reduce(curConfig.config.scripts, (innerIndex, curCommand) => {
logger.info(`Running script: ${curCommand} @ path ${curConfig.path}`);
return helpers.runTask(curCommand, curConfig.path, logger);
}, 0), 0))
.then(() => {
logger.info('All tasks completed');
helpers.updateStatus(reqConfig.postUrl, 'success', 'all tasks completed successfully', targetUrlHost);
})
.catch((ex) => {
logger.error('Error executing task(s)', ex);
helpers.updateStatus(reqConfig.postUrl, 'failure', 'error executing task(s)', targetUrlHost);
})
.finally(() => {
removeAllNp(reqConfig.branchFullPath);
})
.done(() => {
logger.profile(reqConfig.sha1);
logger.info(`All done for ${reqConfig.sha1}`);
return true;
});
return true;
});
github.listen();
resultServer.setupResultsServer(logFolder);