diff --git a/README.md b/README.md index 11182b6a..a7439dd9 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,12 @@ Show repository information repo.show(function(err, repo) {}); ``` +Delete a repository + +```js +repo.deleteRepo(function(err, res) {}); +``` + Get contents at a particular path in a particular branch. Set sync to true to get contents via sync method. ```js @@ -78,7 +84,7 @@ repo.fork(function(err) {}); Create new branch for repo. You can omit oldBranchName to default to "master". ```js -repo.branch(oldBranchName, newBranchName, function(err) {}); +repo.branch(oldBranchName, newBranchName, function(err) {}); ``` Create Pull Request. @@ -208,6 +214,15 @@ List public repositories for a particular user. user.userRepos(username, function(err, repos) {}); ``` +Create a new repo for the authenticated user + +```js +user.createRepo({"name": "test"}, function(err, res) {}); +``` +Repo description, homepage, private/public can also be set. +For a full list of options see the docs [here](https://developer.github.com/v3/repos/#create) + + List repositories for a particular organization. Includes private repositories if you are authorized. ```js @@ -234,7 +249,7 @@ gist.read(function(err, gist) { }); ``` -Updating the contents of a Gist. Please consult the documentation on [GitHub](http://developer.github.com/v3/gists/). +Updating the contents of a Gist. Please consult the documentation on [GitHub](http://developer.github.com/v3/gists/). ```js var delta = { @@ -255,7 +270,7 @@ var delta = { }; gist.update(delta, function(err, gist) { - + }); ``` ## Issues API @@ -264,7 +279,7 @@ gist.update(delta, function(err, gist) { var issues = github.getIssues(username, reponame); ``` -To read all the issues of a given repository +To read all the issues of a given repository ```js issues.list(options, function(err, issues) {}); @@ -313,7 +328,7 @@ Adds support for organizations and fixes an encoding issue. ### 0.5.X -Smart caching of latest commit sha. +Smart caching of latest commit sha. ### 0.4.X diff --git a/github.js b/github.js index 82d0dc00..64bdc112 100644 --- a/github.js +++ b/github.js @@ -100,7 +100,6 @@ } - // User API // ======= @@ -196,8 +195,14 @@ cb(err, res); }); }; - }; + // Create a repo + // ------- + this.createRepo = function(options, cb) { + _request("POST", "/user/repos", options, cb); + }; + + }; // Repository API // ======= @@ -214,6 +219,14 @@ "sha": null }; + + // Delete a repo + // -------- + + this.deleteRepo = function(cb) { + _request("DELETE", repoPath, options, cb); + }; + // Uses the cache if branch has not been changed // ------- @@ -333,14 +346,10 @@ // ------- this.getSha = function(branch, path, cb) { - // Just use head if path is empty - if (path === "") return that.getRef("heads/"+branch, cb); - that.getTree(branch+"?recursive=true", function(err, tree) { + if (!path || path === "") return that.getRef("heads/"+branch, cb); + _request("GET", repoPath + "/contents/"+path, {ref: branch}, function(err, pathContent) { if (err) return cb(err); - var file = _.select(tree, function(file) { - return file.path === path; - })[0]; - cb(null, file ? file.sha : null); + cb(null, pathContent.sha); }); }; @@ -454,8 +463,8 @@ // Get contents // -------- - this.contents = function(branch, path, cb, sync) { - return _request("GET", repoPath + "/contents?ref=" + branch + (path ? "&path=" + path : ""), null, cb, 'raw', sync); + this.contents = function(ref, path, cb) { + _request("GET", repoPath + "/contents/"+path, { ref: ref }, cb); }; // Fork repository @@ -529,34 +538,29 @@ // ------- this.read = function(branch, path, cb) { - that.getSha(branch, path, function(err, sha) { - if (!sha) return cb("not found", null); - that.getBlob(sha, function(err, content) { - cb(err, content, sha); - }); + _request("GET", repoPath + "/contents/"+path, {ref: branch}, function(err, obj) { + if (err && err.error === 404) return cb("not found", null, null); + + if (err) return cb(err); + var sha = obj.sha, + content = atob(obj.content); + + cb(null, content, sha); }); }; - // Remove a file from the tree + + // Remove a file // ------- this.remove = function(branch, path, cb) { - updateTree(branch, function(err, latestCommit) { - that.getTree(latestCommit+"?recursive=true", function(err, tree) { - // Update Tree - var newTree = _.reject(tree, function(ref) { return ref.path === path; }); - _.each(newTree, function(ref) { - if (ref.type === "tree") delete ref.sha; - }); - - that.postTree(newTree, function(err, rootTree) { - that.commit(latestCommit, rootTree, 'Deleted '+path , function(err, commit) { - that.updateHead(branch, commit, function(err) { - cb(err); - }); - }); - }); - }); + that.getSha(branch, path, function(err, sha) { + if (err) return cb(err); + _request("DELETE", repoPath + "/contents/" + path, { + message: path + " is removed", + sha: sha, + branch: branch + }, cb); }); }; @@ -604,18 +608,14 @@ // ------- this.write = function(branch, path, content, message, cb) { - updateTree(branch, function(err, latestCommit) { - if (err) return cb(err); - that.postBlob(content, function(err, blob) { - if (err) return cb(err); - that.updateTree(latestCommit, path, blob, function(err, tree) { - if (err) return cb(err); - that.commit(latestCommit, tree, message, function(err, commit) { - if (err) return cb(err); - that.updateHead(branch, commit, cb); - }); - }); - }); + that.getSha(branch, path, function(err, sha) { + if (err && err.error!=404) return cb(err); + _request("PUT", repoPath + "/contents/" + path, { + message: message, + content: btoa(content), + branch: branch, + sha: sha + }, cb); }); }; diff --git a/package.json b/package.json index 1fe90b1b..3bd5c54e 100644 --- a/package.json +++ b/package.json @@ -36,63 +36,14 @@ "testling": { "files": "test/test.*.js", "browsers": [ - "iexplore/10.0", - "iexplore/9.0", - "chrome/6.0", - "chrome/7.0", - "chrome/8.0", - "chrome/9.0", - "chrome/10.0", - "chrome/11.0", - "chrome/12.0", - "chrome/13.0", - "chrome/14.0", - "chrome/15.0", - "chrome/16.0", - "chrome/17.0", - "chrome/18.0", - "chrome/19.0", - "chrome/20.0", - "chrome/21.0", - "chrome/22.0", - "chrome/23.0", - "chrome/24.0", - "chrome/25.0", - "firefox/3.0", - "firefox/3.5", - "firefox/3.6", - "firefox/4.0", - "firefox/5.0", - "firefox/6.0", - "firefox/7.0", - "firefox/8.0", - "firefox/9.0", - "firefox/10.0", - "firefox/11.0", - "firefox/12.0", - "firefox/13.0", - "firefox/14.0", - "firefox/15.0", - "firefox/16.0", - "firefox/17.0", - "firefox/18.0", - "firefox/19.0", - "opera/10.0", - "opera/10.5", - "opera/11.0", - "opera/11.5", - "opera/11.6", - "opera/12.0", - "safari/4.0", - "safari/5.0.5", - "safari/5.1", - "firefox/nightly", - "opera/next", - "chrome/canary", - "iphone/6.0", - "ipad/6.0", - "safari/6.0", - "android-browser/4.2" + "iexplore/9.0..latest", + "chrome/18.0..latest", + "firefox/15.0..latest", + "opera/11.0..latest", + "safari/5.0.5..latest", + "iphone/6.0..latest", + "ipad/6.0..latest", + "android-browser/4.2..latest" ] } } diff --git a/test/test.repo.js b/test/test.repo.js index b14048bc..79a7196d 100644 --- a/test/test.repo.js +++ b/test/test.repo.js @@ -47,7 +47,44 @@ test("Repo API", function(t) { }); -test('Repo Returns commit errors correctly', function(t){ +var repoTest = Date.now(); + +test('Create Repo', function(t) { + var timeout = setTimeout(function () { t.fail(); }, 10000); + var github = new Github({ + username: test_user.USERNAME, + password: test_user.PASSWORD, + auth: "basic" + }); + var user = github.getUser(); + + user.createRepo({ "name": repoTest }, function (err, res) { + t.error(err); + t.equals(res.name, repoTest.toString(), 'Repo created'); + clearTimeout(timeout); + t.end(); + }); + +}); + +test('delete Repo', function(t) { + var timeout = setTimeout(function () { t.fail(); }, 10000); + var github = new Github({ + username: test_user.USERNAME, + password: test_user.PASSWORD, + auth: "basic" + }); + var repo = github.getRepo(test_user.USERNAME, repoTest); + repo.deleteRepo(function(err, res) { + t.error(err); + t.equals(res, true, 'Repo Deleted'); + clearTimeout(timeout); + t.end(); + }); + +}); + +test('Repo Returns commit errors correctly', function(t) { var timeout = setTimeout(function () { t.fail(); }, 10000); var github = new Github({ username: test_user.USERNAME,