Skip to content

Commit 19945bb

Browse files
committed
Improve commit method
2 parents e4ffdb4 + ccd71af commit 19945bb

File tree

4 files changed

+70
-15
lines changed

4 files changed

+70
-15
lines changed

github.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -414,21 +414,25 @@
414414
// -------
415415

416416
this.commit = function(parent, tree, message, cb) {
417-
var data = {
418-
"message": message,
419-
"author": {
420-
"name": options.username
421-
},
422-
"parents": [
423-
parent
424-
],
425-
"tree": tree
426-
};
427-
428-
_request("POST", repoPath + "/git/commits", data, function(err, res) {
429-
currentTree.sha = res.sha; // update latest commit
417+
var user = new Github.User();
418+
user.show(null, function(err, userData){
430419
if (err) return cb(err);
431-
cb(null, res.sha);
420+
var data = {
421+
"message": message,
422+
"author": {
423+
"name": options.user,
424+
"email": userData.email
425+
},
426+
"parents": [
427+
parent
428+
],
429+
"tree": tree
430+
};
431+
_request("POST", repoPath + "/git/commits", data, function(err, res) {
432+
if (err) return cb(err);
433+
currentTree.sha = res.sha; // update latest commit
434+
cb(null, res.sha);
435+
});
432436
});
433437
};
434438

test/commit_test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
if (typeof require !== 'undefined') {
2+
var Github = require("../")
3+
, chai = require("chai");
4+
}
5+
chai.should();
6+
var expect = chai.expect
7+
, TEST_USERNAME = "mikedeboertest"
8+
, TEST_PASSWORD = "test1324"
9+
, TEST_REPO = "github";
10+
11+
var github = new Github({
12+
username: TEST_USERNAME,
13+
password: TEST_PASSWORD,
14+
auth: "basic"
15+
});
16+
17+
describe("Repo", function(){
18+
19+
it("should return commit errors correctly", function(done){
20+
var repo = github.getRepo(TEST_USERNAME, TEST_REPO);
21+
repo.commit("broken-parent-hash", "broken-tree-hash", "commit message", function(err){
22+
if (err != null && err.request != null && err.request.status == 500) {
23+
done();
24+
} else {
25+
done(new Error("No `err` passed to .commit callback"));
26+
}
27+
});
28+
});
29+
30+
});

test/test.repo.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,23 @@ test("Repo API", function(t) {
4646
t.end();
4747

4848
});
49+
50+
test('Repo Returns commit errors correctly', function(t){
51+
var timeout = setTimeout(function () { t.fail(); }, 10000);
52+
var github = new Github({
53+
username: test_user.USERNAME,
54+
password: test_user.PASSWORD,
55+
auth: "basic"
56+
});
57+
var repo = github.getRepo(test_user.USERNAME, test_user.REPO);
58+
59+
repo.commit("broken-parent-hash", "broken-tree-hash", "commit message", function(err){
60+
t.ok(err, 'error thrown for bad commit');
61+
t.ok(err.request);
62+
t.equals(err.request.status, 422, 'Returns 422 status');
63+
clearTimeout(timeout);
64+
t.end();
65+
});
66+
67+
68+
});

test/user.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"USERNAME": "mikedeboertest",
3-
"PASSWORD": "test1324"
3+
"PASSWORD": "test1324",
4+
"REPO": "github"
45
}

0 commit comments

Comments
 (0)