-
Notifications
You must be signed in to change notification settings - Fork 0
/
list-github-forks.js
executable file
·214 lines (182 loc) · 6.97 KB
/
list-github-forks.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
"use strict";
var fs = require("fs");
var path = require("path");
var url = require("url");
var http = require("http");
var https = require("https");
var util = require("util");
var apiurl_branches = "https://api.github.com/repos/%s/%s/branches";
var apiurl_forks = "https://api.github.com/repos/%s/%s/forks";
var target_username = process.argv[2];
var target_repo = process.argv[3];
var maximum_forks = process.argv[4] || 32;
if (!target_username || !target_repo) {
console.log("Usage: " + process.argv[0] + " user repo [maxforks]");
console.log("Avoid API limits by placing a Personal Access Token in ./.ghtoken with the");
console.log("format <username>:<token>");
console.log("Create one here: https://github.com/settings/tokens");
if (require.main === module) {
process.exit(1);
}
}
var api_token;
var api_token_path;
if (fs.existsSync("./.ghtoken")) {
api_token_path = "./.ghtoken";
} else if (fs.existsSync("~/.ghtoken")) {
api_token_path = "~/.ghtoken";
} else if (fs.existsSync(path.resolve(__dirname, ".ghtoken"))){
api_token_path = fs.existsSync(path.resolve(__dirname, ".ghtoken"));
}
if (api_token_path) {
api_token = fs.readFileSync(api_token_path, {encoding: "utf8"});
api_token = api_token.replace(/[\n\t\r]/g, "");
}
var origin_branches = [];
printInterestingForks();
function printInterestingForks(){
//Get origin branches
getBranches(target_username, target_repo, function(branches){
origin_branches = branches;
//console.log("origin branches: "+JSON.stringify(origin_branches));
getForkBranches(target_username, target_repo, function(forks){
forks = forks.sort(function(a, b){
return new Date(b.pushed_at) - new Date(a.pushed_at);
});
forks = forks.slice(0, maximum_forks);
iterateForks(forks);
});
});
}
function iterateForks(forks){
if (forks.length > 0) {
var fork = forks[0];
console.info(fork.html_url);
process.stdout.write("Interesting branches: ");
getBranches(fork.owner.login, fork.name, function(branches){
printInterestingBranches(branches, origin_branches, function(){
console.info("");
iterateForks(forks.slice(1));
});
});
}
}
function getForkBranches(username, repo, callback) { // (and sort them by last push date, descending)
var target_url = util.format(apiurl_forks, username, repo);
followJSONpages(target_url, callback);
}
function printInterestingBranches(branches, exclude_branches, callback) {
//This will break if github stops returning branch names in alaphalabeletical order
for (var i_origin = 0, i_fork = 0; i_fork < branches.length; i_fork++) {
while (exclude_branches[i_origin] < branches[i_fork]) {
i_origin++;
}
if (exclude_branches[i_origin] > branches[i_fork] || i_origin >= exclude_branches.length) {
process.stdout.write(branches[i_fork] + " ");
}
}
console.info("");
if (callback) {
callback();
}
}
function getBranches(repo_owner, repo_name, callback) {
var target_url = util.format(apiurl_branches, repo_owner, repo_name);
followJSONpages(target_url, function(data){
var branches = [];
for (var branch in data) {
branches.push(data[branch].name);
}
callback(branches);
});
}
function followJSONpages(target_url, callback, json_data) {
json_data = json_data ? json_data : [];
var parsed_url = url.parse(target_url);
var options = {
hostname: parsed_url.hostname,
port: parsed_url.port,
path: parsed_url.path,
headers: {"User-Agent": "list-github-forks"}
};
if (api_token && api_token.length > 0) {
options.auth = api_token;
}
//console.log("options: "+JSON.stringify(options));
var http_maybe_s = parsed_url.protocol === "https:" ? https : http;
var req = http_maybe_s.request(options, function(res){
var body = "";
res.on("data", function(chunk){
//It'd be nice to have a stream parser here
body += chunk;
});
if(res.statusCode === 200) {
res.setEncoding("utf8");
res.on("end", function(){
json_data = json_data.concat(JSON.parse(body));
//console.log("headers: "+JSON.stringify(res.headers));
if (res.headers.link) {
var links = processLinkHeader(res.headers.link);
if (links.next) {
//console.log("follow " + links.next);
setTimeout(function(){followJSONpages(links.next, callback, json_data);}, 50);
} else {
//console.log("callback!");
callback(json_data);
}
} else {
//console.log("callback!");
callback(json_data);
}
});
} else {
res.on("end", function(){
throw "HTTP error " + res.statusCode + " " + res.statusMessage + " while fetching " + target_url + ": " + body;
});
}
});
req.end();
}
function processLinkHeader(value){
var re_csv = /(<[^<>]+>[^,]+)(,|$)/g;
var re_linkparts = /<([^<>]+)>;\s*rel="([^"]+)"/;
var links = {};
var link;
while ((link = re_csv.exec(value)) !== null) {
var link_parts = link[1].match(re_linkparts);
if (link_parts.length >= 3) {
links[link_parts[2]] = link_parts[1];
} else {
console.log("Didn't understand a Link header: " + link);
}
}
return links;
}
//ad-hoc tests
function test_printInterestingBranches() {
printInterestingBranches(["abranch", "boring", "feature", "master", "optimize", "whatever", "zinteresting"],
["boring", "master", "whatever"]);
}
function test_processLinkHeaders() {
var assert = require("assert");
var links = processLinkHeader('<https://example.com/wh,at?page=2>; rel="next", <https://example.com/?page=64>; rel="last"');
assert.deepEqual(links, {next:"https://example.com/wh,at?page=2", last:"https://example.com/?page=64"});
}
function test_loadJSON(){
var assert = require("assert");
followJSONpages("http://www.zone42.ca/~philippe/cake.json", function(data){
assert.deepEqual(data,[{"name":"Toroid cake"},{"name":"rawberry cake"},{"name":"that's way too much meringue"}]);
});
}
function test_followJSONpages(){
var assert = require("assert");
followJSONpages("http://localhost:8666", function(data){
assert.deepEqual(data,[{"name":"Genesis cake"},{"name":"Toroid cake"},{"name":"rawberry cake"},{"name":"that's way too much meringue"}]);
console.log("test_followJSONpages: " + JSON.stringify(data));
});
/* nc -l 8666 -k
HTTP/1.1 200 OK
Link: <http://www.zone42.ca/~philippe/cake.json>; rel="next"
[{"name":"Genesis cake"}]
*/
}