-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathrepoManager.js
382 lines (330 loc) · 11.2 KB
/
repoManager.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
'use strict';
// Define some pseudo module globals
var isPro = require('../libs/debug').isPro;
var isDev = require('../libs/debug').isDev;
var isDbg = require('../libs/debug').isDbg;
var uaOUJS = require('../libs/debug').uaOUJS;
var statusError = require('../libs/debug').statusError;
//--- Dependency inclusions
var util = require('util');
var colors = require('ansi-colors');
//--- Model inclusions
var Sync = require('../models/sync').Sync;
//--- Controller inclusions
var scriptStorage = require('../controllers/scriptStorage');
//
var https = require('https');
var async = require('async');
var _ = require('underscore');
var Strategy = require('../models/strategy').Strategy;
var nil = require('./helpers').nil;
var github = require('../libs/githubClient');
var settings = require('../models/settings.json');
var clientId = null;
var clientKey = null;
Strategy.findOne({ name: 'github' }, function (aErr, aStrat) {
if (aErr) {
console.error( aErr.message );
process.exit(1);
return;
}
if (!aStrat) {
console.warn( colors.red([
'Default GitHub Strategy document not found in DB.',
'Lower rate limit will be available.'
].join('\n')));
} else {
clientId = aStrat.id;
clientKey = aStrat.key;
}
});
// Requests a GitHub url and returns the chunks as buffers
function fetchRaw(aHost, aPath, aCallback, aOptions) {
var options = {
hostname: aHost,
port: 443,
path: aPath,
method: 'GET',
headers: {
'User-Agent': uaOUJS + (process.env.UA_SECRET ? ' ' + process.env.UA_SECRET : '')
}
};
if (aOptions) {
// Ideally do a deep merge of aOptions -> options
// But for now, we just need the headers
if (aOptions.headers) {
Object.assign(options.headers, aOptions.headers);
}
}
var req = https.request(options,
function (aRes) {
if (isDbg) {
console.log(aRes);
}
var bufs = [];
if (aRes.statusCode !== 200) {
console.warn(aRes.statusCode);
return aCallback([Buffer.from('')]);
}
else {
aRes.on('data', function (aData) {
bufs.push(aData);
});
aRes.on('end', function () {
aCallback(bufs);
});
}
});
req.end();
}
// Use for call the GitHub JSON api
// Returns the JSON parsed object
function fetchJSON(aPath, aCallback) {
var encodedAuth = null;
var opts = null;
// The old authentication method, which GitHub deprecated
//aPath += '?client_id=' + clientId + '&client_secret=' + clientKey;
// We must now use OAuth Basic (user+key) or Bearer (token)
if (clientId && clientKey) {
encodedAuth = Buffer.from(`${clientId}:${clientKey}`).toString('base64');
opts = {
headers: {
Authorization: `Basic ${encodedAuth}`
}
};
}
fetchRaw('api.github.com', aPath, function (aBufs) {
aCallback(JSON.parse(Buffer.concat(aBufs).toString()));
}, opts);
}
// This manages actions on the repos of a user
function RepoManager(aUserId, aUser, aRepos) {
this.userId = aUserId;
this.user = aUser;
this.repos = aRepos || nil();
}
// Fetches the information about repos that contain user scripts
RepoManager.prototype.fetchRecentRepos = function (aCallback) {
var repoList = [];
var that = this;
async.waterfall([
function (aCallback) {
github.repos.getFromUser({
user: encodeURIComponent(that.userId),
sort: 'updated',
order: 'desc',
per_page: 3,
}, aCallback);
},
function (aGithubRepoList, aCallback) {
// Don't search through forks
// to speedup this request.
// aGithubRepoList = _.where(aGithubRepoList, {fork: false});
_.map(aGithubRepoList, function (aGithubRepo) {
repoList.push(new Repo(that, aGithubRepo.owner.login, aGithubRepo.name));
});
async.each(repoList, function (aRepo, aCallback) {
aRepo.fetchUserScripts(function () {
aCallback(null);
});
}, aCallback);
},
], aCallback);
};
// Import scripts to be sync'd into Sync model
RepoManager.prototype.loadSyncs = function (aUpdate, aCallback) {
var arrayOfRepos = this.makeRepoArray();
var that = this;
// TODO: Alter usage of makeRepoArray since it causes redundant looping
arrayOfRepos.forEach(function (aRepo) {
async.each(aRepo.scripts, function (aScript, aInnerCallback) {
var hostname = 'raw.githubusercontent.com';
var uri = '/' + aRepo.user + '/' + aRepo.repo
+ '/master' + aScript.path;
Sync.findOne(
{ _authorId: that.user.id, id: aUpdate, target: 'https://' + hostname + uri },
function (aErr, aSync) {
if (aErr) {
console.error('Error retrieving sync status');
aInnerCallback(aErr, aSync);
return;
}
if (aSync) {
// TODO: Maybe update the updated, response, and message to reflect redelivery?
aInnerCallback(null, aSync);
} else {
var sync = new Sync({
strat: 'github',
id: aUpdate,
target: 'https://' + hostname + uri,
response: 202,
message: 'Accepted',
created: new Date(),
_authorId: that.user.id
});
sync.save(function (aErr, aSync) {
if (aErr || !aSync) {
console.error('Unable to create Sync record');
aInnerCallback(aErr, aSync);
return;
}
aInnerCallback(null, aSync);
});
}
});
}, aCallback);
});
};
// Import scripts from GitHub
RepoManager.prototype.loadScripts = function (aUpdate, aCallback) {
var arrayOfRepos = this.makeRepoArray();
var that = this;
// TODO: Alter usage of makeRepoArray since it causes redundant looping
arrayOfRepos.forEach(function (aRepo) {
async.each(aRepo.scripts, function (aScript, aInnerCallback) {
var hostname = 'raw.githubusercontent.com';
var uri = '/' + aRepo.user + '/' + aRepo.repo
+ '/master' + aScript.path;
var url = '/' + encodeURI(aRepo.user) + '/' + encodeURI(aRepo.repo)
+ '/master' + aScript.path;
fetchRaw(hostname, url, function (aBufs) {
var msg = null;
var thisBuf = Buffer.concat(aBufs);
if (thisBuf.byteLength <= settings.maximum_upload_script_size) {
scriptStorage.getMeta(aBufs, function (aMeta) {
if (aMeta) {
scriptStorage.storeScript(that.user, aMeta, thisBuf, !!aUpdate,
function (aErr, aScript) {
if (aErr || !aScript) {
msg = (aErr instanceof statusError ? aErr.status.message : aErr.message)
|| 'Unknown error with storing script';
Sync.findOneAndUpdate(
{ _authorId: that.user.id, id: aUpdate, target: 'https://' + hostname + uri }, {
response: (aErr instanceof statusError ? aErr.status.code : aErr.code),
message: msg,
updated: new Date()
},
function (aErr, aSync) {
if (aErr || !aSync) {
console.error('Error changing sync status with ' + msg);
return;
}
});
} else {
msg = 'OK';
Sync.findOneAndUpdate(
{ _authorId: that.user.id, id: aUpdate, target: 'https://' + hostname + uri },
{ response: 200, message: msg, updated: new Date()},
function (aErr, aSync) {
if (aErr || !aSync) {
console.error('Error changing sync status with ' + msg);
return;
}
});
}
aInnerCallback(aErr, aScript);
});
} else {
msg = 'Metadata block(s) missing.'
Sync.findOneAndUpdate(
{ _authorId: that.user.id, id: aUpdate, target: 'https://' + hostname + uri },
{ response: 400, message: msg, updated: new Date()},
function (aErr, aSync) {
if (aErr || !aSync) {
console.error('Error changing sync status with ' + msg);
return;
}
});
aInnerCallback(new statusError({
message: msg,
code: 400
}, null));
}
});
} else {
msg = util.format('File size is larger than maximum (%s bytes).',
settings.maximum_upload_script_size);
Sync.findOneAndUpdate(
{ _authorId: that.user.id, id: aUpdate, target: 'https://' + hostname + uri },
{ response: 400, message: msg},
function (aErr, aSync) {
if (aErr || !aSync) {
console.error('Error changing sync status with ' + msg);
return;
}
});
aInnerCallback(new statusError({
message: msg,
code: 400
}, null));
}
});
}, aCallback);
});
};
// Create the Mustache object to display repos with their user scrips
RepoManager.prototype.makeRepoArray = function () {
var retOptions = [];
var repos = this.repos;
var username = this.user.ghUsername;
var reponame = null;
var scripts = null;
var scriptname = null;
var option = null;
for (reponame in repos) {
option = { repo: reponame, user: username };
option.scripts = [];
scripts = repos[reponame];
for (scriptname in scripts) {
option.scripts.push({ name: scriptname, path: scripts[scriptname] });
}
retOptions.push(option);
}
return retOptions;
};
// Manages a single repo
function Repo(aManager, aUsername, aReponame) {
this.manager = aManager;
this.user = aUsername;
this.repo = aReponame;
}
// Use recursive requests to locate all user scripts in a repo
Repo.prototype.fetchUserScripts = function (aCallback) {
this.getTree('HEAD', '', aCallback);
};
// Looks for user script in the current directory
// and initiates searches on subdirectories
Repo.prototype.parseTree = function (aTree, aPath, aDone) {
var trees = [];
var that = this;
var repos = this.manager.repos;
aTree.forEach(function (object) {
if (object.type === 'tree') {
trees.push({
sha: object.sha, path: aPath + '/'
+ encodeURI(object.path)
});
} else if (object.path.substr(-8) === '.user.js') {
if (!repos[that.repo]) { repos[that.repo] = nil(); }
repos[that.repo][object.path] = aPath + '/' + encodeURI(object.path);
}
});
async.each(trees, function (aTree, aCallback) {
that.getTree(aTree.sha, aTree.path, aCallback);
}, function () {
aDone();
});
};
// Gets information about a directory
Repo.prototype.getTree = function (aSha, aPath, aCallback) {
var that = this;
fetchJSON('/repos/' + encodeURI(this.user) + '/' + encodeURI(this.repo)
+ '/git/trees/' + aSha,
function (aJson) {
that.parseTree(aJson.tree, aPath, aCallback);
}
);
};
exports.getManager = function (aUserId, aUser, aRepos) {
return new RepoManager(aUserId, aUser, aRepos);
};