|
10 | 10 |
|
11 | 11 | Github = window.Github = function(options) { |
12 | 12 |
|
13 | | - // Util |
| 13 | + // HTTP Request Abstraction |
14 | 14 | // ======= |
| 15 | + // |
| 16 | + // I'm not proud of this and neither should you be if you were responsible for the XMLHttpRequest spec. |
15 | 17 |
|
16 | 18 | function _request(method, path, data, cb, raw) { |
| 19 | + function getURL() { |
| 20 | + var url = API_URL + path; |
| 21 | + return url + ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime(); |
| 22 | + } |
| 23 | + |
17 | 24 | var xhr = new XMLHttpRequest(); |
18 | 25 | if (!raw) {xhr.dataType = "json"} |
19 | | - xhr.open(method, API_URL + path); |
| 26 | + |
| 27 | + xhr.open(method, getURL()); |
20 | 28 | xhr.onreadystatechange = function () { |
21 | 29 | if (this.readyState == 4) { |
22 | 30 | if (this.status >= 200 && this.status < 300 || this.status === 304) { |
|
143 | 151 | }); |
144 | 152 | }; |
145 | 153 |
|
| 154 | + // Create a new reference |
| 155 | + // -------- |
| 156 | + // |
| 157 | + // { |
| 158 | + // "ref": "refs/heads/my-new-branch-name", |
| 159 | + // "sha": "827efc6d56897b048c772eb4087f854f46256132" |
| 160 | + // } |
| 161 | + |
| 162 | + this.createRef = function(options, cb) { |
| 163 | + _request("POST", repoPath + "/git/refs", options, cb); |
| 164 | + }; |
| 165 | + |
| 166 | + // Delete a reference |
| 167 | + // -------- |
| 168 | + // |
| 169 | + // repo.deleteRef('heads/gh-pages') |
| 170 | + // repo.deleteRef('tags/v1.0') |
| 171 | + |
| 172 | + this.deleteRef = function(ref, cb) { |
| 173 | + _request("DELETE", repoPath + "/git/refs/"+ref, options, cb); |
| 174 | + }; |
| 175 | + |
146 | 176 | // List all branches of a repository |
147 | 177 | // ------- |
148 | 178 |
|
|
269 | 299 | // ------- |
270 | 300 |
|
271 | 301 | this.show = function(cb) { |
272 | | - _request("GET", repoPath, null, function(err, info) { |
273 | | - cb(null, info); |
| 302 | + _request("GET", repoPath, null, cb); |
| 303 | + }; |
| 304 | + |
| 305 | + // Get contents |
| 306 | + // -------- |
| 307 | + |
| 308 | + this.contents = function(path, cb) { |
| 309 | + _request("GET", repoPath + "/contents", { path: path }, cb); |
| 310 | + }; |
| 311 | + |
| 312 | + // Fork repository |
| 313 | + // ------- |
| 314 | + |
| 315 | + this.fork = function(cb) { |
| 316 | + _request("POST", repoPath + "/forks", null, function(err, forkedRepo) { |
| 317 | + cb(null, forkedRepo); |
274 | 318 | }); |
275 | 319 | }; |
276 | 320 |
|
| 321 | + // Create pull request |
| 322 | + // -------- |
| 323 | + |
| 324 | + this.createPullRequest = function(options, cb) { |
| 325 | + _request("POST", repoPath + "/pulls", options, cb); |
| 326 | + }; |
| 327 | + |
277 | 328 | // Read file at given path |
278 | 329 | // ------- |
279 | 330 |
|
280 | 331 | this.read = function(branch, path, cb) { |
281 | 332 | that.getSha(branch, path, function(err, sha) { |
282 | 333 | if (!sha) return cb("not found", null); |
283 | | - that.getBlob(sha, cb); |
| 334 | + that.getBlob(sha, function(err, content) { |
| 335 | + cb(err, content, sha); |
| 336 | + }); |
284 | 337 | }); |
285 | 338 | }; |
286 | 339 |
|
|
0 commit comments