Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github-api

Package Overview
Dependencies
Maintainers
2
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github-api - npm Package Compare versions

Comparing version 0.7.0 to 0.10.1

test/test.auth.js

460

github.js

@@ -1,6 +0,12 @@

// Github.js 0.8.0
// (c) 2013 Michael Aufreiter, Development Seed
// Github.js is freely distributable under the MIT license.
// For all details and documentation:
// http://substance.io/michael/github
/*!
* @overview Github.js
*
* @copyright (c) 2013 Michael Aufreiter, Development Seed
* Github.js is freely distributable.
*
* @license Licensed under MIT license
*
* For all details and documentation:
* http://substance.io/michael/github
*/

@@ -12,10 +18,9 @@ (function() {

var XMLHttpRequest, Base64, _;
var XMLHttpRequest, _;
if (typeof exports !== 'undefined') {
XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
_ = require('underscore');
Base64 = require('./lib/base64.js');
}else{
btoa = require('btoa');
} else {
_ = window._;
Base64 = window.Base64;
}

@@ -27,3 +32,3 @@ //prefer native XMLHttpRequest always

var API_URL = 'https://api.github.com';

@@ -35,8 +40,8 @@

// =======
//
//
// I'm not proud of this and neither should you be if you were responsible for the XMLHttpRequest spec.
function _request(method, path, data, cb, raw) {
function _request(method, path, data, cb, raw, sync) {
function getURL() {
var url = API_URL + path;
var url = path.indexOf('//') >= 0 ? path : API_URL + path;
return url + ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime();

@@ -48,28 +53,55 @@ }

xhr.open(method, getURL());
xhr.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status >= 200 && this.status < 300 || this.status === 304) {
cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true);
xhr.open(method, getURL(), !sync);
if (!sync) {
xhr.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status >= 200 && this.status < 300 || this.status === 304) {
cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true, this);
} else {
cb({path: path, request: this, error: this.status});
}
}
};
}
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)) {
var authorization = options.token ? 'token ' + options.token : 'Basic ' + btoa(options.username + ':' + options.password);
xhr.setRequestHeader('Authorization', authorization);
}
if (data)
xhr.send(JSON.stringify(data));
else
xhr.send();
if (sync) return xhr.response;
}
function _requestAllPages(path, cb) {
var results = [];
(function iterate() {
_request("GET", path, null, function(err, res, xhr) {
if (err) {
return cb(err);
}
results.push.apply(results, res);
var links = (xhr.getResponseHeader('link') || '').split(/\s*,\s*/g),
next = _.find(links, function(link) { return /rel="next"/.test(link); });
if (next) {
next = (/<(.*)>/.exec(next) || [])[1];
}
if (!next) {
cb(err, results);
} else {
cb({request: this, error: this.status});
path = next;
iterate();
}
}
};
xhr.setRequestHeader('Accept','application/vnd.github.raw');
xhr.setRequestHeader('Content-Type','application/json');
if (
(options.auth == 'oauth' && options.token) ||
(options.auth == 'basic' && options.username && options.password)
) {
xhr.setRequestHeader('Authorization',options.auth == 'oauth'
? 'token '+ options.token
: 'Basic ' + Base64.encode(options.username + ':' + options.password)
);
}
data ? xhr.send(JSON.stringify(data)) : xhr.send();
});
})();
}
// User API

@@ -80,3 +112,4 @@ // =======

