This repository has been archived by the owner on Nov 1, 2017. It is now read-only.
/
github.js
496 lines (401 loc) · 14.7 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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// ## Client-side Javascript API wrapper for GitHub (mostly v3)
//
// Stolen, stripped and rebuilt from https://github.com/fitzgen/github-api
// thanks to Nick Fitzgerald
//
(function (globals) {
// Before we implement the API methods, we will define all of our private
// variables and helper functions with one `var` statement.
var
// The username and authentication token of the library's user.
authToken,
// To save keystrokes when we make JSONP calls to the HTTP API, we will keep
// track of the root from which all V2 urls extend.
apiRoot = "https://github.com/api/v2/json/",
api3Root = "https://api.github.com/",
// Send a JSONP request to the Github API that calls `callback` with
// the `context` argument as `this`.
//
// The `url` parameter is concatenated with the apiRoot, for the reasons
// mentioned above. The way that we are supporting non-global, anonymous
// functions is by sticking them in the globally exposed
// `gh.__jsonp_callbacks` object with a "unique" `id` that is the current
// time in milliseconds. Once the callback is called, it is deleted from the
// object to prevent memory leaks.
jsonp = function (url, callback, context) {
var id = +new Date,
script = document.createElement("script");
while (gh.__jsonp_callbacks[id] !== undefined)
id += Math.random(); // Avoid slight possibility of id clashes.
gh.__jsonp_callbacks[id] = function () {
delete gh.__jsonp_callbacks[id];
callback.apply(context, arguments);
};
var prefix = "?";
if (url.indexOf("?") >= 0)
prefix = "&";
url += prefix + "callback=" + encodeURIComponent("gh.__jsonp_callbacks[" + id + "]");
if (authToken) {
url += "&access_token=" + authToken;
}
script.setAttribute("src", apiRoot + url);
document.getElementsByTagName('head')[0].appendChild(script);
},
// post to sinatra proxy so this is do-able, freaking cross-site grr
postp = function (url, vals, callback) {
v = {}
v['proxy_url'] = url
v['datap'] = Base64.encode(JSON.stringify(vals))
$.post('/proxy', v, callback, 'json')
},
// This helper function will throw a TypeError if the library user is not
// properly authenticated. Otherwise, it silently returns.
authRequired = function (username) {
if (!authToken) {
throw new TypeError("gh: Must be authenticated to do that.");
}
},
// Convert an object to a url parameter string.
//
// paramify({foo:1, bar:3}) -> "foo=1&bar=3".
paramify = function (params) {
var str = "", key;
for (key in params) if (params.hasOwnProperty(key))
str += key + "=" + params[key] + "&";
return str.replace(/&$/, "");
},
// Get around how the GH team haven't migrated all the API to version 2, and
// how gists use a different api root.
withTempApiRoot = function (tempApiRoot, fn) {
return function () {
var oldRoot = apiRoot;
apiRoot = tempApiRoot;
fn.apply(this, arguments);
apiRoot = oldRoot;
};
},
withV3Api = function (fn) {
return function () {
var oldRoot = apiRoot;
apiRoot = api3Root;
fn.apply(this, arguments);
apiRoot = oldRoot;
};
},
// Expose the global `gh` variable, through which every API method is
// accessed, but keep a local variable around so we can reference it easily.
gh = globals.gh = {};
// Psuedo private home for JSONP callbacks (which are required to be global
// by the nature of JSONP, as discussed earlier).
gh.__jsonp_callbacks = {};
// Authenticate as a user. Does not try to validate at any point; that job
// is up to each individual method, which calls `authRequired` as needed.
gh.authenticate = function (token) {
authToken = token;
return this;
};
// ### Users
// The constructor for user objects. Just creating an instance of a user
// doesn't fetch any data from GitHub, you need to get explicit about what
// you want to do that.
//
// var huddlej = gh.user("huddlej");
gh.user = function (username) {
if ( !(this instanceof gh.user)) {
return new gh.user(username);
}
this.username = username;
};
// Show basic user info; you can get more info if you are authenticated as
// this user.
//
// gh.user("fitzgen").show(function (data) {
// console.log(data.user);
// });
gh.user.prototype.show = withV3Api(function (callback, context) {
jsonp("users/" + this.username, callback, context);
return this;
});
gh.user.prototype.orgs = withV3Api(function (callback, context) {
jsonp("user/orgs", callback, context);
return this;
});
gh.user.prototype.orgRepos = withV3Api(function (org, callback, context) {
jsonp("orgs/" + org + "/repos", callback, context);
return this;
});
// Get a list of this user's repositories, 30 per page
//
// gh.user("fitzgen").repos(function (data) {
// data.repositories.forEach(function (repo) {
// ...
// });
// });
gh.user.prototype.repos = function (callback, context, page) {
gh.repo.forUser(this.username, callback, context, page);
return this;
};
// Get a list of all repos for this user.
//
// gh.user("fitzgen").allRepos(function (data) {
// alert(data.repositories.length);
// });
gh.user.prototype.allRepos = function (callback, context) {
var repos = [],
username = this.username,
page = 1;
function exitCallback () {
callback.call(context, { repositories: repos });
}
function pageLoop (data) {
if (data.data.length == 0) {
exitCallback();
} else {
repos = repos.concat(data.data);
page += 1;
if (data.data.length < 100) {
exitCallback();
} else {
gh.repo.forUser(username, pageLoop, context, page);
}
}
}
gh.repo.forUser(username, pageLoop, context, page);
return this;
};
// Get a list of all repos that this user can push to (including ones that
// they are just a collaborator on, and do not own). Must be authenticated
// as this user.
gh.user.prototype.pushable = function (callback, context) {
authRequired(this.user);
jsonp("repos/pushable", callback, context);
};
// ### Repositories
// This is the base constructor for creating repo objects. Note that this
// won't actually hit the GitHub API until you specify what data you want,
// or what action you wish to take via a prototype method.
gh.repo = function (user, repo) {
if ( !(this instanceof gh.repo)) {
return new gh.repo(user, repo);
}
this.repo = repo;
this.user = user;
};
// Get basic information on this repo.
//
// gh.repo("schacon", "grit").show(function (data) {
// console.log(data.repository.description);
// });
gh.repo.prototype.show = function (callback, context) {
jsonp("repos/show/" + this.user + "/" + this.repo, callback, context);
return this;
};
// Get all the repos that are owned by `user`.
gh.repo.forUser = withV3Api(function (user, callback, context, page) {
if (!page)
page = 1;
jsonp("user/repos?page=" + page + '&per_page=' + 100, callback, context);
return this;
});
gh.repo.prototype.commit = function (sha) {
return gh.commit(this.user, this.repo, sha)
};
gh.repo.prototype.tree = function (sha) {
return gh.tree(this.user, this.repo, sha)
};
gh.repo.prototype.blob = function (sha) {
return gh.blob(this.user, this.repo, sha)
};
gh.repo.prototype.ref = function (ref, sha) {
return gh.ref(this.user, this.repo, ref, sha)
};
gh.repo.prototype.branches = withV3Api(function (callback, context) {
jsonp("repos/" + this.user + "/" + this.repo + "/git/refs/heads", callback, context);
return this;
});
// ### Commits
gh.commit = function (user, repo, sha) {
if ( !(this instanceof gh.commit) )
return new gh.commit(user, repo, sha);
this.user = user;
this.repo = repo;
this.sha = sha;
};
gh.commit.prototype.show = withV3Api(function (callback, context) {
jsonp("repos/" + this.user + "/" + this.repo + "/commits/" + this.sha,
callback, context);
return this;
});
gh.commit.prototype.list = withV3Api(function (callback, context) {
jsonp("repos/" + this.user + "/" + this.repo + "/commits?sha=" + this.sha,
callback, context);
return this;
});
gh.commit.prototype.write = function (commitHash, callback) {
url = "repos/" + this.user + "/" + this.repo + "/git/commits"
postp(url, commitHash, callback)
return this;
};
// ### Trees
gh.tree = function (user, repo, sha) {
if ( !(this instanceof gh.tree) )
return new gh.tree(user, repo, sha);
this.user = user;
this.repo = repo;
this.sha = sha;
};
gh.tree.prototype.show = withV3Api(function (callback, context) {
jsonp("repos/" + this.user + "/" + this.repo + "/git/trees/" + this.sha,
callback, context);
return this;
});
gh.tree.prototype.write = function (treeHash, callback) {
url = "repos/" + this.user + "/" + this.repo + "/git/trees"
postp(url, treeHash, callback)
return this;
};
// ### Blobs
gh.blob = function (user, repo, sha) {
if ( !(this instanceof gh.blob) )
return new gh.blob(user, repo, sha);
this.user = user;
this.repo = repo;
this.sha = sha;
};
gh.blob.prototype.show = withV3Api(function (callback, context) {
jsonp("repos/" + this.user + "/" + this.repo + "/git/blobs/" + this.sha,
callback, context);
return this;
});
gh.blob.prototype.write = function (content, callback) {
var vals = {}
vals['content'] = content
url = "repos/" + this.user + "/" + this.repo + "/git/blobs"
postp(url, vals, callback)
return this;
};
gh.blob.prototype.decode = function (data) {
return Base64.decode(data)
}
// ### Refs
gh.ref = function (user, repo, ref, sha) {
if ( !(this instanceof gh.ref) )
return new gh.ref(user, repo, ref, sha);
this.user = user;
this.repo = repo;
this.ref = ref;
this.sha = sha;
};
gh.ref.prototype.show = withV3Api(function (callback, context) {
jsonp("repos/" + this.user + "/" + this.repo + "/git/" + this.ref,
callback, context);
return this;
});
gh.ref.prototype.update = function (commitSha, callback) {
var vals = {'ref': this.ref, 'type': 'commit', 'sha': commitSha}
url = "repos/" + this.user + "/" + this.repo + "/git/" + this.ref
postp(url, vals, callback)
return this;
};
}(window));
var Base64 = {
// private property
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
// public method for encoding
encode : function (input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = Base64._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
}
return output;
},
// public method for decoding
decode : function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = this._keyStr.indexOf(input.charAt(i++));
enc2 = this._keyStr.indexOf(input.charAt(i++));
enc3 = this._keyStr.indexOf(input.charAt(i++));
enc4 = this._keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = Base64._utf8_decode(output);
return output;
},
// private method for UTF-8 encoding
_utf8_encode : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
// private method for UTF-8 decoding
_utf8_decode : function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
}