-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
387 lines (352 loc) · 10.9 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
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
var http = require('http');
var async = require('async');
require('dotenv').load();
const PORT = process.env.PORT || 5000;
const NAME_LENGTH = process.env.NAME_LENGTH || 30;
// Github Configuration
const GITHUB_SECRET = process.env.GITHUB_SECRET || false;
const GITHUB_TOKEN = process.env.GITHUB_TOKEN || false;
// Bitbucket Configuration
const BITBUCKET_USER = process.env.BITBUCKET_USER || false;
const BITBUCKET_PASS = process.env.BITBUCKET_PASS || false;
// Slack Configuration
const SLACK_URL = process.env.SLACK_URL || false;
const SLACK_USER = process.env.SLACK_USER || 'CI Agent';
const SLACK_ICON = process.env.SLACK_ICON || ':computer:';
const SLACK_CHANNEL = process.env.SLACK_CHANNEL || false;
// Remote Docksal Configuration
const docksalDomain = process.env.DOCKSAL_DOMAIN || false;
const buildDirectory = process.env.BUILD_DIRECTORY || "/home/ubuntu/builds";
const DOCKSAL_REMOTE_HOST = process.env.DOCKSAL_REMOTE_HOST || false;
const DOCSKAL_REMOTE_PORT = process.env.DOCKSAL_REMOTE_PORT || 22;
const DOCKSAL_REMOTE_USER = process.env.DOCKSAL_REMOTE_USER || 'ubuntu';
const DOCKSAL_REMOTE_KEY = process.env.DOCKSAL_REMOTE_KEY || false;
/**
* Github Event Listener
*/
var createGitHubHandler = require('github-webhook-handler');
var gitHubHandler = createGitHubHandler({ path: '/webhook', secret: GITHUB_SECRET });
/**
* BitBucket Event Listener
*/
var createBitBucketHandler = require('bitbucket-webhook-handler');
var bitBucketHandler = createBitBucketHandler({ path: '/webhook' });
http.createServer(function (req, res) {
// Bitbucket API
if((req.headers['user-agent']).search('Bitbucket-Webhooks') != -1){
bitBucketHandler(req, res, function(err) {
});
}
// Github Webhook
else if((req.headers['user-agent']).search('GitHub-Hookshot') != -1) {
if(GITHUB_SECRET == false){
res.statusCode = 404;
res.end('GITHUB_SECRET not set');
}else {
gitHubHandler(req, res, function (err) {
})
}
}
// Send 404
else {
res.statusCode = 404;
res.end('no such location');
}
}).listen(PORT);
/**
* GitHub Handler
*/
gitHubHandler
.on('error', function (err) {
console.error('Error:', err.message)
})
.on('pull_request', function(event){
console.log('Received a pull request event for %s',
event.payload.repository.name
);
var provider = github_variables(event);
event.provider = 'github';
if(event.payload.action == 'closed'){
deleteRemote(provider);
} else {
deployRemote(provider);
}
});
/**
* Delete the remote
*/
function deleteRemote(provider){
// Delete the remote if available
async.series([
function (callback) {
var cmd = "(cd " + provider.remoteBuildDirectory + " && fin rm -f) || true";
ssh_cmd(cmd, callback);
},
function (callback) {
var cmd = "if [ -d " + provider.remoteBuildDirectory + " ]; then sudo rm -rf " + provider.remoteBuildDirectory + '; fi';
ssh_cmd(cmd, callback);
}
]);
}
function deployRemote(provider){
var msg = 'Completed sandbox build for branch *' + provider.branch + '*: http://' + provider.domain;
async.series([
function (callback) {
var cmd = "(cd " + provider.remoteBuildDirectory + " && fin rm -f) || true";
ssh_cmd(cmd, callback);
},
function (callback) {
var cmd = "sudo rm -rf " + provider.remoteBuildDirectory + "; mkdir -p " + provider.remoteBuildDirectory;
ssh_cmd(cmd, callback);
},
function (callback) {
var cmd = "cd " + provider.remoteBuildDirectory + " && git clone --branch='" + provider.branch + "' --depth 50 " + provider.repoURL + " . && git reset --hard " + provider.hash + " && ls -la";
ssh_cmd(cmd, callback);
},
function (callback) {
var cmd = "cd " + provider.remoteBuildDirectory + " && echo COMPOSE_PROJECT_NAME=" + provider.compose_project_name + " | tee -a .docksal/docksal-local.env";
ssh_cmd(cmd, callback);
},
function (callback) {
var cmd = "cd " + provider.remoteBuildDirectory + " && echo VIRTUAL_HOST=" + provider.domain + " | tee -a .docksal/docksal-local.env";
ssh_cmd(cmd, callback);
},
function (callback) {
var cmd = "cd " + provider.remoteBuildDirectory + " && echo APACHE_BASIC_AUTH_USER=$HTTP_USER | tee -a .docksal/docksal-local.env";
//ssh_cmd(cmd, callback);
callback();
},
function (callback) {
var cmd = "cd " + provider.remoteBuildDirectory + " && echo APACHE_BASIC_AUTH_PASS=$HTTP_PASS | tee -a .docksal/docksal-local.env";
//ssh_cmd(cmd, callback);
callback();
},
function (callback) {
var cmd = "cd " + provider.remoteBuildDirectory + " && fin start";
ssh_cmd(cmd, callback);
},
function (callback) {
if(provider.provider == 'github') {
github_post({
owner: provider.repository.owner.login,
repo: provider.repo,
number: provider.pr.number,
body: msg
});
}
else if(provider.provider == 'bitbucket'){
provider.message = msg;
bitbucket_post(provider);
}
callback();
},
function (callback) {
slack_post(msg);
callback()
}
]);
}
/**
*
* @param event
* @returns {{repository, pr: *, branch, branchNameSafe: string, repo, repoNameSafe: string, domain: string, repoURL: *, compose_project_name: string, hash: string, remoteBuildDirectory: string}}
*/
function github_variables(event) {
var repository = event.payload.repository;
var pr = event.payload.pull_request;
var branch = pr.head.ref;
var branchNameSafe = name_safe(branch);
var repo = repository.name;
var repoNameSafe = name_safe(repo);
var domain = branchNameSafe + '-' + repoNameSafe + '.' + docksalDomain;
var repoURL = repository.git_url;
var compose_project_name = branchNameSafe + "-" + repoNameSafe;
var hash = pr.head.sha;
var remoteBuildDirectory = buildDirectory + compose_project_name;
return {
repository: repository,
pr: pr,
branch: branch,
branchNameSafe: branchNameSafe,
repo: repo,
repoNameSafe: repoNameSafe,
domain: domain,
repoURL: repoURL,
compose_project_name: compose_project_name,
hash: hash,
remoteBuildDirectory: remoteBuildDirectory
}
}
/**
*
* @param event
* @returns {{repository, pr: *, branch, branchNameSafe: string, repo, repoNameSafe: string, domain: string, repoURL: string, compose_project_name: string, hash: string | AlgorithmIdentifier, remoteBuildDirectory: string}}
*/
function bitbucket_variables(event) {
var repository = event.payload.repository;
var owner = repository.owner.username;
var pr = event.payload.pullrequest;
var branch = pr.source.branch.name;
var branchNameSafe = name_safe(branch);
var repo = repository.name;
var repoNameSafe = name_safe(repo);
var domain = branchNameSafe + '-' + repoNameSafe + '.' + docksalDomain;
var repoURL = "[email protected]:" + owner + "/" + repo + ".git";
var compose_project_name = branchNameSafe + "-" + repoNameSafe;
var hash = pr.source.commit.hash;
var remoteBuildDirectory = buildDirectory + compose_project_name;
var pullRequestId = pr.id;
return {
repository: repository,
pr: pr,
branch: branch,
branchNameSafe: branchNameSafe,
repo: repo,
repoNameSafe: repoNameSafe,
domain: domain,
repoURL: repoURL,
compose_project_name: compose_project_name,
hash: hash,
remoteBuildDirectory: remoteBuildDirectory,
pullRequestId: pullRequestId,
owner: owner
}
}
/**
* BitBucket Handler
*/
bitBucketHandler
.on('pullrequest:created', function(event) {
var provider = bitbucket_variables(event);
provider.provider = 'bitbucket';
deployRemote(provider);
})
.on('pullrequest:updated', function(event) {
var provider = bitbucket_variables(event);
provider.provider = 'bitbucket';
deployRemote(provider);
})
.on('pullrequest:approved', function(event) {
var provider = bitbucket_variables(event);
provider.provider = 'bitbucket';
deleteRemote(provider);
})
.on('pullrequest:unapproved', function(event) {
var provider = bitbucket_variables(event);
provider.provider = 'bitbucket';
deleteRemote(provider);
})
.on('pullrequest:rejected', function(event) {
var provider = bitbucket_variables(event);
provider.provider = 'bitbucket';
deleteRemote(provider);
});
/**
* Convert the provided argument into a safe name.
* @param name
* @returns {string}
*/
function name_safe(name) {
var newName = name.toLowerCase();
if(newName.length > NAME_LENGTH){
var md5 = require('md5');
newName = newName.substr(0, NAME_LENGTH) + (md5(newName)).substr(0,4);
}
return newName;
}
/**
* Run ssh commands to remote server
*
* @param cmd
* @param callback
*/
function ssh_cmd(cmd, callback) {
var Client = require('ssh2').Client;
var conn = new Client();
console.log(cmd);
conn.on('ready', function() {
conn.exec(cmd, function(err, stream) {
if (err) throw err;
stream.on('close', function(code, signal) {
//console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
conn.end();
callback();
}).on('data', function(data) {
//console.log('STDOUT: ' + data);
}).stderr.on('data', function(data) {
//console.log('STDERR: ' + data);
});
});
}).connect({
host: DOCKSAL_REMOTE_HOST,
port: DOCSKAL_REMOTE_PORT,
username: DOCKSAL_REMOTE_USER,
privateKey: DOCKSAL_REMOTE_KEY
});
}
/**
* Send message to slack
* @param msg
*/
function slack_post(msg) {
if (SLACK_URL == false || SLACK_CHANNEL == false || msg == ""){
return;
}
const { IncomingWebhook } = require('@slack/client');
const webhook = new IncomingWebhook(SLACK_URL, {
username: SLACK_USER,
iconEmoji: SLACK_ICON,
channel: SLACK_CHANNEL
});
// Send simple text to the webhook channel
webhook.send(msg, function(err, res) {
if (err) {
console.log('Error:', err);
}
});
}
/**
* Post message to Github PR
* @param comment
*/
function github_post(comment) {
if(GITHUB_TOKEN == false){
return;
}
var GitHubApi = require('github');
var github = new GitHubApi({
timeout: 5000
});
// user token
// @Todo: Otherways of authenticating
github.authenticate({
type: 'token',
token: GITHUB_TOKEN
});
github.issues.createComment({
'owner': comment.owner,
'repo': comment.repo,
'number': comment.number,
'body': comment.body
});
}
/**
* Post message to Bitbucket API
* @param comment
*/
function bitbucket_post(comment) {
if (BITBUCKET_USER == false || BITBUCKET_PASS == false){
return;
}
var bitbucket = require('bitbucket-api');
var credentials = {username: BITBUCKET_USER, password: BITBUCKET_PASS};
var client = bitbucket.createClient(credentials);
var repository = client.getRepository({slug: comment.repo, owner: comment.owner}, function (err, repo) {
if (err) throw err;
repo.pullrequest(comment.pullRequestId).comments().create(comment.message, function(err, res){
if (err) {
console.log('Error:', err);
}
});
});
}