this.repos = function(cb) {
_request("GET", "/user/repos?type=all&per_page=1000&sort=updated", null, function(err, res) {
// Github does not always honor the 1000 limit so we want to iterate over the data set.
_requestAllPages("/user/repos?type=all&per_page=1000&sort=updated", function(err, res) {
cb(err, res);

@@ -104,2 +137,11 @@ });

// List authenticated user's unread notifications
// -------
this.notifications = function(cb) {
_request("GET", "/notifications", null, function(err, res) {
cb(err,res);
});
};
// Show user information

@@ -120,3 +162,4 @@ // -------

this.userRepos = function(username, cb) {
_request("GET", "/users/"+username+"/repos?type=all&per_page=1000&sort=updated", null, function(err, res) {
// 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) {
cb(err, res);

@@ -139,3 +182,4 @@ });

this.orgRepos = function(orgname, cb) {
_request("GET", "/orgs/"+orgname+"/repos?type=all&per_page=1000&sort=updated&direction=desc", null, function(err, res) {
// Github does not always honor the 1000 limit so we want to iterate over the data set.
_requestAllPages("/orgs/"+orgname+"/repos?type=all&&page_num=1000&sort=updated&direction=desc", function(err, res) {
cb(err, res);

@@ -162,5 +206,11 @@ });

};
// Create a repo
// -------
this.createRepo = function(options, cb) {
_request("POST", "/user/repos", options, cb);
};
};
// Repository API

@@ -172,3 +222,3 @@ // =======

var user = options.user;
var that = this;

@@ -182,2 +232,10 @@ var repoPath = "/repos/" + user + "/" + repo;

// Delete a repo
// --------
this.deleteRepo = function(cb) {
_request("DELETE", repoPath, options, cb);
};
// Uses the cache if branch has not been changed

@@ -219,3 +277,3 @@ // -------

// --------
//
//
// repo.deleteRef('heads/gh-pages')

@@ -228,2 +286,56 @@ // repo.deleteRef('tags/v1.0')

// 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) {
if (err) return cb(err);
cb(null, tags);
});
};
// List all pull requests of a respository
// -------
this.listPulls = function(state, cb) {
_request("GET", repoPath + "/pulls" + (state ? '?state=' + state : ''), null, function(err, pulls) {
if (err) return cb(err);
cb(null, pulls);
});
};
// Gets details for a specific pull request
// -------
this.getPull = function(number, cb) {
_request("GET", repoPath + "/pulls/" + number, null, function(err, pull) {
if (err) return cb(err);
cb(null, pull);
});
};
// Retrieve the changes made between base and head
// -------
this.compare = function(base, head, cb) {
_request("GET", repoPath + "/compare/" + base + "..." + head, null, function(err, diff) {
if (err) return cb(err);
cb(null, diff);
});
};
// List all branches of a repository

@@ -250,9 +362,6 @@ // -------

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) {
var file = _.select(tree, function(file) {
return file.path === path;
})[0];
cb(null, file ? file.sha : null);
if (!path || path === "") return that.getRef("heads/"+branch, cb);
_request("GET", repoPath + "/contents/"+path, {ref: branch}, function(err, pathContent) {
if (err) return cb(err);
cb(null, pathContent.sha);
});

@@ -280,3 +389,8 @@ };

};
}
} else {
content = {
"content": btoa(String.fromCharCode.apply(null, new Uint8Array(content))),
"encoding": "base64"
};
}

