forked from fitzgen/github-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub.js
More file actions
520 lines (450 loc) · 16.1 KB
/
Copy pathgithub.js
File metadata and controls
520 lines (450 loc) · 16.1 KB
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
(function (globals) {
var
// Keep authentication variables private.
authUsername,
authToken,
// Always using JSON because it is the only way to leverage JSONP.
apiRoot = "https://github.com/api/v2/json/",
// Send a JSONP request to the Github API that calls `callback` with
// `context` as `this`.
jsonp = function (url, callback, context) {
var id = +new Date,
script = document.createElement("script");
gh.__jsonp_callbacks[id] = function () {
delete gh.__jsonp_callbacks[id];
callback.apply(context, arguments);
};
url += "?callback=" + encodeURIComponent("gh.__jsonp_callbacks["+ id +"]");
if (authUsername && authToken) {
url += "&login=" + authUsername + "&authToken=" + authToken;
}
script.setAttribute("src", apiRoot + url);
document.getElementsByTagName('head')[0].appendChild(script);
},
// Send an HTTP POST. Unfortunately, it isn't possible to support a callback
// with the resulting data. (Please prove me wrong!)
post = function (url, vals) {
var
form = document.createElement("form"),
iframe = document.createElement("iframe"),
doc = iframe.contentDocument !== undefined ?
iframe.contentDocument :
iframe.contentWindow.document,
key, field;
vals = vals || {};
form.setAttribute("method", "post");
form.setAttribute("action", apiRoot + url);
for (key in vals) {
if (vals.hasOwnProperty(key)) {
field = document.createElement("input");
field.type = "hidden";
field.value = encodeURIComponent(vals[key]);
form.appendChild(field);
}
}
iframe.setAttribute("style", "display: none;");
doc.body.appendChild(form);
document.body.appendChild(iframe);
form.submit();
},
// Throw a TypeError if the user is not authenticated properly.
authRequired = function (username) {
if (!authUsername || !authToken || authUsername !== username) {
throw new TypeError("gh: Must be authenticated to do that.");
}
},
// Convert an object to a url parameter string, e.g. {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;
};
},
// Expose global gh variable and keep a local variable.
gh = globals.gh = {};
// Psuedo private home for JSONP callbacks (which are required to be global
// by the nature of JSONP).
gh.__jsonp_callbacks = {};
// Authenticate a user. Does not try to validate at any point.
gh.authenticate = function (username, token) {
authUsername = username;
authToken = token;
return this;
};
/*
* Users
*/
gh.user = function (username) {
if ( !(this instanceof gh.user)) {
return new gh.user(username);
}
this.username = username;
};
// Show basic user info. More info if authenticated.
gh.user.prototype.show = function (callback, context) {
jsonp("user/show/" + this.username, callback, context);
return this;
};
// Update user info. Must be authenticated as this user.
gh.user.prototype.update = function (params) {
authRequired(this.username);
var key, postData = {
login: authUsername,
token: authToken
};
for (key in params) {
if (params.hasOwnProperty(key)) {
postData["values["+key+"]"] = encodeURIComponent(params[key]);
}
}
post("user/show/" + this.username, postData);
return this;
};
// Find out who this user is following.
gh.user.prototype.following = function (callback, context) {
jsonp("user/show/" + this.username + "/following", callback, context);
};
// Find out who is following this user.
gh.user.prototype.followers = function (callback, context) {
jsonp("user/show/" + this.username + "/followers", callback, context);
};
// Make this user follow some other user. Must be authenticated as this
// user.
gh.user.prototype.follow = function (user) {
authRequired.call(this);
post("user/follow/" + user);
return this;
};
// Quit following the given user. Must be authenticated as this user.
gh.user.prototype.unfollow = function (user) {
authRequired.call(this);
post("user/unfollow/" + user);
return this;
};
// Find out who this user is watching.
gh.user.prototype.watching = function (callback, context) {
jsonp("repos/watched/" + this.username, callback, context);
return this;
};
// Find out what repos this user has.
gh.user.prototype.repos = function (callback, context) {
gh.repo.forUser(this.username, callback, context);
return this;
};
// Fork the repo that lives at http://github.com/user/repo. Must be
// authenticated.
gh.user.prototype.forkRepo = function (user, repo) {
authRequired(this.username);
post("repos/fork/" + user + "/" + repo);
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.
gh.user.prototype.pushable = function (callback, context) {
authRequired(authUsername);
jsonp("repos/pushable", callback, context);
};
gh.user.prototype.publicGists = withTempApiRoot(
"http://gist.github.com/api/v1/json/gists/",
function (callback, context) {
jsonp(this.username, callback, context);
return this;
}
);
// Search users for `query`.
gh.user.search = function (query, callback, context) {
jsonp("user/search/" + query, callback, context);
return this;
};
/*
* Repositories
*/
gh.repo = function (user, repo) {
if ( !(this instanceof gh.repo)) {
return new gh.repo(user, repo);
}
this.repo = repo;
this.user = user;
};
// Get information on this repo.
gh.repo.prototype.show = function (callback, context) {
jsonp("repos/show/" + this.user + "/" + this.repo, callback, context);
return this;
};
// Update information on this repo. Must be authenticated. Params can include:
// * description
// * homepage
// * has_wiki
// * has_issues
// * has_downloads
gh.repo.prototype.update = function (params) {
authRequired(this.user);
var key, postData = {
login: authUsername,
token: authToken
};
for (key in params) {
if (params.hasOwnProperty(key)) {
postData["values["+key+"]"] = encodeURIComponent(params[key]);
}
}
post("repos/show/" + this.user + "/" + this.repo, postData);
return this;
};
// Get all tags in this repo.
gh.repo.prototype.tags = function (callback, context) {
jsonp("repos/show/" + this.user + "/" + this.repo + "/tags",
callback,
context);
return this;
};
// Get all branches in this repo.
gh.repo.prototype.branches = function (callback, context) {
jsonp("repos/show/" + this.user + "/" + this.repo + "/branches",
callback,
context);
return this;
};
// Gather line count information on the language(s) used in this repo.
gh.repo.prototype.languages = function (callback, context) {
jsonp("/repos/show/" + this.user + "/" + this.repo + "/languages",
callback,
context);
return this;
};
// Gather data on all the forks of this repo.
gh.repo.prototype.network = function (callback, context) {
jsonp("repos/show/" + this.user + "/" + this.repo + "/network",
callback,
context);
return this;
};
// All users who have contributed to this repo. Pass true to showAnon if you
// want to see the non-github contributors.
gh.repo.prototype.contributors = function (callback, context, showAnon) {
var url = "repos/show/" + this.user + "/" + this.repo + "/contributors";
if (showAnon)
url += "/anon";
jsonp(url,
callback,
context);
return this;
};
// Get all of the collaborators for this repo.
gh.repo.prototype.collaborators = function (callback, context) {
jsonp("repos/show/" + this.user + "/" + this.repo + "/collaborators",
callback,
context);
return this;
};
// Add a collaborator to this project. Must be authenticated.
gh.repo.prototype.addCollaborator = function (collaborator) {
authRequired(this.user);
post("repos/collaborators/" + this.repo + "/add/" + collaborator);
return this;
};
// Remove a collaborator from this project. Must be authenticated.
gh.repo.prototype.removeCollaborator = function (collaborator) {
authRequired(this.user);
post("repos/collaborators/" + this.repo + "/remove/" + collaborator);
return this;
};
// Make this repository private. Authentication required.
gh.repo.prototype.setPrivate = function () {
authRequired(this.user);
post("repo/set/private/" + this.repo);
return this;
};
// Make this repository public. Authentication required.
gh.repo.prototype.setPublic = function () {
authRequired(this.user);
post("repo/set/public/" + this.repo);
return this;
};
// Search for repositories. `opts` may include `start_page` or `language`,
// which must be capitalized.
gh.repo.search = function (query, opts, callback, context) {
var url = "repos/search/" + query.replace(" ", "+");
if (typeof opts === "function") {
opts = {};
callback = arguments[1];
context = arguments[2];
}
url += "?" + paramify(opts);
return this;
};
// Get all the repos that are owned by `user`.
gh.repo.forUser = function (user, callback, context) {
jsonp("repos/show/" + user, callback, context);
return this;
};
// Create a repository. Must be authenticated.
gh.repo.create = function (name, opts) {
authRequired(authUsername);
opts.name = name;
post("repos/create", opts);
return this;
};
// Delete a repository. Must be authenticated.
gh.repo.del = function (name) {
authRequired(authUsername);
post("repos/delete/" + name);
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 = function (callback, context) {
jsonp("commits/show/" + this.user + "/" + this.repo + "/" + this.sha,
callback,
context);
return this;
};
// Get a list of all commits on a repos branch.
gh.commit.forBranch = function (user, repo, branch, callback, context) {
jsonp("commits/list/" + user + "/" + repo + "/" + branch,
callback,
context);
return this;
};
// Get a list of all commits on this path (file or dir).
gh.commit.forPath = function (user, repo, branch, path, callback, context) {
jsonp("commits/list/" + user + "/" + repo + "/" + branch + "/" + path,
callback,
context);
return this;
};
/*
* Issues
*/
gh.issue = function (user, repo, number) {
if ( !(this instanceof gh.issue) )
return new gh.commit(user, repo, number);
this.user = user;
this.repo = repo;
this.number = number;
};
// View this issue's info.
gh.issue.prototype.show = function (callback, context) {
jsonp("issues/show/" + this.user + "/" + this.repo + "/" + this.number,
callback,
context);
return this;
};
// Get a list of all comments on this issue.
gh.issue.prototype.comments = function (callback, context) {
jsonp("issues/comments/" + this.user + "/" + this.repo + "/" + this.number,
callback,
context);
return this;
};
// Close this issue.
gh.issue.prototype.close = function () {
authRequired(this.user);
post("issues/close/" + this.user + "/" + this.repo + "/" + this.number);
return this;
};
// Reopen this issue.
gh.issue.prototype.reopen = function () {
authRequired(this.user);
post("issues/reopen/" + this.user + "/" + this.repo + "/" + this.number);
return this;
};
// Reopen this issue.
gh.issue.prototype.update = function (title, body) {
authRequired(this.user);
post("issues/edit/" + this.user + "/" + this.repo + "/" + this.number, {
title: title,
body: body
});
return this;
};
// Add `label` to this issue. If the label is not yet in the system, it will
// be created.
gh.issue.prototype.addLabel = function (label) {
post("issues/label/add/" + this.user + "/" + this.repo + "/" + label + "/" + this.number);
return this;
};
// Remove a label from this issue.
gh.issue.prototype.removeLabel = function (label) {
post("issues/label/remove/" + this.user + "/" + this.repo + "/" + label + "/" + this.number);
return this;
};
// Comment on this issue as the user that is authenticated.
gh.issue.prototype.comment = function (comment) {
authRequired(authUsername);
post("/issues/comment/" + user + "/" + repo + "/" + this.number, {
comment: comment
});
return this;
};
// Get all issues' labels for the repo.
gh.issue.labels = function (user, repo) {
jsonp("issues/labels/" + user + "/" + repo,
callback,
context);
return this;
};
// Open an issue. Must be authenticated.
gh.issue.open = function (repo, title, body) {
authRequired(authUsername);
post("issues/open/" + authUsername + "/" + repo, {
title: title,
body: body
});
return this;
};
// Search a repository's issue tracker. `state` can be "open" or "closed".
gh.issue.search = function (user, repo, state, query, callback, context) {
jsonp("/issues/search/" + user + "/" + repo + "/" + state + "/" + query,
callback,
context);
return this;
};
// Get a list of issues for the given repo. `state` can be "open" or
// "closed".
gh.issue.list = function (user, repo, state, callback, context) {
jsonp("issues/list/" + user + "/" + repo + "/" + state,
callback,
context);
return this;
};
gh.gist = function (id) {
if ( !(this instanceof gh.gist) ) {
return new gh.gist(id);
}
this.id = id;
};
gh.gist.prototype.show = withTempApiRoot(
"http://gist.github.com/api/v1/json/",
function (callback, context) {
jsonp(this.id, callback, cont);
return this;
}
);
gh.gist.prototype.file = withTempApiRoot(
"http://gist.github.com/raw/v1/json/",
function (filename, callback, context) {
jsonp(this.id + "/" + filename, callback, cont);
return this;
}
);
}(window));