Skip to content

Commit 9377c71

Browse files
incropAurelioDeRosa
authored andcommitted
repo.write: Support author and committer info
Closes gh-214
1 parent 61f526f commit 9377c71

3 files changed

Lines changed: 35 additions & 7 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ repo.listBranches(function(err, branches) {});
130130
Store contents at a certain path, where files that don't yet exist are created on the fly.
131131

132132
```js
133-
repo.write('master', 'path/to/file', 'YOUR_NEW_CONTENTS', 'YOUR_COMMIT_MESSAGE', function(err) {});
133+
var options = {
134+
author: {name: 'Author Name', email: 'author@example.com'},
135+
committer: {name: 'Committer Name', email: 'committer@example.com'}
136+
}
137+
repo.write('master', 'path/to/file', 'YOUR_NEW_CONTENTS', 'YOUR_COMMIT_MESSAGE', options, function(err) {});
134138
```
135139

136140
Not only can you can write files, you can of course read them.

github.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,13 +718,19 @@
718718
// Write file contents to a given branch and path
719719
// -------
720720

721-
this.write = function(branch, path, content, message, cb) {
721+
this.write = function(branch, path, content, message, options, cb) {
722+
if (typeof cb === 'undefined') {
723+
cb = options;
724+
options = {};
725+
}
722726
that.getSha(branch, encodeURI(path), function(err, sha) {
723727
if (err && err.error !== 404) return cb(err);
724728
_request("PUT", repoPath + "/contents/" + encodeURI(path), {
725729
message: message,
726730
content: b64encode(content),
727731
branch: branch,
732+
committer: options.committer,
733+
author: options.author,
728734
sha: sha
729735
}, cb);
730736
});

test/test.repo.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,24 @@ test("Repo API", function(t) {
5858

5959
t.test('repo.read', function(q) {
6060
repo.read('master', 'README.md', function(err, res) {
61-
q.ok(res.indexOf('# Github.js') !== -1, true, 'Returned README');
61+
q.notEquals(res.indexOf('# Github.js'), -1, 'Returned README');
6262
q.end();
6363
});
6464
});
6565

6666
t.test('repo.getCommit', function(q) {
6767
repo.getCommit('master', '20fcff9129005d14cc97b9d59b8a3d37f4fb633b', function(err, commit) {
6868
q.error(err, 'get commit' + err);
69-
q.ok(commit.message, 'v0.10.4', 'Returned commit message.');
70-
q.ok(commit.author.date, '2015-03-20T17:01:42Z', 'Got correct date.');
69+
q.equals(commit.message, 'v0.10.4', 'Returned commit message.');
70+
q.equals(commit.author.date, '2015-03-20T17:01:42Z', 'Got correct date.');
7171
q.end();
7272
});
7373
});
7474

7575
t.test('repo.getSha', function(q) {
7676
repo.getSha('master', '.gitignore', function(err, sha) {
7777
q.error(err, 'get sha error: ' + err);
78-
q.ok(sha, '153216eb946aedc51f4fe88a51008b4abcac5308', 'Returned sha message.');
78+
q.equals(sha, '153216eb946aedc51f4fe88a51008b4abcac5308', 'Returned sha message.');
7979
q.end();
8080
});
8181
});
@@ -191,6 +191,24 @@ test('Create Repo', function(t) {
191191
});
192192
});
193193

194+
t.test('repo.writeAuthorAndCommitter', function(q) {
195+
var options = {
196+
author: {name: 'Author Name', email: 'author@example.com'},
197+
committer: {name: 'Committer Name', email: 'committer@example.com'}
198+
};
199+
repo.write('dev', 'TEST.md', 'THIS IS A TEST BY AUTHOR AND COMMITTER', 'Updating', options, function(err, res) {
200+
q.error(err);
201+
repo.getCommit('dev', res.commit.sha, function(err, commit) {
202+
q.error(err);
203+
q.equals(commit.author.name, 'Author Name', 'Got correct author name.');
204+
q.equals(commit.author.email, 'author@example.com', 'Got correct author email.');
205+
q.equals(commit.committer.name, 'Committer Name', 'Got correct committer name.');
206+
q.equals(commit.committer.email, 'committer@example.com', 'Got correct committer email.');
207+
q.end();
208+
});
209+
});
210+
});
211+
194212
t.test('repo.writeChinese', function(q) {
195213
repo.write('master', '中文测试.md', 'THIS IS A TEST', 'Creating test', function(err) {
196214
q.error(err);
@@ -248,4 +266,4 @@ test('Repo Returns commit errors correctly', function(t) {
248266
clearTimeout(timeout);
249267
t.end();
250268
});
251-
});
269+
});

0 commit comments

Comments
 (0)