-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.js
339 lines (306 loc) · 9.35 KB
/
github.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
/* global require, exports */
/* jshint -W116 */
/*eslint no-use-before-define: 0*/
"use strict";
var fs = require('fs'),
childProcess = require('child_process'),
gitcomment = require('./git-api.js')[0],
config = require('./config.js'),
log = require('./logger.js'),
dir,
scrollbackProcesses = {};
var startScrollback = function(branch, cb) {
//process.chdir(config.baseDir + 'scrollback-' + branch);
childProcess.execSync('rm -rf node_modules/');
try {
//installing npm
log.i('installing npm module...');
childProcess.execSync('npm install');
childProcess.execSync('npm rebuild node-sass');
} catch (err) {
log.e(err.message);
}
try {
//installingg bower
log.i('installing bower...');
childProcess.execSync('bower install');
} catch (err) {
log.e(err.message);
}
try {
//running gulp files
log.i('running gulp...');
childProcess.execSync('gulp');
} catch (err) {
try{
childProcess.execSync('npm rebuild node-sass');
childProcess.execSync('npm install');
childProcess.execSync('gulp');
} catch (err){log.e(err.message)}
}
//starting scrollback
log.i('Autostaging the ' + branch + ' branch ');
try {
scrollbackProcesses[branch] = childProcess.execSync('sudo start ' + branch);
log.i(scrollbackProcesses[branch].toString('utf-8'));
} catch (err) {
log.e(err.message);
}
cb();
};
var deleteDomain = function(branch) { //delete the directory when a pull request is closed
process.chdir(config.baseDir);
if (fs.existsSync(config.baseDir + 'scrollback-' + branch)) {
try {
childProcess.execSync('sudo service ' + branch + ' stop');
} catch (err) {
log.e(err.message);
}
childProcess.exec('rm -rf scrollback-' + branch + ' ' + branch + '.nginx.conf ' + branch + '.conf', function(err) {
if (err) log.e(err);
log.i('Removing the scrollback-' + branch + ' directory and all config files');
});
childProcess.exec('sudo rm ' + config.nginxDir + 'scrollback-' + branch, function(err) {
if (err) log.e(err);
log.i('Removing nginx config files');
});
childProcess.exec('sudo rm /etc/init/' + branch + '.conf', function(err) {
if (err) log.e(err);
log.i('Removing upstart config files');
});
childProcess.exec('sudo rm -rf /var/run/scrollback-' + branch, function(err) {
if (err) log.e(err);
log.i('Removing upstart config files');
});
if (scrollbackProcesses[branch]) {
// scrollbackProcesses[branch].kill();
delete scrollbackProcesses[branch];
} else {
log.i("no scrollbackProcesss");
}
} else {
log.i("no scrollback-" + branch + " directory present");
}
};
var updateDomain = function(branch, pullRequestNo, release) { //upadate the directory
if (release) dir = release;
else dir = branch;
if (fs.existsSync(config.baseDir + 'scrollback-' + dir)) {
process.chdir(config.baseDir + 'scrollback-' + dir);
log.i(process.cwd());
childProcess.exec('git pull', function() {
log.i("updating the repository scrollback-" + dir);
try {
//running gulp files
log.i('running gulp...');
childProcess.execSync('gulp');
} catch (err) {
log.i(err.message);
try {
childProcess.execSync('sudo service ' + dir + ' stop');
} catch (err2) {
log.e(err2.message);
}
startScrollback(dir, function() {
log.e('Restarting ' + dir + '.stage.scrollback.io ...');
});
return;
}
//starting scrollback
log.i('Restarting ' + dir + '.stage.scrollback.io ...');
try {
childProcess.execSync('sudo restart ' + dir);
} catch (err) {
log.e(err.message);
try {
childProcess.execSync('sudo start ' + dir);
} catch (err2) {
log.e(err2.message);
}
}
});
} else if (release) {
createDomain(branch, pullRequestNo, release);
} else createDomain(branch, pullRequestNo);
};
var createDomain = function(branch, pullRequestNo, release) { //create a new directory
var port = 7000 + pullRequestNo;
if (release) {
dir = release;
} else dir = branch;
log.i("cloning ur branch into scrollback-" + dir);
if (fs.existsSync(config.baseDir + 'scrollback-' + branch)) {
updateDomain(branch, pullRequestNo);
return;
}
var copyFile = function(readPath, writePath, lineTransform, callback) {
fs.readFile(readPath, function(err, data) {
if (err) log.e(err);
fs.writeFile(
writePath,
data.toString('utf-8').split("\n").map(lineTransform).join('\n'),
function(err1) {
if (err1) throw err1;
callback();
}
);
});
};
process.chdir(config.baseDir);
childProcess.exec(
'git clone --depth 1 --branch ' + branch +
' https://github.com/scrollback/scrollback.git scrollback-' + dir,
function(err1) {
log.i("scrollback-" + dir + " created");
if (err1 !== null) {
log.e('Error occured while checking out ' + branch, err1.message);
return;
}
var createCallback = (function() {
var numOps = 0;
return function() {
numOps++;
return function() {
numOps--;
if (numOps === 0) {
nginxOp(dir, function() {
log.i('reload nginx');
try {
childProcess.execSync("sudo nginx -s reload");
} catch (err) {
log.e(err.message);
}
});
process.chdir(config.baseDir + 'scrollback-' + dir);
startScrollback(dir, function() {
if (release) return;
gitcomment(branch, pullRequestNo);
});
}
};
};
})();
copyFile(
config.baseDir + 'scrollback-' + dir + '/server-config-template.js',
config.baseDir + 'scrollback-' + dir + '/server-config.js',
function(line) { // line transform function
return line.replace(/\$branch\b/g, dir).replace(/\$port\b/g, port);
// inside the file, write server_name $branch.stage.scrollack.io
},
createCallback()
);
copyFile(
config.baseDir + 'scrollback-' + dir + '/client-config-template.js',
config.baseDir + 'scrollback-' + dir + '/client-config.js',
function(line) { // line transform function
return line.replace(/\$branch\b/g, dir);
// inside the file, write server_name $branch.stage.scrollack.io
},
createCallback()
);
copyFile(
config.baseDir + 'scrollback-' + dir + '/lib/logger.js',
config.baseDir + 'scrollback-' + dir + '/lib/logger.js',
function(line) { // line transform function
return line.replace(/email && isProduction\(\)/, "email").replace(/Error logs/, 'Error logs from ' + branch + ' server');
// inside the file, write server_name $branch.stage.scrollack.io
},
createCallback()
);
copyFile(
config.baseDir + 'scrollback-' + dir + '/tools/nginx.conf',
config.baseDir + dir + '.nginx.conf',
function(line) { // line transform function
return line.replace(/\$branch\b/g, dir).replace(/\$port\b/g, port);
// inside the file, replace $branch with branch name and $port with port no
},
createCallback()
);
copyFile(
config.baseDir + 'scrollback-' + dir + '/tools/scrollback.template.conf',
config.baseDir + dir + '.conf',
function(line) { // line transform function
return line.replace(/\$branch\b/g, dir);
// inside the file, replace $branch with branch name
},
createCallback()
);
});
};
exports.autostage = function(state, branch, pullRequestNo, release) {
// console.log(arguments)
if (state === 'opened' || state === 'reopened') {
if (release) { //check if its a release branch
try {
childProcess.execSync('sudo stop release');
} catch (err1) {
log.e(err1.message);
}
if (fs.existsSync(config.baseDir + 'scrollback-' + release)) {
childProcess.exec('rm -rf ' + config.baseDir + 'scrollback-' + release, function(err) { //delete the previous release branch directory
if (err) {
log.e(err);
}
log.i("deleting previous release directory and creating a new one");
createDomain(branch, pullRequestNo, release);
});
} else createDomain(branch, pullRequestNo, release);
} else createDomain(branch, pullRequestNo);
}
if (state === "synchronize") {
// if (release) updateDomain(branch, pullRequestNo, release);
// else
updateDomain(branch, pullRequestNo);
return;
}
if (state === 'closed') {
deleteDomain(branch);
return;
}
//gitcomment.gitComment(branch, pullRequestNo);
};
var nginxOp = function(branch, callback) {
try {
childProcess.exec("chmod +x autostage/./run.sh");
} catch (err) {
log.e(err.message);
}
try {
childProcess.execSync("autostage/./run.sh " + branch);
} catch (err) {
log.e(err.message);
}
log.i("can start your server with upstart");
callback();
};
exports.hotfix = function(sha, user) {
log.i("creating a pull request with " + sha + " commit only")
var newBranch = user + "-hotfix";
process.chdir(config.baseDir + "scrollback");
childProcess.execSync("git checkout master");
childProcess.execSync("git pull");
try {
childProcess.execSync("git checkout -b " + newBranch + " master");
} catch (err) {
log.e(err.message);
var ar = newBranch.split("-");
log.e(ar);
newBranch = ar[0]+"-"+1+"-"+ar[1];
log.e(newBranch);
try {
log.e(newBranch);
childProcess.execSync("git checkout -b " + newBranch);
} catch(err){
log.e(err.message);
var arr = newBranch.split("-");
log.e(arr);
newBranch = arr[0]+"-"+arr[1]+1+"-"+arr[2]
log.e(newBranch);
childProcess.execSync("git checkout -b " + newBranch);
}
log.i(newBranch);
}
childProcess.execSync("git cherry-pick " + sha);
childProcess.execSync("git push origin " + newBranch);
childProcess.execSync("git checkout master");
}