+
Skip to content

Support author and committer info for write() #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ repo.listBranches(function(err, branches) {});
Store contents at a certain path, where files that don't yet exist are created on the fly.

```js
repo.write('master', 'path/to/file', 'YOUR_NEW_CONTENTS', 'YOUR_COMMIT_MESSAGE', function(err) {});
var options = {
author: {name: 'Author Name', email: 'author@example.com'},
committer: {name: 'Committer Name', email: 'committer@example.com'}
}
repo.write('master', 'path/to/file', 'YOUR_NEW_CONTENTS', 'YOUR_COMMIT_MESSAGE', options, function(err) {});
```

Not only can you can write files, you can of course read them.
Expand Down
8 changes: 7 additions & 1 deletion github.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,13 +674,19 @@
// Write file contents to a given branch and path
// -------

this.write = function(branch, path, content, message, cb) {
this.write = function(branch, path, content, message, options, cb) {
if (typeof cb === 'undefined') {
cb = options;
options = {};
}
that.getSha(branch, encodeURI(path), function(err, sha) {
if (err && err.error !== 404) return cb(err);
_request("PUT", repoPath + "/contents/" + encodeURI(path), {
message: message,
content: btoa(content),
branch: branch,
committer: options.committer,
author: options.author,
sha: sha
}, cb);
});
Expand Down
26 changes: 22 additions & 4 deletions test/test.repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,24 @@ test("Repo API", function(t) {

t.test('repo.read', function(q) {
repo.read('master', 'README.md', function(err, res) {
q.ok(res.indexOf('##Setup') !== -1, true, 'Returned REAMDE');
q.notEquals(res.indexOf('##Setup'), -1, 'Returned REAMDE');
q.end();
});
});

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

t.test('repo.getSha', function(q) {
repo.getSha('master', '.gitignore', function(err, sha) {
q.error(err, 'get sha error: ' + err);
q.ok(sha, '153216eb946aedc51f4fe88a51008b4abcac5308', 'Returned sha message.');
q.equals(sha, '153216eb946aedc51f4fe88a51008b4abcac5308', 'Returned sha message.');
q.end();
});
});
Expand Down Expand Up @@ -126,6 +126,24 @@ test('Create Repo', function(t) {
});
});

t.test('repo.writeAuthorAndCommitter', function(q) {
var options = {
author: {name: 'Author Name', email: 'author@example.com'},
committer: {name: 'Committer Name', email: 'committer@example.com'}
};
repo.write('dev', 'TEST.md', 'THIS IS A TEST BY AUTHOR AND COMMITTER', 'Updating', options, function(err, res) {
q.error(err);
repo.getCommit('dev', res.commit.sha, function(err, commit) {
q.error(err);
q.equals(commit.author.name, 'Author Name', 'Got correct author name.');
q.equals(commit.author.email, 'author@example.com', 'Got correct author email.');
q.equals(commit.committer.name, 'Committer Name', 'Got correct committer name.');
q.equals(commit.committer.email, 'committer@example.com', 'Got correct committer email.');
q.end();
});
});
});

t.test('repo.writeChinese', function(q) {
repo.write('master', '中文测试.md', 'THIS IS A TEST', 'Creating test', function(err) {
q.error(err);
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载