@@ -326,17 +440,21 @@ _request("POST", repoPath + "/git/blobs", content, function(err, res) {

this.commit = function(parent, tree, message, cb) {
var data = {
"message": message,
"author": {
"name": options.username
},
"parents": [
parent
],
"tree": tree
};
_request("POST", repoPath + "/git/commits", data, function(err, res) {
currentTree.sha = res.sha; // update latest commit
var user = new Github.User();
user.show(null, function(err, userData){
if (err) return cb(err);
cb(null, res.sha);
var data = {
"message": message,
"author": {
"name": options.user,
"email": userData.email
},
"parents": [
parent
],
"tree": tree
};
_request("POST", repoPath + "/git/commits", data, function(err, res) {
if (err) return cb(err);
currentTree.sha = res.sha; // update latest commit
cb(null, res.sha);
});
});

@@ -364,4 +482,4 @@ };

this.contents = function(branch, path, cb) {
_request("GET", repoPath + "/contents?ref=" + branch, { path: path }, cb);
this.contents = function(ref, path, cb) {
_request("GET", repoPath + "/contents/"+path, { ref: ref }, cb);
};

@@ -376,2 +494,20 @@

// Branch repository
// --------
this.branch = function(oldBranch,newBranch,cb) {
if(arguments.length === 2 && typeof arguments[1] === "function") {
cb = newBranch;
newBranch = oldBranch;
oldBranch = "master";
}
this.getRef("heads/" + oldBranch, function(err,ref) {
if(err && cb) return cb(err);
that.createRef({
ref: "refs/heads/" + newBranch,
sha: ref
},cb);
});
};
// Create pull request

@@ -384,2 +520,37 @@ // --------

// List hooks
// --------
this.listHooks = function(cb) {
_request("GET", repoPath + "/hooks", null, cb);
};
// 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

@@ -389,30 +560,42 @@ // -------

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.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);
});
};
that.postTree(newTree, function(err, rootTree) {
that.commit(latestCommit, rootTree, 'Deleted '+path , function(err, commit) {
that.updateHead(branch, commit, function(err) {
cb(err);
});
});
});
});
// Delete a file from the tree
// -------
this.delete = function(branch, path, cb) {
that.getSha(branch, path, function(err, sha) {
if (!sha) return cb("not found", null);
var delPath = repoPath + "/contents/" + path;
var params = {
"message": "Deleted " + path,
"sha": sha
};
delPath += "?message=" + encodeURIComponent(params.message);
delPath += "&sha=" + encodeURIComponent(params.sha);
_request("DELETE", delPath, null, cb);
});

@@ -448,16 +631,55 @@ };

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);
});
};
// List commits on a repository. Takes an object of optional paramaters:
// sha: SHA or branch to start listing commits from
// 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
// -------
this.getCommits = function(options, cb) {
options = options || {};
var url = repoPath + "/commits";
var params = [];
if (options.sha) {
params.push("sha=" + encodeURIComponent(options.sha));
}
if (options.path) {
params.push("path=" + encodeURIComponent(options.path));
}
if (options.since) {
var since = options.since;
if (since.constructor === Date) {
since = since.toISOString();
}
params.push("since=" + encodeURIComponent(since));
}
if (options.until) {
var until = options.until;
if (until.constructor === Date) {
until = until.toISOString();
}
params.push("until=" + encodeURIComponent(until));
}
if (options.page) {
params.push("page=" + options.page);
}
if (options.perpage) {
params.push("per_page=" + options.perpage);
}
if (params.length > 0) {
url += "?" + params.join("&");
}
_request("GET", url, null, cb);
};
};

@@ -492,3 +714,3 @@

// }
this.create = function(options, cb){

@@ -524,7 +746,49 @@ _request("POST","/gists", options, cb);

};
// Star a gist
// --------
this.star = function(cb) {
_request("PUT", gistPath+"/star", null, function(err,res) {
cb(err,res);
});
};
// Untar a gist
// --------
this.unstar = function(cb) {
_request("DELETE", gistPath+"/star", null, function(err,res) {
cb(err,res);
});
};
// Check if a gist is starred
// --------
this.isStarred = function(cb) {
_request("GET", gistPath+"/star", null, function(err,res) {
cb(err,res);
});
};
};
// Issues API
// ==========
Github.Issue = function(options) {
var path = "/repos/" + options.user + "/" + options.repo + "/issues";
this.list = function(options, cb) {
_request("GET", path, options, cb);
};
};
// Top Level API
// -------
this.getIssues = function(user, repo) {
return new Github.Issue({user: user, repo: repo});
};
this.getRepo = function(user, repo) {

@@ -531,0 +795,0 @@ return new Github.Repository({user: user, name: repo});

{
"name": "github-api",
"version": "0.7.0",
"version": "0.10.1",
"description": "A higher-level wrapper around the Github API.",
"main": "github.js",
"dependencies": {
"xmlhttprequest": "*",
"underscore": "*"
"btoa": "^1.1.2",
"underscore": "~1.6.0",
"xmlhttprequest": "~1.6.0"
},
"devDependencies": {
"mocha": "*",
"chai": "*"
"jshint": "^2.5.8",
"tape": "^3.0.3"
},
"scripts": {
"test": "mocha"
"test": "tape test/test.*.js && jshint github.js test/*.js"
},
"repository": {
"type": "git",
"url": "git://github.com/darvin/github.git"
"url": "git://github.com/michael/github.git"
},

@@ -37,65 +37,14 @@ "keywords": [

"testling": {
"files": "test/*_test.js",
"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"
],
"harness": "mocha"
"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"
]
}
}

