-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathgithub.js
247 lines (216 loc) · 7.26 KB
/
github.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
// Dependencies
var Config = require("./config")
, Request = require("request")
, Logger = require("bug-killer")
, Repo = require("gry")
, Async = require("async")
, Fs = require("fs")
, SameTimeLimit = require("same-time-limit")
;
// Logger configuration
Logger.config.progress = {
color: "#3498db"
, text: " > "
};
/**
* makeApiRequest
* Run API requests.
*
* @name makeApiRequest
* @function
* @param {String} api The relative API url.
* @param {Function} callback The callback function.
* @return {undefined}
*/
function makeApiRequest(api, callback) {
Request({
url: "https://api.github.com/" + api
, auth: Config.github
, json: true
, headers: {
"user-agent": "Repo Downloader"
}
}, function (err, res, body) {
err = err || body.error || body.message;
if (err) { return callback(err); }
callback(null, body);
});
}
/**
* getOrgs
* Gets the organizations where the authenticated user is member.
*
* @name getOrgs
* @function
* @param {Function} callback The callback function.
* @return {undefined}
*/
function getOrgs(callback) {
makeApiRequest("user/orgs", callback);
}
/**
* getPRRepos
* Gets the repositories where the `user` created pull requests.
*
* @name getPRRepos
* @function
* @param {String} user The user login value.
* @param {Array} orgs The organizations where the user belongs to.
* @param {Function} callback The callback function.
* @return {undefined}
*/
function getPRRepos(user, orgs, callback) {
var api = "search/issues?q=author:" + user + "%20is:pr"
, page = 0
, allRepos = []
, ignoreAccounts = orgs.map(function (c) { return c.login; }).concat([Config.github.username])
, match = null
;
function seq() {
Logger.log("Page: " + (++page), "progress");
makeApiRequest(api + "&per_page=100&page=" + page, function (err, res) {
if (err) { return callback(err); }
allRepos = allRepos.concat(res.items.filter(function (c) {
if (!c.full_name || !c.ssh_url || !c.owner) {
match = c.html_url.match(/github\.com\/(.*)\/(.*)\/pull/);
if (!match || match.length !== 3) {
Logger.log("Failed to get repository data for: " + c.html_url + " Incident must be reported.", "error");
return false;
}
c.owner = {
login: match[1]
};
c.full_name = c.owner.login + "/" + match[2];
c.ssh_url = "[email protected]:" + c.full_name + ".git";
}
return ignoreAccounts.indexOf(c.owner.login) === -1;
}));
if (!res.items.length) {
return callback(null, allRepos);
}
seq();
});
}
seq();
}
/**
* getAllRepos
* Gets all repositories from the provided `user`.
*
* @name getAllRepos
* @function
* @param {String} user The user login value.
* @param {Array} orgs The organizations where the user belongs to.
* @param {Boolean} isOrg A flag if the `user` is an organization or not.
* @param {Function} callback The callback function.
* @return {undefined}
*/
function getAllRepos(user, orgs, isOrg, callback) {
if (typeof isOrg === "function") {
callback = isOrg;
isOrg = false;
}
var api = "users/" + user + "/repos"
, page = 0
, allRepos = []
;
if (isOrg) {
api = "orgs/" + user + "/repos";
}
function seq() {
Logger.log("Page: " + (++page), "progress");
makeApiRequest(api + "?per_page=100&page=" + page, function (err, res) {
if (err) { return callback(err); }
allRepos = allRepos.concat(res);
if (!res.length) {
Logger.log("Getting the repositories where " + user + " created pull requests.", "info");
return getPRRepos(user, orgs, function (err, prRepos) {
if (err) {
Logger.log(err, "warn");
}
allRepos = allRepos.concat(prRepos);
return callback(null, allRepos);
});
}
seq();
});
}
seq();
}
/**
* downloadRepos
* Download repositories the provided repositories.
*
* @name downloadRepos
* @function
* @param {Array} repos An array with the repositories to download.
* @param {Function} callback The callback function.
* @return {undefined}
*/
function downloadRepos(repos, callback) {
var funcs = []
, complete = 0
, notDownloaded = []
;
repos.forEach(function (c) {
if (!c) { return; }
funcs.push(function (callback) {
var repo = new Repo("./downloads")
, path = "github/" + c.full_name
;
if (Fs.existsSync(__dirname + "/downloads/" + path)) {
Logger.log("Repository already downloaded: " + c.full_name, "warn");
return callback();
}
Logger.log("Repository: " + c.full_name, "progress");
repo.exec("clone " + c.ssh_url + " " + path, function (err) {
if (err) {
Logger.log(err);
notDownloaded.push(c);
return callback();
}
Logger.log("Downloaded " + c.full_name + " (" + (++complete) + "/" + repos.length + ")", "progress");
callback();
});
});
});
SameTimeLimit(funcs, 4, function (err, data) {
if (notDownloaded.length) {
Logger.log(notDownloaded.length + " repos failed to download. Trying again.", "warn");
return downloadRepos(notDownloaded, callback);
}
callback();
});
}
// Start the magic
Logger.log("Getting the organizations you belong to.", "info");
getOrgs(function (err, orgs) {
if (err) { return Logger.log(err, "error"); }
// Download user reposiotires
Logger.log("Getting all your repositories.", "info");
getAllRepos(Config.github.username, orgs, function (err, myRepos) {
if (err) { return Logger.log(err, "error"); }
// Download user repositories
Logger.log("Downloading all your repositories.", "info");
downloadRepos(myRepos, function (err) {
if (err) { return Logger.log(err, "error"); }
Logger.log("Downloaded all user repos.", "info");
// Download the organization repositories
var downloadOrgRepos = [];
orgs.forEach(function (c) {
downloadOrgRepos.push(function (callback) {
Logger.log("Getting " + c.login + "'s repositories.", "info");
getAllRepos(c.login, orgs, true, function (err, repos) {
if (err) { return callback(err); }
Logger.log("Downloading " + c.login + "'s repositories.", "info");
downloadRepos(repos, callback);
});
});
});
Async.series(downloadOrgRepos, function (err) {
if (err) { return Logger.log(err, "error"); }
Logger.log("Done.", "info");
});
});
});
});