Skip to content

Commit fac1911

Browse files
author
Ændrew Rininsland
committed
Added 'encode' option to repo.write, added more tests. Tweaked jscsrc. Resolves #251.
1 parent f63deb8 commit fac1911

File tree

7 files changed

+106
-50
lines changed

7 files changed

+106
-50
lines changed

.jscsrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"disallowYodaConditions": true,
3939
"maximumLineLength": 120,
4040
"requireBlocksOnNewline": true,
41-
"requireCamelCaseOrUpperCaseIdentifiers": true,
41+
"requireCamelCaseOrUpperCaseIdentifiers": false,
4242
"requireCapitalizedComments": {
4343
"allExcept": [
4444
"exported",

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ You can also provide an optional object literal, (`options` in the example below
133133
```js
134134
var options = {
135135
author: {name: 'Author Name', email: 'author@example.com'},
136-
committer: {name: 'Committer Name', email: 'committer@example.com'}
136+
committer: {name: 'Committer Name', email: 'committer@example.com'},
137+
encode: true // Whether to base64 encode the file. (default: true)
137138
}
138139
repo.write('master', 'path/to/file', 'YOUR_NEW_CONTENTS', 'YOUR_COMMIT_MESSAGE', options, function(err) {});
139140
```

github.js

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -413,44 +413,49 @@
413413

414414
this.listPulls = function(options, cb) {
415415
options = options || {};
416-
var url = repoPath + "/pulls";
416+
var url = repoPath + '/pulls';
417417
var params = [];
418418

419419
if (typeof options === 'string') {
420-
// backward compatibility
421-
params.push('state=' + options);
422-
}
423-
else {
424-
if (options.state) {
425-
params.push("state=" + encodeURIComponent(options.state));
426-
}
427-
if (options.head) {
428-
params.push("head=" + encodeURIComponent(options.head));
429-
}
430-
if (options.base) {
431-
params.push("base=" + encodeURIComponent(options.base));
432-
}
433-
if (options.sort) {
434-
params.push("sort=" + encodeURIComponent(options.sort));
435-
}
436-
if (options.direction) {
437-
params.push("direction=" + encodeURIComponent(options.direction));
438-
}
439-
if (options.page) {
440-
params.push("page=" + options.page);
441-
}
442-
if (options.per_page) {
443-
params.push("per_page=" + options.per_page);
444-
}
420+
// Backward compatibility
421+
params.push('state=' + options);
422+
} else {
423+
if (options.state) {
424+
params.push('state=' + encodeURIComponent(options.state));
425+
}
426+
427+
if (options.head) {
428+
params.push('head=' + encodeURIComponent(options.head));
429+
}
430+
431+
if (options.base) {
432+
params.push('base=' + encodeURIComponent(options.base));
433+
}
434+
435+
if (options.sort) {
436+
params.push('sort=' + encodeURIComponent(options.sort));
437+
}
438+
439+
if (options.direction) {
440+
params.push('direction=' + encodeURIComponent(options.direction));
441+
}
442+
443+
if (options.page) {
444+
params.push('page=' + options.page);
445+
}
446+
447+
if (options.per_page) {
448+
params.push('per_page=' + options.per_page);
449+
}
445450
}
446451

447452
if (params.length > 0) {
448-
url += "?" + params.join("&");
453+
url += '?' + params.join('&');
449454
}
450455

451456
_request('GET', url, null, function(err, pulls, xhr) {
452-
if (err) return cb(err);
453-
cb(null, pulls, xhr);
457+
if (err) return cb(err);
458+
cb(null, pulls, xhr);
454459
});
455460
};
456461

@@ -536,7 +541,7 @@
536541
};
537542
} else {
538543
content = {
539-
content: b64encode(String.fromCharCode.apply(null, new Uint8Array(content))),
544+
content: b64encode(content),
540545
encoding: 'base64'
541546
};
542547
}
@@ -552,7 +557,7 @@
552557

553558
this.updateTree = function(baseTree, path, blob, cb) {
554559
var data = {
555-
base_tree: baseTree, // jscs:ignore requireCamelCaseOrUpperCaseIdentifiers
560+
base_tree: baseTree,
556561
tree: [
557562
{
558563
path: path,
@@ -797,7 +802,7 @@
797802
that.getSha(branch, encodeURI(path), function(err, sha) {
798803
var writeOptions = {
799804
message: message,
800-
content: b64encode(content),
805+
content: typeof options.encode === 'undefined' || options.encode ? b64encode(content) : content,
801806
branch: branch,
802807
committer: options && options.committer ? options.committer : undefined,
803808
author: options && options.author ? options.author : undefined
@@ -957,7 +962,7 @@
957962
};
958963

959964
this.comment = function(issue, comment, cb) {
960-
_request('POST', issue.comments_url, { // jscs:ignore requireCamelCaseOrUpperCaseIdentifiers
965+
_request('POST', issue.comments_url, {
961966
body: comment
962967
}, function(err, res) {
963968
cb(err, res);

gulpfile.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,28 @@ function runTests (singleRun, done) {
5050
var reporters = ['mocha'];
5151
var preprocessors = {};
5252

53-
var pathSrcJs = [
53+
var files = [
5454
path.join(__dirname, 'github.js'),
55-
path.join(__dirname, 'test/*.js'),
56-
path.join(__dirname, 'test/user.json')
55+
path.join(__dirname, 'test/*.js')
5756
];
5857

5958
if (singleRun) {
60-
pathSrcJs.forEach(function(path) {
59+
files.forEach(function(path) {
6160
preprocessors[path] = ['coverage'];
6261
});
6362
reporters.push('coverage');
64-
65-
preprocessors['test/user.json'] = ['json_fixtures'];
6663
}
6764

65+
files.push(path.join(__dirname, 'test/user.json'));
66+
files.push({
67+
pattern: path.join(__dirname, 'test/gh.png'),
68+
watched: false,
69+
included: false
70+
});
71+
preprocessors['test/user.json'] = ['json_fixtures'];
72+
6873
var localConfig = {
69-
files: pathSrcJs,
74+
files: files,
7075
configFile: path.join(__dirname, './karma.conf.js'),
7176
singleRun: singleRun,
7277
autoWatch: !singleRun,

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = function(config) {
1212

1313
frameworks: ['mocha', 'chai'],
1414

15-
browsers: ['PhantomJS'],
15+
browsers: ['PhantomJS2'],
1616

1717
coverageReporter: {
1818
reporters: [

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"karma-json-fixtures-preprocessor": "0.0.5",
2626
"karma-mocha": "^0.2.0",
2727
"karma-mocha-reporter": "^1.1.1",
28-
"karma-phantomjs-launcher": "^0.2.1",
28+
"karma-phantomjs2-launcher": "^0.3.2",
2929
"mocha": "^2.3.3",
3030
"phantomjs": "^1.9.18"
3131
},

test/test.repo.js

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
var github, repo, user, testUser, timeout;
3+
var github, repo, user, testUser, timeout, imageB64, imageBlob;
44

5-
if (typeof window === 'undefined') {
5+
if (typeof window === 'undefined') { // We're in NodeJS
66
// Module dependencies
77
var chai = require('chai');
88
var Github = require('../');
@@ -14,9 +14,32 @@ if (typeof window === 'undefined') {
1414

1515
// Long timeouts for Mocha.
1616
timeout = 60000;
17-
} else {
18-
// Short timeouts for Karma!
17+
18+
var fs = require('fs');
19+
var path = require('path');
20+
21+
imageBlob = fs.readFileSync(path.join(__dirname, 'gh.png')); // This is a Buffer().
22+
imageB64 = imageBlob.toString('base64');
23+
} else { // We're in the browser
24+
// Shorter timeouts for Karma!
1925
timeout = 12000;
26+
27+
var xhr = new XMLHttpRequest();
28+
29+
xhr.responseType = 'blob';
30+
xhr.open('GET', 'base/test/gh.png');
31+
xhr.onload = function() {
32+
var reader = new FileReader();
33+
34+
reader.onloadend = function() {
35+
imageB64 = btoa(reader.result);
36+
imageBlob = reader.result;
37+
};
38+
39+
reader.readAsBinaryString(xhr.response);
40+
};
41+
42+
xhr.send();
2043
}
2144

2245
describe('Github.Repository', function() {
@@ -216,13 +239,13 @@ describe('Creating new Github.Repository', function() {
216239
sort: 'updated',
217240
direction: 'desc',
218241
page: 1,
219-
per_page: 100
242+
per_page: 10
220243
};
221244

222245
repo.listPulls(options, function(err, pull_list) {
223246
should.not.exist(err);
224247
pull_list.should.be.instanceof(Array);
225-
pull_list.should.have.length(100);
248+
pull_list.should.have.length(10);
226249
should.exist(pull_list[0].title);
227250
should.exist(pull_list[0].body);
228251
should.exist(pull_list[0].url);
@@ -300,6 +323,7 @@ describe('Creating new Github.Repository', function() {
300323
repo.write('master', 'TEST_unicode.md', '\u2014', 'Long dash unicode', function(err) {
301324
should.not.exist(err);
302325

326+
if (err) console.log(err);
303327
repo.read('master', 'TEST_unicode.md', function(err, obj) {
304328
should.not.exist(err);
305329
obj.should.equal('\u2014');
@@ -334,6 +358,27 @@ describe('Creating new Github.Repository', function() {
334358
});
335359
});
336360
});
361+
362+
it('should be able to write an image to the repo', function(done) {
363+
repo.write('master', 'TEST_image.png', imageB64, 'Image test', {
364+
encode: false
365+
}, function(err) {
366+
if (err) console.log(err);
367+
should.not.exist(err);
368+
done();
369+
});
370+
});
371+
372+
it('should be able to write a blob to the repo', function(done) {
373+
repo.postBlob('String test', function(err) { // Test strings
374+
should.not.exist(err);
375+
376+
repo.postBlob(imageBlob, function(err) { // Test non-strings
377+
should.not.exist(err);
378+
done();
379+
});
380+
});
381+
});
337382
});
338383

339384
describe('deleting a Github.Repository', function() {

0 commit comments

Comments
 (0)