diff --git a/github.js b/github.js index bc63ab0d..4dd52d39 100644 --- a/github.js +++ b/github.js @@ -54,7 +54,7 @@ } } }; - xhr.setRequestHeader('Accept','application/vnd.github.raw+json'); + xhr.setRequestHeader('Accept','application/vnd.github.v3.raw+json'); xhr.setRequestHeader('Content-Type','application/json;charset=UTF-8'); if ((options.token) || (options.username && options.password)) { xhr.setRequestHeader('Authorization', options.token @@ -107,7 +107,7 @@ }; // List user organizations - // ------- + // -------+ this.orgs = function(cb) { _request("GET", "/user/orgs", null, function(err, res) { @@ -116,7 +116,7 @@ }; // List authenticated user's gists - // ------- + // -------+ this.gists = function(cb) { _request("GET", "/gists", null, function(err, res) { @@ -125,7 +125,7 @@ }; // List authenticated user's unread notifications - // ------- + // -------+ this.notifications = function(cb) { _request("GET", "/notifications", null, function(err, res) { @@ -134,10 +134,15 @@ }; // Show user information - // ------- + // -------+ + // username can be omitted or '' or null or undefined this.show = function(username, cb) { - var command = username ? "/users/"+username : "/user"; + if(arguments.length === 1 && typeof arguments[0] === "function") { + cb = username; + username = options.username; + } + var command = username ? "/users/" + username : "/user"; _request("GET", command, null, function(err, res) { cb(err, res); @@ -148,8 +153,13 @@ // ------- this.userRepos = function(username, cb) { + if(arguments.length === 1 && typeof arguments[0] === "function") { + cb = username; + username = options.username; + } + var command = username ? "/users/" + username : '/user'; // Github does not always honor the 1000 limit so we want to iterate over the data set. - _requestAllPages("/users/"+username+"/repos?type=all&per_page=1000&sort=updated", function(err, res) { + _requestAllPages(command + "/repos?type=all&per_page=1000&sort=updated", function(err, res) { cb(err, res); }); }; @@ -158,7 +168,12 @@ // ------- this.userGists = function(username, cb) { - _request("GET", "/users/"+username+"/gists", null, function(err, res) { + if(arguments.length === 1 && typeof arguments[0] === "function") { + cb = username; + username = options.username; + } + var command = username ? "/users/" + username : ''; + _request("GET", command + "/gists", null, function(err, res) { cb(err,res); }); }; @@ -197,7 +212,7 @@ // ======= Github.Repository = function(options) { - var repo = options.name; + var repo = options.repo; var user = options.user; var that = this; @@ -209,7 +224,7 @@ }; // Uses the cache if branch has not been changed - // ------- + // -------+ function updateTree(branch, cb) { if (branch === currentTree.branch && currentTree.sha) return cb(null, currentTree.sha); @@ -221,7 +236,7 @@ } // Get a particular reference - // ------- + // -------+ this.getRef = function(ref, cb) { _request("GET", repoPath + "/git/refs/" + ref, null, function(err, res) { @@ -231,7 +246,7 @@ }; // Create a new reference - // -------- + // --------+ // // { // "ref": "refs/heads/my-new-branch-name", @@ -253,21 +268,21 @@ }; // Create a repo - // ------- + // -------+ this.createRepo = function(options, cb) { _request("POST", "/user/repos", options, cb); }; // Delete a repo - // -------- + // --------+ this.deleteRepo = function(cb) { _request("DELETE", repoPath, options, cb); }; // List all tags of a repository - // ------- + // -------+ this.listTags = function(cb) { _request("GET", repoPath + "/tags", null, function(err, tags) { @@ -277,7 +292,7 @@ }; // List all pull requests of a respository - // ------- + // -------+ this.listPulls = function(state, cb) { _request("GET", repoPath + "/pulls" + (state ? '?state=' + state : ''), null, function(err, pulls) { @@ -287,7 +302,7 @@ }; // Gets details for a specific pull request - // ------- + // -------+ this.getPull = function(number, cb) { _request("GET", repoPath + "/pulls/" + number, null, function(err, pull) { @@ -307,24 +322,28 @@ }; // List all branches of a repository - // ------- + // -------+ this.listBranches = function(cb) { - _request("GET", repoPath + "/git/refs/heads", null, function(err, heads) { + // _request("GET", repoPath + "/git/refs/heads", null, function(err, heads) { + // if (err) return cb(err); + // cb(null, _.map(heads, function(head) { return _.last(head.ref.split('/')); })); + // }); + _request("GET", repoPath + "/branches", null, function(err, branches) { if (err) return cb(err); - cb(null, _.map(heads, function(head) { return _.last(head.ref.split('/')); })); + cb(null, _.map(branches, function(branch){return branch.name})); }); }; // Retrieve the contents of a blob - // ------- + // -------+ this.getBlob = function(sha, cb) { _request("GET", repoPath + "/git/blobs/" + sha, null, cb, 'raw'); }; // For a given file path, get the corresponding sha (blob for files, tree for dirs) - // ------- + // -------+ this.getSha = function(branch, path, cb) { // Just use head if path is empty @@ -339,7 +358,7 @@ }; // Retrieve the tree a commit points to - // ------- + // -------+ this.getTree = function(tree, cb) { _request("GET", repoPath + "/git/trees/"+tree, null, function(err, res) { @@ -349,7 +368,7 @@ }; // Post a new blob object, getting a blob SHA back - // ------- + // -------+ this.postBlob = function(content, cb) { if (typeof(content) === "string") { @@ -371,7 +390,7 @@ }; // Update an existing tree adding a new blob object getting a tree SHA back - // ------- + // -------+ this.updateTree = function(baseTree, path, blob, cb) { var data = { @@ -393,7 +412,7 @@ // Post a new tree object having a file path pointer replaced // with a new blob SHA getting a tree SHA back - // ------- + // -------+ this.postTree = function(tree, cb) { _request("POST", repoPath + "/git/trees", { "tree": tree }, function(err, res) { @@ -404,13 +423,13 @@ // Create a new commit object with the current commit SHA as the parent // and the new tree SHA, getting a commit SHA back - // ------- + // -------+ this.commit = function(parent, tree, message, cb) { var data = { "message": message, "author": { - "name": options.username + "name": options.user }, "parents": [ parent @@ -426,7 +445,7 @@ }; // Update the reference of your head to point to the new commit SHA - // ------- + // -------+ this.updateHead = function(head, commit, cb) { _request("PATCH", repoPath + "/git/refs/heads/" + head, { "sha": commit }, function(err, res) { @@ -435,28 +454,28 @@ }; // Show repository information - // ------- + // -------+ this.show = function(cb) { _request("GET", repoPath, null, cb); }; // Get contents - // -------- + // --------+ this.contents = function(branch, path, cb, sync) { - return _request("GET", repoPath + "/contents?ref=" + branch + (path ? "&path=" + path : ""), null, cb, 'raw', sync); + return _request("GET", repoPath + "/contents" + (path ? "/" + path : "") + "?ref=" + branch, null, cb, 'raw', sync); }; // Fork repository - // ------- + // -------+ this.fork = function(cb) { _request("POST", repoPath + "/forks", null, cb); }; // Branch repository - // -------- + // --------+ this.branch = function(oldBranch,newBranch,cb) { if(arguments.length === 2 && typeof arguments[1] === "function") { @@ -474,7 +493,8 @@ } // Create pull request - // -------- + // https://developer.github.com/v3/pulls/#create-a-pull-request + // --------+ this.createPullRequest = function(options, cb) { _request("POST", repoPath + "/pulls", options, cb); @@ -488,35 +508,35 @@ }; // Get a hook - // -------- + // --------+ this.getHook = function(id, cb) { _request("GET", repoPath + "/hooks/" + id, null, cb); }; // Create a hook - // -------- + // --------+ this.createHook = function(options, cb) { _request("POST", repoPath + "/hooks", options, cb); }; // Edit a hook - // -------- + // --------+ this.editHook = function(id, options, cb) { _request("PATCH", repoPath + "/hooks/" + id, options, cb); }; // Delete a hook - // -------- + // --------+ this.deleteHook = function(id, cb) { _request("DELETE", repoPath + "/hooks/" + id, null, cb); }; // Read file at given path - // ------- + // -------+ this.read = function(branch, path, cb) { that.getSha(branch, path, function(err, sha) { @@ -528,11 +548,11 @@ }; // Remove a file from the tree - // ------- + // -------+ // TODO: when newTree is [] this.remove = function(branch, path, cb) { updateTree(branch, function(err, latestCommit) { - that.getTree(latestCommit+"?recursive=true", function(err, tree) { + that.getTree(latestCommit+"?recursive=1", function(err, tree) { // Update Tree var newTree = _.reject(tree, function(ref) { return ref.path === path; }); _.each(newTree, function(ref) { @@ -551,7 +571,7 @@ }; // Delete a file from the tree - // ------- + // -------+ this.delete = function(branch, path, cb) { that.getSha(branch, path, function(err, sha) { @@ -559,16 +579,15 @@ var delPath = repoPath + "/contents/" + path; var params = { "message": "Deleted " + path, - "sha": sha + "sha": sha, + "branch": branch }; - delPath += "?message=" + encodeURIComponent(params.message); - delPath += "&sha=" + encodeURIComponent(params.sha); - _request("DELETE", delPath, null, cb); + _request("DELETE", delPath, params, cb); }) } // Move a file to a new location - // ------- + // -------+ this.move = function(branch, path, newPath, cb) { updateTree(branch, function(err, latestCommit) { @@ -591,7 +610,7 @@ }; // Write file contents to a given branch and path - // ------- + // -------+ this.write = function(branch, path, content, message, cb) { updateTree(branch, function(err, latestCommit) { @@ -614,10 +633,14 @@ // path: Only commits containing this file path will be returned // since: ISO 8601 date - only commits after this date will be returned // until: ISO 8601 date - only commits before this date will be returned - // ------- + // https://developer.github.com/v3/repos/commits/ + // -------+ this.getCommits = function(options, cb) { - options = options || {}; + if(arguments.length === 1 && typeof arguments[0] === "function") { + cb = options; + options = {}; + } var url = repoPath + "/commits"; var params = []; if (options.sha) { @@ -648,6 +671,7 @@ }; // Gists API + // https://developer.github.com/v3/gists/ // ======= Github.Gist = function(options) { @@ -655,7 +679,7 @@ var gistPath = "/gists/"+id; // Read the gist - // -------- + // --------+ this.read = function(cb) { _request("GET", gistPath, null, function(err, gist) { @@ -664,9 +688,9 @@ }; // Create the gist - // -------- + // --------+ // { - // "description": "the description for this gist", + // "description": "the description for this gist", // "public": true, // "files": { // "file1.txt": { @@ -680,7 +704,7 @@ }; // Delete the gist - // -------- + // --------+ this.delete = function(cb) { _request("DELETE", gistPath, null, function(err,res) { @@ -689,16 +713,33 @@ }; // Fork a gist - // -------- + // --------+ this.fork = function(cb) { - _request("POST", gistPath+"/fork", null, function(err,res) { + _request("POST", gistPath+"/forks", null, function(err,res) { cb(err,res); }); }; // Update a gist with the new stuff - // -------- + // options + // { + // "description": "the description for this gist", + // "files": { + // "file1.txt": { + // "content": "updated file contents" + // }, + // "old_name.txt": { + // "filename": "new_name.txt", + // "content": "modified contents" + // }, + // "new_file.txt": { + // "content": "a new file" + // }, + // "delete_this_file.txt": null + // } + // } + // --------+ this.update = function(options, cb) { _request("PATCH", gistPath, options, function(err,res) { @@ -707,7 +748,7 @@ }; // Star a gist - // -------- + // --------+ this.star = function(cb) { _request("PUT", gistPath+"/star", null, function(err,res) { @@ -716,7 +757,7 @@ }; // Untar a gist - // -------- + // --------+ this.unstar = function(cb) { _request("DELETE", gistPath+"/star", null, function(err,res) { @@ -725,7 +766,7 @@ }; // Check if a gist is starred - // -------- + // --------+ this.isStarred = function(cb) { _request("GET", gistPath+"/star", null, function(err,res) { @@ -755,7 +796,7 @@ }; this.getRepo = function(user, repo) { - return new Github.Repository({user: user, name: repo}); + return new Github.Repository({user: user, repo: repo}); }; this.getUser = function() {