This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
410 lines (361 loc) · 13.8 KB
/
main.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
'use strict';
const getRepoInfo = require('git-repo-info');
const GitHubHook = require('githubhook');
const glob = require('glob');
const path = require('path');
const Promise = require('bluebird'); //jshint ignore:line
const shell = require('shelljs');
class GitHubListener extends global.AKP48.pluginTypes.Generic {
constructor(AKP48) {
super(AKP48, 'GitHubListener');
}
load() {
this._isRepo = (getRepoInfo._findRepo('.') !== null);
if(this.config.enabled) {
this._listener = new GitHubHook({
path: this.config.path,
port: this.config.port,
secret: this.config.secret,
logger: { //define a logger object, so the module doesn't just use console directly.
log: function(msg){
global.logger.silly(`${self.name}|GitHubHook: ${msg}`);
},
error: function(msg){
global.logger.error(`${self.name}|GitHubHook: ${msg}`);
}
}
});
global.logger.info(`${this.name}: Listening for Webhooks from GitHub.`);
global.logger.debug(`${this.name}: Listening at ${this.config.path} on ${this.config.port}.`);
global.logger.silly(`${this.name}: Listening for repo ${this.config.repository}, branch ${this.config.branch}.`);
this._listener.listen();
var self = this;
this._listener.on(`push`, function (repo, ref, data) {
if(data.deleted) {
return;
}
global.logger.silly(`${self.name}: Received Webhook: ref => ${ref}.`);
var branch = ref.substring(ref.indexOf('/', 5) + 1);
//send out alert.
var commits = `${data.commits.length} commit`.pluralize(data.commits.length);
var url = data.compare;
var msg = [];
msg.push({style: 'pink', text: '[Github] '});
msg.push({style: 'green', text: `[${repo}]`});
msg.push(` ${commits} ${data.forced && !data.created ? 'force ' : ''}pushed to ${data.created ? 'new ' : ''}`);
msg.push(`${data.ref.startsWith('refs/tags/') ? 'tag' : 'branch'} `);
msg.push({style: 'bold', text: `${branch} `});
msg.push(`by ${data.pusher.name}. `);
msg.push(`(${url})`);
for (var i = 0; i < data.commits.length && i < 3; i++) {
var _c = data.commits[data.commits.length - 1 - i];
var _m = _c.message;
var end = _m.indexOf('\n');
msg.push('\n');
msg.push({style: 'green', text: `[${_c.id.substring(0,7)}] `});
msg.push(`${_c.author.username}: ${_m.substring(0, end === -1 ? _m.length : end)}`);
}
if(self.shouldSendAlert('push')) {
global.logger.verbose(`${self.name}: Sending alert.`);
AKP48.sendAlert(msg);
}
if(self.shouldUpdate(branch) && repo === self._config.repository) {
self.handle(branch, data);
}
}).on(`pull_request`, function(repo, ref, data) {
if(!self.shouldSendAlert('pull_request')) { return; }
if(data.action === 'closed' && data.pull_request.merged) {
data.action = 'merged';
}
if(data.pull_request.title.length >= 80) {
data.pull_request.title = data.pull_request.title.substring(0,80) + '...';
}
var out = [];
out.push({style: 'pink', text: '[Github] '});
out.push({style: 'green', text: `[${repo}] `});
out.push(`Pull Request ${data.number} ${data.action}. Title: ${data.pull_request.title}`);
AKP48.sendAlert(out);
}).on(`issues`, function(repo, ref, data) {
if(!self.shouldSendAlert('issues')) { return; }
if(data.issue.title.length >= 80) {
data.issue.title = data.issue.title.substring(0,80) + '...';
}
if(data.action === 'assigned' || data.action === 'unassigned') {
data.action += ` ${data.action === 'unassigned' ? 'from' : 'to'} ${data.assignee.login}`;
}
if(data.action === 'labeled' || data.action === 'unlabeled') {
data.action += ` ${data.label.name}`;
}
var out = [];
out.push({style: 'pink', text: '[Github] '});
out.push({style: 'green', text: `[${repo}] `});
out.push(`Issue ${data.issue.number} ${data.action}. Title: ${data.issue.title}`);
AKP48.sendAlert(out);
}).on(`issue_comment`, function(repo, ref, data) {
if(!self.shouldSendAlert('issue_comment')) { return; }
if(data.comment.body.length >= 80) {
data.comment.body = data.comment.body.substring(0,80) + '...';
}
var out = [];
out.push({style: 'pink', text: '[Github] '});
out.push({style: 'green', text: `[${repo}] `});
out.push(`New comment on issue ${data.issue.number} by `);
out.push({style: bold, text: `${data.comment.user.login}. `});
out.push(`${data.comment.body} (${data.comment.html_url})`);
AKP48.sendAlert(out);
}).on(`gollum`, function(repo, ref, data) {
if(!self.shouldSendAlert('gollum')) { return; }
for (var i = 0; i < data.pages.length; i++) {
var pg = data.pages[i];
var out = [];
out.push({style: 'pink', text: '[Github] '});
out.push({style: 'green', text: `[${repo}] `});
out.push(`Wiki Page`);
out.push({style: bold, text: `${pg.page_name} `});
out.push(`${pg.action}. (${pg.html_url})`);
AKP48.sendAlert(out);
}
}).on(`fork`, function(repo, ref, data) {
if(!self.shouldSendAlert('fork')) { return; }
var out = [];
out.push({style: 'pink', text: '[Github] '});
out.push({style: 'green', text: `[${repo}] `});
out.push(`New Fork!`);
out.push({style: bold, text: `${data.sender.login} `});
out.push(`forked the repo! (${data.forkee.html_url})`);
AKP48.sendAlert(out);
}).on(`watch`, function(repo, ref, data) {
if(!self.shouldSendAlert('watch')) { return; }
var out = [];
out.push({style: 'pink', text: '[Github] '});
out.push({style: 'green', text: `[${repo}] `});
out.push(`New Star!`);
out.push({style: bold, text: `${data.sender.login} `});
out.push(`starred the repo!`);
AKP48.sendAlert(out);
}).on(`repository`, function(repo, ref, data) {
if(!self.shouldSendAlert('watch')) { return; }
var out = [];
out.push({style: 'pink', text: '[Github] '});
out.push({style: 'green', text: `[${data.organization.login} Organization] `});
out.push(`Repository `);
out.push({style: 'bold', text: `${data.repository.name} `});
out.push(`${data.action} by `);
out.push({style: 'bold', text: `${data.sender.login}`});
out.push(`. (${data.repository.html_url})`);
AKP48.sendAlert(out);
}).on('commit_comment', function (repo, ref, data) {
if (!self.shouldSendAlert('commit_comment')) { return; }
var out = [];
out.push({style: 'pink', text: '[Github] '});
out.push({style: 'green', text: `[${repo}] `});
out.push(`${data.comment.user.login} left a comment. ${data.comment.html_url}`);
AKP48.sendAlert(out);
});
}
}
getDefaultConfig() {
return {
port: 4269,
path: '/github/callback',
secret: '',
repository: 'AKP48Squared',
branch: 'master',
autoUpdate: false,
events: {
push: true,
commit_comment: true,
pull_request: true,
issues: true,
issue_comment: true,
gollum: true,
fork: true,
watch: true,
repository: true
},
enabled: true
};
}
}
GitHubListener.prototype.compare = function (original, other) {
if (other === '*' || original === other) { // Checking here saves pain and effort
return true;
} else if (other.startsWith('!') || other.startsWith('-')) { // We should update to all except the specified
// Should we do a compare?
//return !compare(original, other.substring(1));
return original !== other.substring(1);
}
var star = other.indexOf('*'), star2 = other.lastIndexOf('*');
if (star !== -1) {
if (star2 > star) {
return original.contains(other.substring(star + 1, star2 - 1));
}
if (star === 0) {
return original.endsWith(other.substring(star + 1));
} else {
return original.startsWith(other.substring(star - 1));
}
}
return false;
};
GitHubListener.prototype.shouldUpdate = function (branch) {
var updateBranch = this.config.branch;
if (Array.isArray(updateBranch)) { // We update only if it is listed
for (var x = 0; x < updateBranch.length; x++) {
var _branch = updateBranch[x];
if (this.compare(branch, _branch)) {
return true;
}
}
return false;
}
return this.compare(branch, updateBranch);
};
GitHubListener.prototype.handle = function (branch, data) {
var self = this;
global.logger.info(`${this.name}: Handling Webhook for branch ${branch}.`);
if (!shell.which('git') || !this._isRepo) {
global.logger.debug(`${this.name}: Not a git repo; stopping update.`);
return;
}
var changing_branch = branch !== this.getBranch();
var update = this.config.autoUpdate && (data.commits.length !== 0 || changing_branch);
global.logger.silly(`${this.name}: Is changing branch? ${changing_branch}.`);
global.logger.silly(`${this.name}: Is updating? ${update}.`);
if (!update) {
global.logger.debug(`${this.name}: Nothing to update; stopping update.`);
return;
}
var shutdown = changing_branch;
var npm = changing_branch;
var hot_files = ['app.js'];
if (!shutdown) {
for (var commit in data.commits) {
if (data.commits.hasOwnProperty(commit)) {
var com = data.commits[commit];
for (var file in com.modified) {
if (com.modified.hasOwnProperty(file)) {
if(hot_files.indexOf(com.modified[file]) !== -1) {
shutdown = true;
}
if(com.modified[file].includes('package.json')) {
npm = true;
}
}
}
for (var f in com.created) {
if (com.modified.hasOwnProperty(f)) {
if(com.modified[f].includes('package.json')) {
npm = true;
}
}
}
}
}
}
global.logger.debug(`${this.name}: Updating to branch "${branch}".`);
// Fetch, Checkout
if (!this.checkout(branch)) {
return;
}
if(npm) {
global.logger.debug(`${this.name}: Executing npm install.`);
shell.cd(require('app-root-path').path);
shell.exec('npm install');
}
var pluginPath = path.resolve(require('app-root-path').path, 'plugins/*/plugin.json');
glob(pluginPath, function(err, files) {
if(err) {global.logger.error(`${this.name}: Glob error: "${err}".`);return;}
new Promise(function(resolve) {
//two separate loops because shell is doing something weird if I do it all as one loop.
//first loop resolves paths to full absolute paths.
for (var i = 0; i < files.length; i++) {
files[i] = path.dirname(path.resolve(files[i]));
}
var proms = [];
//second loop CDs into each directory and runs npm install.
for (var j = 0; j < files.length; j++) {
shell.cd(files[j]);
proms.push(new Promise(function(resolve){ // jshint ignore:line
if(npm) {
global.logger.verbose(`${self.name}: Executing npm install for ${files[j]}.`);
shell.exec('npm install', function(){
resolve();
});
} else {
resolve();
}
}));
}
Promise.all(proms).then(function(){
resolve(); //resolve promise after all npm installs are finished.
});
}).then(function(){
if (shutdown) {
self._AKP48.shutdown(`I'm updating! :3`);
} else {
self._AKP48.reload();
}
});
});
};
GitHubListener.prototype.fetch = function () {
if(shell.exec('git fetch').code) {
global.logger.error(`${this.name}: Attempted git fetch failed!`);
return;
} else {
global.logger.verbose(`${this.name}: Fetched latest code from git.`);
}
return true;
};
GitHubListener.prototype.getCommit = function () {
return getRepoInfo().sha;
};
GitHubListener.prototype.getBranch = function () {
return getRepoInfo().branch;
};
GitHubListener.prototype.getTag = function () {
return getRepoInfo().tag;
};
GitHubListener.prototype.checkout = function (branch) {
if (!branch || !this.fetch()) {
return;
}
if (this.getBranch() !== branch) {
if (shell.exec(`git checkout -q ${branch}`).code) {
global.logger.error(`${this.name}: Attempted git reset failed!`);
return;
} else {
global.logger.verbose(`${this.name}: Successfully checked out branch "${branch}".`);
}
}
if ((this.getBranch() || this.getTag()) && shell.exec(`git reset -q origin/${branch} --hard`).code) {
global.logger.error(`${this.name}: Attempted git reset failed!`);
return;
} else {
global.logger.verbose(`${this.name}: Successfully reset to branch "${branch}".`);
}
return true;
};
GitHubListener.prototype.shouldSendAlert = function (hookType) {
if(!this.config.events || !this.config.events.hasOwnProperty('commit_comment')) { // legacy config didn't have events object.
this.config.events = this.config.events || defaultConfig.events;
if (!this.config.events.hasOwnProperty('commit_comment')) { this.config.events.commit_comment = true; }
this._AKP48.saveConfig(this.config, 'GithubListener');
return true;
}
if(this.config.events[hookType]) {return true;} // hookType is enabled in config.
return false;
};
//called when we are told we're unloading.
GitHubListener.prototype.unload = function () {
var self = this;
return new Promise(function (resolve) {
if(self._listener) {
self._listener.stop();
delete self._listener;
}
resolve();
});
};
module.exports = GitHubListener;