From e6f361cc8d0d6f62b1901c1e457feaef109d1e9f Mon Sep 17 00:00:00 2001 From: Terence Lee Date: Mon, 24 Mar 2014 11:56:17 +0800 Subject: [PATCH 1/8] added repo.req access point to allow making custom request without setting up headers/tokens again --- github.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/github.js b/github.js index bc63ab0d..2a9e90d0 100644 --- a/github.js +++ b/github.js @@ -647,6 +647,8 @@ }; }; + this.req=_request; + // Gists API // ======= From 8f3b016915e23bb6280cc593b18cbae697fcea40 Mon Sep 17 00:00:00 2001 From: Terence Lee Date: Mon, 24 Mar 2014 12:35:41 +0800 Subject: [PATCH 2/8] added repo.req access point to allow making custom request without setting up headers/tokens again --- github.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/github.js b/github.js index 2a9e90d0..fec69880 100644 --- a/github.js +++ b/github.js @@ -645,9 +645,10 @@ } _request("GET", url, null, cb); }; - }; - this.req=_request; + this.req=_request; + + }; // Gists API // ======= From 20653f7acd43b04137254cba70a613e10eb9531d Mon Sep 17 00:00:00 2001 From: Terence Lee Date: Mon, 24 Mar 2014 16:19:48 +0800 Subject: [PATCH 3/8] Add option to limit number of commits to list with getCommits --- github.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/github.js b/github.js index fec69880..360e13c4 100644 --- a/github.js +++ b/github.js @@ -612,6 +612,7 @@ // List commits on a repository. Takes an object of optional paramaters: // sha: SHA or branch to start listing commits from // path: Only commits containing this file path will be returned + // per_page: number of commits to list // since: ISO 8601 date - only commits after this date will be returned // until: ISO 8601 date - only commits before this date will be returned // ------- @@ -626,6 +627,9 @@ if (options.path) { params.push("path=" + encodeURIComponent(options.path)); } + if (options.per_page) { + params.push("per_page="+ encodeURIComponent(options.per_page)); + } if (options.since) { var since = options.since; if (since.constructor === Date) { From 6e4c5a6d4546238a0d2023d2e9cde3e8d5f1a644 Mon Sep 17 00:00:00 2001 From: Terence Lee Date: Tue, 25 Mar 2014 10:41:27 +0800 Subject: [PATCH 4/8] Add status===0 in request to allow offline use of xhr --- github.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github.js b/github.js index 360e13c4..668cd65f 100644 --- a/github.js +++ b/github.js @@ -46,7 +46,7 @@ if (!sync) { xhr.onreadystatechange = function () { if (this.readyState == 4) { - if (this.status >= 200 && this.status < 300 || this.status === 304) { + if (this.status >= 200 && this.status < 300 || this.status === 304 || this.status === 0) { cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true, this); } else { cb({path: path, request: this, error: this.status}); From 3280a972252673c4ed3d1a553a88815653fa3695 Mon Sep 17 00:00:00 2001 From: Terence Lee Date: Sun, 30 Mar 2014 12:39:59 +0800 Subject: [PATCH 5/8] fix request header to default to json and allow custom Accept headers --- github.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/github.js b/github.js index 668cd65f..b1f1121d 100644 --- a/github.js +++ b/github.js @@ -33,28 +33,28 @@ // // I'm not proud of this and neither should you be if you were responsible for the XMLHttpRequest spec. - function _request(method, path, data, cb, raw, sync) { + function _request(method, path, data, cb, datatype, sync) { function getURL() { var url = path.indexOf('//') >= 0 ? path : API_URL + path; return url + ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime(); } var xhr = new XMLHttpRequest(); - if (!raw) {xhr.dataType = "json";} + if (!datatype)datatype="json"; xhr.open(method, getURL(), !sync); if (!sync) { xhr.onreadystatechange = function () { if (this.readyState == 4) { if (this.status >= 200 && this.status < 300 || this.status === 304 || this.status === 0) { - cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true, this); + cb(null, datatype ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true, this); } else { cb({path: path, request: this, error: this.status}); } } } }; - xhr.setRequestHeader('Accept','application/vnd.github.raw+json'); + xhr.setRequestHeader('Accept','application/vnd.github.'+datatype); xhr.setRequestHeader('Content-Type','application/json;charset=UTF-8'); if ((options.token) || (options.username && options.password)) { xhr.setRequestHeader('Authorization', options.token From 187b4a3fbc703cbf5b614d7a2055945f75c7dd7e Mon Sep 17 00:00:00 2001 From: Terence Lee Date: Sun, 30 Mar 2014 12:43:46 +0800 Subject: [PATCH 6/8] added datatype option to contents calls --- github.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/github.js b/github.js index b1f1121d..6ad37eb8 100644 --- a/github.js +++ b/github.js @@ -444,8 +444,9 @@ // Get contents // -------- - this.contents = function(branch, path, cb, sync) { - return _request("GET", repoPath + "/contents?ref=" + branch + (path ? "&path=" + path : ""), null, cb, 'raw', sync); + this.contents = function(branch, path, cb, sync, datatype) { + if(!datatype)datatype='raw'; + return _request("GET", repoPath + "/contents?ref=" + branch + (path ? "&path=" + path : ""), null, cb, datatype, sync); }; // Fork repository From c558c6b3c948ef4c570bb23fa506ad303be32341 Mon Sep 17 00:00:00 2001 From: Terence Lee Date: Sun, 30 Mar 2014 22:17:09 +0800 Subject: [PATCH 7/8] further fix to req response datatype handling --- github.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/github.js b/github.js index 6ad37eb8..170c6efb 100644 --- a/github.js +++ b/github.js @@ -47,7 +47,7 @@ xhr.onreadystatechange = function () { if (this.readyState == 4) { if (this.status >= 200 && this.status < 300 || this.status === 304 || this.status === 0) { - cb(null, datatype ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true, this); + cb(null, /^(?!json)/.test(datatype) ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true, this); } else { cb({path: path, request: this, error: this.status}); } @@ -446,7 +446,7 @@ this.contents = function(branch, path, cb, sync, datatype) { if(!datatype)datatype='raw'; - return _request("GET", repoPath + "/contents?ref=" + branch + (path ? "&path=" + path : ""), null, cb, datatype, sync); + return _request("GET", repoPath + "/contents/" + (path ? path : "") + "?ref=" + branch, null, cb, datatype, sync); }; // Fork repository From fea7913fbfac63a1454df96b0445e2be0934110e Mon Sep 17 00:00:00 2001 From: Terence Lee Date: Mon, 31 Mar 2014 18:13:29 +0800 Subject: [PATCH 8/8] added contents_update function --- github.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/github.js b/github.js index 170c6efb..844d8127 100644 --- a/github.js +++ b/github.js @@ -445,10 +445,18 @@ // -------- this.contents = function(branch, path, cb, sync, datatype) { - if(!datatype)datatype='raw'; + if(!datatype)datatype='raw';// use 'json' if you want to get sha of content as well, it's need for update contents return _request("GET", repoPath + "/contents/" + (path ? path : "") + "?ref=" + branch, null, cb, datatype, sync); }; + // Update contents + // -------- + + this.contents_update = function(branch, path, message, content, previous_sha, cb, sync) { + return _request("PUT", repoPath + "/contents/" + (path ? path : "") + "?ref=" + branch, + { message:message, content:Base64.encode(content), sha:previous_sha }, cb, null, sync); + } + // Fork repository // -------