-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmetahub.js
440 lines (361 loc) · 10.5 KB
/
metahub.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// cached wrapper around Github
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var qequire = require('qequire');
var GitHubApi = require('github');
var _ = require('lodash');
var Q = require('q');
Q.longStackSupport = true;
var makeCache = require('./lib/cache');
var makeServer = require('./lib/server');
// recursively remove *_url props
var stripUrl = require('./lib/strip-url');
var Metahub = function (config) {
this.config = config || {};
this.repo = null;
this.issues = null;
this.rest = qequire.quire(
this.config.gitHubApi ||
new GitHubApi({
version: '3.0.0'
}));
this.cache = makeCache();
this.server = this.config.server || makeServer();
this.server.on('hook', this._merge.bind(this));
this.requestQueue = [];
this.resolvingQueue = false;
EventEmitter.call(this);
};
util.inherits(Metahub, EventEmitter);
Metahub.prototype._enqueRequest = function (fn, args) {
var deferred = Q.defer();
this.requestQueue.push({
fn: fn,
args: args,
deferred: deferred
});
if (!this.resolvingQueue) {
this.resolvingQueue = true;
this._invokeRequest();
}
return deferred.promise;
};
Metahub.prototype._invokeRequest = function () {
var request = this.requestQueue[0];
return request.fn.apply(this, request.args).
then(function (val) {
request.deferred.resolve(val);
return val;
}).
then(this._resolveRequest.bind(this), function (err) {
if (err.toString().indexOf('API rate limit exceeded')) {
this.log('API rate limit exceeded');
setTimeout(function () {
this.log('resuming scraping')
this._invokeRequest();
}.bind(this), 1000 * 60 * 60);
}
}.bind(this));
};
Metahub.prototype._resolveRequest = function () {
this.requestQueue.shift();
if (this.requestQueue.length) {
this._invokeRequest();
} else {
this.resolvingQueue = false;
}
};
// grab all data from Github API and cache it
Metahub.prototype._populate = function () {
this.log('Populating cache');
return this._populateRepo().
then(function (repo) {
if (this.cache.exists('repo')) {
this.repo = this.cache.get('repo');
if (Date.parse(repo.updated_at) > Date.parse(this.repo.updated_at)) {
this.log('Cache is stale');
this.repo = repo;
this.cache.set('repo', repo);
} else {
this.issues = this.cache.get('issues');
return;
}
} else {
this.log('No repo cache');
this.cache.set('repo', repo);
this.repo = repo;
}
return this._populateAndCacheIssues(repo).
then(function () {
this.log('Done caching repo issues');
}.bind(this));
}.bind(this));
};
Metahub.prototype._populateIssues = require('./lib/scrape-issues');
Metahub.prototype._populateAndCacheIssues = function (repo) {
this.log('Scraping issue data from Github API');
return this._populateIssues(repo).
then(function (issues) {
this.issues = issues;
this._cacheIssues();
this.emit('cacheBuilt');
}.bind(this));
};
Metahub.prototype._populateRepo = function () {
return this.rest.repos.get(this.config.msg).
then(stripUrl);
};
Metahub.prototype.start = function () {
this._config();
return this._populate().
then(function () {
this.serverInstance = this.server.listen(this.config.hook.port);
}.bind(this));
};
Metahub.prototype.stop = function () {
if (this.serverInstance) {
this.serverInstance.close();
}
};
Metahub.prototype._config = function () {
this.config.msg = this.config.msg || {};
this.config.msg.user = this.config.msg.user || this.config.target.user;
this.config.msg.repo = this.config.msg.repo || this.config.target.repo;
this.rest.authenticate({
type: 'basic',
username: this.config.login.username,
password: this.config.login.password
});
};
Metahub.prototype.clearCache = function () {
if (this.cache.exists('repo')) {
this.cache.clear('repo');
}
};
Metahub.prototype.getHooks = function () {
return this.rest.repos.getHooks(this.config.msg).
then(stripUrl);
};
Metahub.prototype.getCommits = function (number) {
var msg = _.defaults({
number: number
}, this.config.msg);
return this.rest.pullRequests.getCommits(msg).
then(stripUrl);
};
Metahub.prototype.getIssue = function (number) {
var msg = _.defaults({
number: number
}, this.config.msg);
return this.rest.issues.getRepoIssue(msg).
then(stripUrl);
};
Metahub.prototype.getContributors = function () {
return this.rest.repos.getContributors(this.config.msg).
then(stripUrl);
};
Metahub.prototype.createComment = function (number, body) {
var msg = _.defaults({
number: number,
body: body
}, this.config.msg);
return this.rest.issues.createComment(msg).
then(stripUrl);
};
Metahub.prototype.createHook = function () {
var msg = _.defaults({
name: 'web',
active: true,
events: [
'pull_request',
'issues',
'issue_comment'
],
config: {
url: this.config.hook.url,
content_type: 'json'
}
}, this.config.msg);
return this.rest.repos.createHook(msg);
};
Metahub.prototype.updateHook = function (id, args) {
if (!id) {
if (!this.cache.exists('hook')) {
throw new Error('No id given');
}
id = this.cache.get('hook');
}
var msg = _.defaults(args || {}, this.config.msg, {
id: id,
name: 'web',
events: [
'pull_request',
'issues',
'issue_comment'
],
config: {
url: this.config.hook.url,
content_type: 'json'
}
});
return this.rest.repos.updateHook(msg);
};
Metahub.prototype.enableHook = function (id) {
return this.updateHook(id, {
active: true
});
};
Metahub.prototype.disableHook = function (id) {
return this.updateHook(id, {
active: false
});
};
Metahub.prototype.deleteHook = function (id) {
if (!id) {
if (!this.cache.exists('hook')) {
throw new Error('No id given');
}
id = this.cache.get('hook');
}
var msg = _.defaults({
id: id
}, this.config.msg);
return this.rest.repos.deleteHook(msg);
};
Metahub.prototype._tryToMerge = function (data) {
return this._merge(data);
};
// merge a change event
Metahub.prototype._merge = function (data) {
this.log('GitHub hook pushed');
// i have no idea what i'm doing WRT the GitHub API
try {
if (data.payload && typeof data.payload === 'string') {
data = JSON.parse(data.payload);
}
data = stripUrl(data);
} catch (e) {
this.log('Bad message:\n' + indent(
JSON.stringify(data, null, 2) +
'\n=======\n' +
e.stack
));
return Q.reject();
}
var hook = hookName(data);
var runExternalHooks = function () {
this.log('Emitting ' + hook + ' event', data);
this.emit(hook, data);
return data;
}.bind(this);
if (this['_' + hook]) {
this.log('Running internal ' + hook + ' book-keeping for #' + issueNumber(data));
return Q.try(this['_' + hook].bind(this), data).
then(runExternalHooks, function (err) {
this.log('Error from internal ' + hook + ' book-keeping', data, err);
}.bind(this));
} else {
return Q.resolve(runExternalHooks());
}
};
// there methods are invoked by merge
// they are applies before event handlers
Metahub.prototype._issueCommentCreated =
Metahub.prototype._pullRequestCommentCreated = function (data) {
var issue = issueish(data),
number = issue.number,
comment = data.comment;
if (!this.issues[issue.number]) {
this.log('Tried to add a comment to a non-existent issue\n' +
indent('Recovering by adding issue to cache'), data);
this.issues[number] = _.merge({}, this.issues[number], issue);
}
var issue = this.issues[issue.number];
var comments = issue.comments = issue.comments || {};
if (comments[comment.id] && newerThan(comments[comment.id].updated_at, comment.updated_at)) {
this.log('Tried to update cache with data older than dates from the cache', data);
return Q.resolve();
}
comments[comment.id] = _.merge({}, comments[comment.id], comment);
this._cacheIssues();
return Q.resolve();
};
Metahub.prototype._issueClosed =
Metahub.prototype._issueReopened =
Metahub.prototype._issueMerged =
Metahub.prototype._issueReferenced =
Metahub.prototype._issueAssigned =
Metahub.prototype._issueOpened = function (data) {
var number = data.issue.number;
if (this.issues[number] && newerThan(this.issues[number].updated_at, data.updated_at)) {
this.log('Tried to update cache with data older than dates from the cache', data);
return Q.resolve();
}
this.issues[number] = _.merge({}, this.issues[number], data.issue);
this.issues[number]._updated = Date.now();
this._cacheIssues();
return Q.resolve();
};
Metahub.prototype._cacheIssues = function () {
this.cache.set('issues', this.issues);
};
function newerThan (a, b) {
return timestamp(a) > timestamp(b);
}
function timestamp (str) {
return +new Date(str);
}
function issueNumber (data) {
return (data.pull_request || data.issue).number;
}
function hookName (data) {
var action = data.action;
var entity = interestingEntity(data);
return entity +
action[0].toUpperCase() +
action.substr(1);
}
function interestingEntity (data) {
return data.comment ?
(data.issue ? 'issueComment' : 'pullRequestComment') :
data.pull_request ? 'pullRequest' :
data.issue ? 'issue' : '';
}
function indent (str, amount) {
amount = amount || ' ';
str = (str instanceof Array) ? str : str.split('\n');
return str.map(function (line) {
return amount + line;
}).join('\n');
}
function issueish (data) {
return data.pull_request || data.issue;
}
function logDataSummary (data) {
return logTitle(data) + '\n' +
indent(
'issue: ' + summerizeBody(issueish(data)) +
(data.comment ? 'comment: ' + summerizeBody(data.comment) : '')
);
}
function logTitle (data) {
return data.repository.full_name + '/#' + issueNumber(data) + ' - ' + issueish(data).title;
}
function summerizeBody (data) {
return data.body ?
('"' + data.body.substr(0, 80) + (data.body.length > 80 ? '…' : '') +
'" -' + data.user.login + '\n') : '';
}
Metahub.prototype.log = function (msg, data, exception) {
if (data) {
msg += ' for:\n' + indent(logDataSummary(data));
}
if (exception) {
msg += indent((data ? '\n=======\n' : '') + (exception.stack || exception));
}
this.emit('log', msg);
};
module.exports = function (config) {
return new Metahub(config);
};
module.exports.Metahub = Metahub;