-
Notifications
You must be signed in to change notification settings - Fork 326
/
Copy pathrepoManager.js
211 lines (180 loc) · 5.53 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
'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 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 clientId = null;
var clientKey = null;
Strategy.findOne({ name: 'github' }, function (aErr, aStrat) {
// WARNING: No err handling
clientId = aStrat.id;
clientKey = aStrat.key;
});
// Requests a GitHub url and returns the chunks as buffers
function fetchRaw(aHost, aPath, aCallback) {
var options = {
hostname: aHost,
port: 443,
path: aPath,
method: 'GET',
headers: {
'User-Agent': 'Node.js'
}
};
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) {
aPath += '?client_id=' + clientId + '&client_secret=' + clientKey;
fetchRaw('api.github.com', aPath, function (aBufs) {
aCallback(JSON.parse(Buffer.concat(aBufs).toString()));
});
}
// 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 on GitHub
RepoManager.prototype.loadScripts = function (aUpdate, aCallback) {
var scriptStorage = require('../controllers/scriptStorage');
var arrayOfRepos = this.makeRepoArray();
var that = this;
// TODO: remove usage of makeRepoArray since it causes redundant looping
arrayOfRepos.forEach(function (aRepo) {
async.each(aRepo.scripts, function (aScript, aInnerCallback) {
var url = '/' + encodeURI(aRepo.user) + '/' + encodeURI(aRepo.repo)
+ '/master' + aScript.path;
fetchRaw('raw.githubusercontent.com', url, function (aBufs) {
scriptStorage.getMeta(aBufs, function (aMeta) {
if (aMeta) {
scriptStorage.storeScript(that.user, aMeta, Buffer.concat(aBufs), aUpdate,
aInnerCallback);
}
});
});
}, 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);
};