@@ -1,2 +0,2 @@

[![browser support](https://ci.testling.com/darvin/github.png)](https://ci.testling.com/darvin/github)
[![browser support](https://ci.testling.com/darvin/github.png)](https://ci.testling.com/darvin/github)

@@ -6,2 +6,4 @@

[![Coverage Status](https://img.shields.io/coveralls/michael/github.svg)](https://coveralls.io/r/michael/github)
# Github.js

@@ -13,2 +15,10 @@

## Installation
Either grab `github.js` from this repo or install via NPM:
```
npm install github-api
```
## Usage

@@ -30,3 +40,3 @@

var github = new Github({
token: "OAUTH_TOKEN"
token: "OAUTH_TOKEN",
auth: "oauth"

@@ -36,2 +46,12 @@ });

You can use either:
* Authorised App Tokens (via client/secret pairs), used for bigger applications, created in web-flows/on the fly
* Personal Access Tokens (simpler to set up), used on command lines, scripts etc, created in GitHub web UI
See these pages for more info:
[Creating an access token for command-line use](https://help.github.com/articles/creating-an-access-token-for-command-line-use)
[Github API OAuth Overview] (http://developer.github.com/v3/oauth)
## Repository API

@@ -50,8 +70,14 @@

Get contents at a particular path.
Delete a repository
```js
repo.contents("path/to/dir", function(err, contents) {});
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
repo.contents(branch, "path/to/dir", function(err, contents) {}, sync);
```
Fork repository. This operation runs asynchronously. You may want to poll for `repo.contents` until the forked repo is ready.

@@ -63,2 +89,8 @@

Create new branch for repo. You can omit oldBranchName to default to "master".
```js
repo.branch(oldBranchName, newBranchName, function(err) {});
```
Create Pull Request.

@@ -71,3 +103,3 @@

base: "gh-pages",
head: "michael" + ":" + "prose-patch",
head: "michael" + ":" + "prose-patch"
};

@@ -171,5 +203,11 @@ repo.createPullRequest(pull, function(err, pullRequest) {});

```js
user.gists(username, function(err, gists) {});
user.gists(function(err, gists) {});
```
List unread notifications for the authenticated user.
```js
user.notifications(function(err, notifications) {});
```
Show user information for a particular username. Also works for organizations.

@@ -187,2 +225,11 @@

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.

@@ -214,3 +261,3 @@

Updating the contents of a Git. 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/).

@@ -236,7 +283,17 @@ ```js

gist.update(delta, function(err, gist) {
});
```
## Issues API
```js
var issues = github.getIssues(username, reponame);
```
To read all the issues of a given repository
```js
issues.list(options, function(err, issues) {});
```
## Tests

@@ -251,3 +308,3 @@

- Underscore
- Base64 (for basic auth). You can leave this if you are not using basic auth.
- btoa (included in modern browsers, an npm module is included in package.json for node)

@@ -258,3 +315,2 @@ Include these before github.js :

<script src="lib/underscore-min.js">
<script src="lib/base64.js">
<script src="github.js">

@@ -265,3 +321,19 @@ ```

### 0.10.X
Create and delete repositories
### 0.9.X
Paging (introduced at tail end of 0.8.X, note: different callbacks for success & errors now)
### 0.8.X
Fixes and tweaks, simpler auth, CI tests, node.js support, Raw+JSON, UTF8, plus:
Users - follow, unfollow, get info, notifications
Gists - create
Issues - get
Repos - createRepo, deleteRepo, createBranch, star, unstar, isStarred, getCommits, listTags, listPulls, getPull, compare
Hooks - listHooks, getHook, createHook, editHook, deleteHook
### 0.7.X

@@ -277,3 +349,3 @@

Smart caching of latest commit sha.
Smart caching of latest commit sha.

@@ -280,0 +352,0 @@ ### 0.4.X

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc