This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
361 lines (331 loc) · 8.96 KB
/
handler.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
var qrcode = require('qrcode-terminal');
var crontab = require('crontab');
var Promise = require('bluebird');
var gitUtils = require('git-utils');
var fs = Promise.promisifyAll(require('fs'));
var path = require('path');
var fetch = require('node-fetch');
var nodeGit = require('nodegit');
var _ = require('lodash');
var mkdirp = Promise.promisify(require('mkdirp'));
var exec = require('child_process').exec;
var nconf = require('nconf');
var CONFIG = {
NAME: 'gitmonitor',
SERVER: process.env.GITMONITOR_SERVER || 'http://dockerhost.dev:3000',
CONFIGFILE: '/.gitmonitor/config.json',
};
module.exports = {
// for testing only
removeConfig: removeConfig, // done
readConfig: readConfig, // done
writeConfig: writeConfig, // done
installCron: installCron, // done
unInstallCron: unInstallCron, // done
getRepoInfos: getRepoInfos, // done
installRaw: installRaw, // done
uninstallRaw: uninstallRaw, // done
updateRaw: updateRaw, // done
isInstalled: isInstalled,
getAppBinaryPath: getAppBinaryPath,
cronCommand: cronCommand,
// interface
install: install,
uninstall: uninstall,
settings: settings,
update: update,
CONFIG: CONFIG
};
function cronCommand(binary, repoPath){
return binary + ' update ' + repoPath;
}
function updateRaw(repoPath){
return getRepoInfos(repoPath).bind({}).then(function(repoInfo) {
this.repoInfo = repoInfo;
return readConfig(repoPath);
}).then(function(config) {
return apiUpdateRepo({
id: config.id,
repoData: this.repoInfo
});
});
}
function settings(args) {
var repoPath = getPath(args);
var configFilePath = path.join(repoPath, CONFIG.CONFIGFILE);
return isInstalled(configFilePath, 'uninstall').then(function() {
return readConfig(repoPath);
}).then(function(config){
console.log('Scan the Qrcode with your '+CONFIG.NAME + '-App')
qrcode.generate(config.id);
console.log('You use as Server: ', config.server);
}).catch(function(err) {
showError(err);
});
}
function update(args) {
var repoPath = getPath(args);
readConfig(repoPath).then(function(config){
if(config.server){
CONFIG.SERVER = config.server
}
return updateRaw(repoPath).then(function() {
showSuccess('Updated Repo successfully');
})
}).catch(function(err) {
showError(err);
});
}
function apiCall(call) {
return fetch(CONFIG.SERVER + call.route, {
method: call.method,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(call.body)
});
}
function apiUpdateRepo(data) {
return apiCall({
method: 'put',
route: '/api/Repos/' + data.id,
body: data.repoData
});
}
function apiRemoveRepo(id) {
return apiCall({
method: 'delete',
route: '/api/Repos/' + id
});
}
function filterResponseId(data) {
return data.json().then(function(dataInner) {
return new Promise(function(resolve, reject) {
if (dataInner.id) {
resolve({
id: dataInner.id
});
} else {
reject('Failed to get Id from server');
}
});
});
}
function readConfig(repoPath) {
var configFilePath = path.join(repoPath, CONFIG.CONFIGFILE);
return fs.readFileAsync(configFilePath).then(function(data) {
return new Promise(function(resolve) {
resolve(JSON.parse(data.toString()));
});
});
}
function getDirFromPath(myPath) {
var dir = myPath.split('/')
return dir.slice(0, dir.length - 1).join('/');
}
function removeConfig(repoPath) {
var configFilePath = path.join(repoPath, CONFIG.CONFIGFILE);
var dir = getDirFromPath(configFilePath);
return fs.unlinkAsync(configFilePath).then(function() {
return fs.rmdirAsync(dir);
});
}
function writeConfig(config, repoPath) {
var configFilePath = path.join(repoPath, CONFIG.CONFIGFILE);
var dir = getDirFromPath(configFilePath);
return mkdirp(dir).then(function() {
return fs.writeFileAsync(configFilePath, JSON.stringify(config)).then(function() {
return new Promise(function(resolve) {
resolve(config);
});
})
});
}
function apiAddRepo(data, repoPath) {
return apiCall({
method: 'post',
route: '/api/Repos/',
body: data
}).then(filterResponseId).then(function(data){
data.server = CONFIG.SERVER;
return writeConfig(data, repoPath);
});
}
function promiseFromChildProcess(args) {
var deferred = Promise.pending();
exec(args, function(err, out) {
if (err) {
deferred.reject(err);
}
deferred.resolve(out);
});
return deferred.promise;
}
function installCron(repoPath) {
return Promise.promisify(crontab.load)().then(function(crontabIn) {
return [
getAppBinaryPath(),
crontabIn
]
}).spread(function(appName, crontabInner) {
var deferred = Promise.pending();
var command = cronCommand(appName.trim(), repoPath);
if (crontabInner.jobs({
command: command
}).length > 0) {
deferred.reject('Crontab already exists');
}
crontabInner.create(command, '* * * * *', 'added by ' + CONFIG.NAME);
crontabInner.save(function(err) {
if (err) {
deferred.reject(err);
}
deferred.resolve();
});
return deferred.promise;
});
};
function getAppBinaryPath(){
return promiseFromChildProcess('which ' + CONFIG.NAME);
}
function unInstallCron(repoPath) {
return Promise.promisify(crontab.load)().then(function(crontabIn) {
return [
getAppBinaryPath(),
crontabIn
]
}).spread(function(nodePath, crontabInner) {
var deferred = Promise.pending();
var command = cronCommand(nodePath.trim(), repoPath);
crontabInner.remove({
command: command
});
crontabInner.save(function(err) {
if (err) {
deferred.reject(err);
}
deferred.resolve();
});
return deferred.promise;
});
};
function getRepoInfos(repoPath) {
var deferred = Promise.pending();
var repositoryU = gitUtils.open(repoPath);
if (repositoryU) {
nodeGit.Repository.open(repoPath).then(function(repository) {
return Promise.all([
repository.getHeadCommit(),
repository.getCurrentBranch()
]);
}).then(function(res) {
var commit = res[0];
var branch = res[1].toString();
var output = {};
output.branch = branch;
output.name = path.basename(repoPath);
output.author = commit.author().toString();
output.message = commit.message();
output.notified = false;
output.isClean = (_.keys(repositoryU.getStatus()).length === 0);
output.branches = repositoryU.getReferences().heads;
output.status = repositoryU.getAheadBehindCount('refs/heads/master');
deferred.resolve(output);
}).catch(function() {
deferred.reject('Could not read repo details');
});
} else {
deferred.reject('Not a git repo');
}
return deferred.promise;
}
function install(args) {
var repoPath = getPath(args);
installRaw(repoPath).then(function(obj) {
console.log('Scan the Qrcode with your '+CONFIG.NAME + '-App')
qrcode.generate(obj.config.id);
}).catch(function(err) {
showError(err);
});
}
function installRaw(repoPath) {
var obj = {};
var configFilePath = path.join(repoPath, CONFIG.CONFIGFILE);
return isInstalled(configFilePath, 'install').then(function() {
return getRepoInfos(repoPath);
}).then(function(repoData) {
return apiAddRepo(repoData, repoPath);
}).then(function(config) {
obj.config = config;
return installCron(repoPath);
}).then(function(){
return Promise.resolve(obj);
})
}
function showSuccess(msg) {
console.log(msg);
}
function showError(msg) {
console.error(msg);
}
function uninstall(args) {
var repoPath = getPath(args);
uninstallRaw(repoPath)
.then(function() {
showSuccess('Sucessfully uninstalled');
})
.catch(function(err) {
showError(err);
});
}
function uninstallRaw(repoPath){
var configFilePath = path.join(repoPath, CONFIG.CONFIGFILE);
return isInstalled(configFilePath, 'uninstall').then(function() {
return unInstallCron(repoPath).then(function() {
return readConfig(repoPath).then(function(config) {
return apiRemoveRepo(config.id);
});
})
}).then(function(){
return removeConfig(repoPath);
})
}
function fExists(file) {
return new Promise(function(resolve) {
try {
fs.lstat(file, function(err) {
if (err) return resolve(false);
resolve(true);
});
} catch (err) {
resolve(false);
}
});
}
function getPath(args){
var repoPath = path.resolve('./');
if (args.argv._[1]) {
repoPath = path.resolve(args.argv._[1]);
}
return repoPath;
}
function isInstalled(file, iOrUi) {
return fExists(file).then(function(exists) {
return new Promise(function(resolve, reject) {
if (exists) {
if (iOrUi == 'install') {
reject('Already installed');
} else {
resolve();
}
} else {
if (iOrUi == 'uninstall') {
reject('Already uninstalled');
} else {
resolve();
}
}
});
});
}