Skip to content

Commit cce1182

Browse files
committed
Merge pull request #144 from michael/darvin-master
Adds Create/Delete Repo Ability
2 parents 71a3cb7 + af1e5b5 commit cce1182

File tree

4 files changed

+111
-108
lines changed

4 files changed

+111
-108
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ Show repository information
6363
repo.show(function(err, repo) {});
6464
```
6565

66+
Delete a repository
67+
68+
```js
69+
repo.deleteRepo(function(err, res) {});
70+
```
71+
6672
Get contents at a particular path in a particular branch. Set sync to true to get contents via sync method.
6773

6874
```js
@@ -78,7 +84,7 @@ repo.fork(function(err) {});
7884
Create new branch for repo. You can omit oldBranchName to default to "master".
7985

8086
```js
81-
repo.branch(oldBranchName, newBranchName, function(err) {});
87+
repo.branch(oldBranchName, newBranchName, function(err) {});
8288
```
8389

8490
Create Pull Request.
@@ -208,6 +214,15 @@ List public repositories for a particular user.
208214
user.userRepos(username, function(err, repos) {});
209215
```
210216

217+
Create a new repo for the authenticated user
218+
219+
```js
220+
user.createRepo({"name": "test"}, function(err, res) {});
221+
```
222+
Repo description, homepage, private/public can also be set.
223+
For a full list of options see the docs [here](https://developer.github.com/v3/repos/#create)
224+
225+
211226
List repositories for a particular organization. Includes private repositories if you are authorized.
212227

213228
```js
@@ -234,7 +249,7 @@ gist.read(function(err, gist) {
234249
});
235250
```
236251

237-
Updating the contents of a Gist. Please consult the documentation on [GitHub](http://developer.github.com/v3/gists/).
252+
Updating the contents of a Gist. Please consult the documentation on [GitHub](http://developer.github.com/v3/gists/).
238253

239254
```js
240255
var delta = {
@@ -255,7 +270,7 @@ var delta = {
255270
};
256271

257272
gist.update(delta, function(err, gist) {
258-
273+
259274
});
260275
```
261276
## Issues API
@@ -264,7 +279,7 @@ gist.update(delta, function(err, gist) {
264279
var issues = github.getIssues(username, reponame);
265280
```
266281

267-
To read all the issues of a given repository
282+
To read all the issues of a given repository
268283

269284
```js
270285
issues.list(options, function(err, issues) {});
@@ -313,7 +328,7 @@ Adds support for organizations and fixes an encoding issue.
313328

314329
### 0.5.X
315330

316-
Smart caching of latest commit sha.
331+
Smart caching of latest commit sha.
317332

318333
### 0.4.X
319334

github.js

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
}
101101

102102

103-
104103
// User API
105104
// =======
106105

@@ -196,8 +195,14 @@
196195
cb(err, res);
197196
});
198197
};
199-
};
200198

199+
// Create a repo
200+
// -------
201+
this.createRepo = function(options, cb) {
202+
_request("POST", "/user/repos", options, cb);
203+
};
204+
205+
};
201206

202207
// Repository API
203208
// =======
@@ -214,6 +219,14 @@
214219
"sha": null
215220
};
216221

222+
223+
// Delete a repo
224+
// --------
225+
226+
this.deleteRepo = function(cb) {
227+
_request("DELETE", repoPath, options, cb);
228+
};
229+
217230
// Uses the cache if branch has not been changed
218231
// -------
219232

@@ -333,14 +346,10 @@
333346
// -------
334347

335348
this.getSha = function(branch, path, cb) {
336-
// Just use head if path is empty
337-
if (path === "") return that.getRef("heads/"+branch, cb);
338-
that.getTree(branch+"?recursive=true", function(err, tree) {
349+
if (!path || path === "") return that.getRef("heads/"+branch, cb);
350+
_request("GET", repoPath + "/contents/"+path, {ref: branch}, function(err, pathContent) {
339351
if (err) return cb(err);
340-
var file = _.select(tree, function(file) {
341-
return file.path === path;
342-
})[0];
343-
cb(null, file ? file.sha : null);
352+
cb(null, pathContent.sha);
344353
});
345354
};
346355

@@ -454,8 +463,8 @@
454463
// Get contents
455464
// --------
456465

457-
this.contents = function(branch, path, cb, sync) {
458-
return _request("GET", repoPath + "/contents?ref=" + branch + (path ? "&path=" + path : ""), null, cb, 'raw', sync);
466+
this.contents = function(ref, path, cb) {
467+
_request("GET", repoPath + "/contents/"+path, { ref: ref }, cb);
459468
};
460469

461470
// Fork repository
@@ -529,34 +538,29 @@
529538
// -------
530539

531540
this.read = function(branch, path, cb) {
532-
that.getSha(branch, path, function(err, sha) {
533-
if (!sha) return cb("not found", null);
534-
that.getBlob(sha, function(err, content) {
535-
cb(err, content, sha);
536-
});
541+
_request("GET", repoPath + "/contents/"+path, {ref: branch}, function(err, obj) {
542+
if (err && err.error === 404) return cb("not found", null, null);
543+
544+
if (err) return cb(err);
545+
var sha = obj.sha,
546+
content = atob(obj.content);
547+
548+
cb(null, content, sha);
537549
});
538550
};
539551

540-
// Remove a file from the tree
552+
553+
// Remove a file
541554
// -------
542555

543556
this.remove = function(branch, path, cb) {
544-
updateTree(branch, function(err, latestCommit) {
545-
that.getTree(latestCommit+"?recursive=true", function(err, tree) {
546-
// Update Tree
547-
var newTree = _.reject(tree, function(ref) { return ref.path === path; });
548-
_.each(newTree, function(ref) {
549-
if (ref.type === "tree") delete ref.sha;
550-
});
551-
552-
that.postTree(newTree, function(err, rootTree) {
553-
that.commit(latestCommit, rootTree, 'Deleted '+path , function(err, commit) {
554-
that.updateHead(branch, commit, function(err) {
555-
cb(err);
556-
});
557-
});
558-
});
559-
});
557+
that.getSha(branch, path, function(err, sha) {
558+
if (err) return cb(err);
559+
_request("DELETE", repoPath + "/contents/" + path, {
560+
message: path + " is removed",
561+
sha: sha,
562+
branch: branch
563+
}, cb);
560564
});
561565
};
562566

@@ -604,18 +608,14 @@
604608
// -------
605609

606610
this.write = function(branch, path, content, message, cb) {
607-
updateTree(branch, function(err, latestCommit) {
608-
if (err) return cb(err);
609-
that.postBlob(content, function(err, blob) {
610-
if (err) return cb(err);
611-
that.updateTree(latestCommit, path, blob, function(err, tree) {
612-
if (err) return cb(err);
613-
that.commit(latestCommit, tree, message, function(err, commit) {
614-
if (err) return cb(err);
615-
that.updateHead(branch, commit, cb);
616-
});
617-
});
618-
});
611+
that.getSha(branch, path, function(err, sha) {
612+
if (err && err.error!=404) return cb(err);
613+
_request("PUT", repoPath + "/contents/" + path, {
614+
message: message,
615+
content: btoa(content),
616+
branch: branch,
617+
sha: sha
618+
}, cb);
619619
});
620620
};
621621

package.json

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -36,63 +36,14 @@
3636
"testling": {
3737
"files": "test/test.*.js",
3838
"browsers": [
39-
"iexplore/10.0",
40-
"iexplore/9.0",
41-
"chrome/6.0",
42-
"chrome/7.0",
43-
"chrome/8.0",
44-
"chrome/9.0",
45-
"chrome/10.0",
46-
"chrome/11.0",
47-
"chrome/12.0",
48-
"chrome/13.0",
49-
"chrome/14.0",
50-
"chrome/15.0",
51-
"chrome/16.0",
52-
"chrome/17.0",
53-
"chrome/18.0",
54-
"chrome/19.0",
55-
"chrome/20.0",
56-
"chrome/21.0",
57-
"chrome/22.0",
58-
"chrome/23.0",
59-
"chrome/24.0",
60-
"chrome/25.0",
61-
"firefox/3.0",
62-
"firefox/3.5",
63-
"firefox/3.6",
64-
"firefox/4.0",
65-
"firefox/5.0",
66-
"firefox/6.0",
67-
"firefox/7.0",
68-
"firefox/8.0",
69-
"firefox/9.0",
70-
"firefox/10.0",
71-
"firefox/11.0",
72-
"firefox/12.0",
73-
"firefox/13.0",
74-
"firefox/14.0",
75-
"firefox/15.0",
76-
"firefox/16.0",
77-
"firefox/17.0",
78-
"firefox/18.0",
79-
"firefox/19.0",
80-
"opera/10.0",
81-
"opera/10.5",
82-
"opera/11.0",
83-
"opera/11.5",
84-
"opera/11.6",
85-
"opera/12.0",
86-
"safari/4.0",
87-
"safari/5.0.5",
88-
"safari/5.1",
89-
"firefox/nightly",
90-
"opera/next",
91-
"chrome/canary",
92-
"iphone/6.0",
93-
"ipad/6.0",
94-
"safari/6.0",
95-
"android-browser/4.2"
39+
"iexplore/9.0..latest",
40+
"chrome/18.0..latest",
41+
"firefox/15.0..latest",
42+
"opera/11.0..latest",
43+
"safari/5.0.5..latest",
44+
"iphone/6.0..latest",
45+
"ipad/6.0..latest",
46+
"android-browser/4.2..latest"
9647
]
9748
}
9849
}

test/test.repo.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,44 @@ test("Repo API", function(t) {
4747

4848
});
4949

50-
test('Repo Returns commit errors correctly', function(t){
50+
var repoTest = Date.now();
51+
52+
test('Create Repo', function(t) {
53+
var timeout = setTimeout(function () { t.fail(); }, 10000);
54+
var github = new Github({
55+
username: test_user.USERNAME,
56+
password: test_user.PASSWORD,
57+
auth: "basic"
58+
});
59+
var user = github.getUser();
60+
61+
user.createRepo({ "name": repoTest }, function (err, res) {
62+
t.error(err);
63+
t.equals(res.name, repoTest.toString(), 'Repo created');
64+
clearTimeout(timeout);
65+
t.end();
66+
});
67+
68+
});
69+
70+
test('delete Repo', function(t) {
71+
var timeout = setTimeout(function () { t.fail(); }, 10000);
72+
var github = new Github({
73+
username: test_user.USERNAME,
74+
password: test_user.PASSWORD,
75+
auth: "basic"
76+
});
77+
var repo = github.getRepo(test_user.USERNAME, repoTest);
78+
repo.deleteRepo(function(err, res) {
79+
t.error(err);
80+
t.equals(res, true, 'Repo Deleted');
81+
clearTimeout(timeout);
82+
t.end();
83+
});
84+
85+
});
86+
87+
test('Repo Returns commit errors correctly', function(t) {
5188
var timeout = setTimeout(function () { t.fail(); }, 10000);
5289
var github = new Github({
5390
username: test_user.USERNAME,

0 commit comments

Comments
 (0)