octonode is a library for nodejs to access the github v3 api
npm install octonode
vargithub=require('octonode');// Then we instanciate a client with or without a token (as show in a later section)varghme=client.me();varghuser=client.user('pksunkara');varghrepo=client.repo('pksunkara/hub');varghorg=client.org('flatiron');varghissue=client.issue('pksunkara/hub',37);varghpr=client.pr('pksunkara/hub',37);varghgist=client.gist();varghteam=client.team(37);varghsearch=client.search();varclient=github.client();client.get('/users/pksunkara',{},function(err,status,body,headers){console.log(body);//json object});varclient=github.client('someaccesstoken');client.get('/user',{},function(err,status,body,headers){console.log(body);//json object});varclient=github.client({username: 'pksunkara',password: 'password'});client.get('/user',{},function(err,status,body,headers){console.log(body);//json object});varclient=github.client({id: 'abcdefghijklmno',secret: 'abcdefghijk'});client.get('/user',{},function(err,status,body,headers){console.log(body);//json object});Request options can be set by setting defaults on the client. (e.g. Proxies)
varclient=github.client();client.requestDefaults['proxy']='https://myproxy.com:1085'These options are passed though to request, see their API here: https://github.com/mikeal/request#requestoptions-callback
You can set proxies dynamically by using the example above, but Octonode will respect environment proxies by default. Just set this using:
export HTTP_PROXY='https://myproxy.com:1085' if you are using the command line
Many of the below use cases use parts of the above code
github.auth.config({username: 'pksunkara',password: 'password'}).login(['user','repo','gist'],function(err,id,token){console.log(id,token);});github.auth.config({username: 'pksunkara',password: 'password'}).revoke(id,function(err){if(err)throwerr;});// Web application which authenticates to githubvarhttp=require('http'),url=require('url'),qs=require('querystring'),github=require('octonode');// Build the authorization config and urlvarauth_url=github.auth.config({id: 'mygithubclientid',secret: 'mygithubclientsecret'}).login(['user','repo','gist']);// Store info to verify against CSRFvarstate=auth_url.match(/&state=([0-9a-z]{32})/i);// Web serverhttp.createServer(function(req,res){uri=url.parse(req.url);// Redirect to github loginif(uri.pathname=='/login'){res.writeHead(301,{'Content-Type': 'text/plain','Location': auth_url})res.end('Redirecting to '+auth_url);}// Callback url from github loginelseif(uri.pathname=='/auth'){varvalues=qs.parse(uri.query);// Check against CSRF attacksif(!state||state[1]!=values.state){res.writeHead(403,{'Content-Type': 'text/plain'});res.end('');}else{github.auth.login(values.code,function(err,token){res.writeHead(200,{'Content-Type': 'text/plain'});res.end(token);});}}else{res.writeHead(200,{'Content-Type': 'text/plain'})res.end('');}}).listen(3000);console.log('Server started on 3000');You can also check your rate limit status by calling the following.
client.limit(function(err,left,max){console.log(left);// 4999console.log(max);// 5000});All the callbacks for the following will take first an error argument, then a data argument, like this:
ghme.info(function(err,data,headers){console.log("error: "+err);console.log("data: "+data);console.log("headers:"+headers);});If a function is said to be supporting pagination, then that function can be used in many ways as shown below. Results from the function are arranged in pages.
The page argument is optional and is used to specify which page of issues to retrieve. The perPage argument is also optional and is used to specify how many issues per page.
// Normal usage of functionghrepo.issues(callback);//array of first 30 issues// Using pagination parametersghrepo.issues(2,100,callback);//array of second 100 issuesghrepo.issues(10,callback);//array of 30 issues from page 10// Pagination parameters can be set with query object tooghrepo.issues({page: 2,per_page: 100,state: 'closed'},callback);//array of second 100 issues which are closedToken/Credentials required for the following:
ghme.info(callback);//jsonghme.update({"name": "monalisa octocat","email": "octocat@github.com",},callback);ghme.emails(callback);//array of emailsghme.emails(['new1@ma.il','new2@ma.il'],callback);//array of emailsghme.emails('new@ma.il',callback);//array of emailsghme.emails(['new1@ma.il','new2@ma.il']);ghme.emails('new@ma.il');ghme.followers(callback);//array of github usersThis query supports pagination.
ghme.following(callback);//array of github usersghme.following('marak',callback);//booleanghme.follow('marak');ghme.unfollow('marak');ghme.keys(callback);//array of keysghme.keys(1,callback);//keyghme.keys({"title":"laptop","key":"ssh-rsa AAA..."},callback);//keyghme.keys(1,{"title":"desktop","key":"ssh-rsa AAA..."},callback);//keyghme.keys(1);This query supports pagination.
ghme.starred(callback);//array of reposghme.starred('flatiron/flatiron',callback);//booleanghme.star('flatiron/flatiron');ghme.unstar('flatiron/flatiron');This query supports pagination.
ghme.watched(callback);//array of reposThis query supports pagination.
ghme.orgs(callback);//array of orgsThis query supports pagination.
ghme.repos(callback);//array of reposghme.repo({"name": "Hello-World","description": "This is your first repo",},callback);//repoghme.fork('pksunkara/hub',callback);//forked repoNo token required for the following
ghuser.info(callback);//jsonThis query supports pagination.
ghuser.followers(callback);//array of github usersThis query supports pagination.
ghuser.following(callback);//array of github usersThis query supports pagination.
ghuser.events(['commit_comment'],callback);//array of eventsThis query supports pagination.
ghuser.orgs(callback);//array of organizationsghrepo.info(callback);//jsonghrepo.collaborators(callback);//array of github usersghrepo.collaborators('marak',callback);//booleanghrepo.commits(callback);//array of commitsghrepo.commit('18293abcd72',callback);//commitghrepo.tags(callback);//array of tagsghrepo.releases(callback);//array of releasesghrepo.languages(callback);//array of languagesghrepo.contributors(callback);//array of github usersghrepo.branches(callback);//array of branchesThis query supports pagination.
ghrepo.issues(callback);//array of issuesghrepo.issue({"title": "Found a bug","body": "I'm having a problem with this.","assignee": "octocat","milestone": 1,"labels": ["Label1","Label2"]},callback);//issueThis query supports pagination.
ghrepo.prs(callback);//array of pull requestsghrepo.pr({"title": "Amazing new feature","body": "Please pull this in!","head": "octocat:new-feature","base": "master"},callback);//pull requestThis query supports pagination.
ghrepo.hooks(callback);//array of hooksghrepo.hook({"name": "web","active": true,"events": ["push","pull_request"],"config": {"url": "http://myawesomesite.com/github/events"}},callback);// hookghrepo.readme(callback);//fileghrepo.readme('v0.1.0',callback);//fileghrepo.contents('lib/index.js',callback);//pathghrepo.contents('lib/index.js','v0.1.0',callback);//pathghrepo.createContents('lib/index.js','commit message','content',callback);//pathghrepo.createContents('lib/index.js','commit message','content','v0.1.0',callback);//pathghrepo.updateContents('lib/index.js','commit message','content','put-sha-here',callback);//pathghrepo.updateContents('lib/index.js','commit message','content','put-sha-here','v0.1.0',callback);//pathghrepo.deleteContents('lib/index.js','commit message','put-sha-here',callback);//pathghrepo.deleteContents('lib/index.js','commit message','put-sha-here','v0.1.0',callback);//pathghrepo.archive('tarball',callback);//link to archiveghrepo.archive('zipball','v0.1.0',callback);//link to archiveghrepo.blob('18293abcd72',callback);//blobghrepo.stargazers(1,100,callback);//array of usersghrepo.stargazers(10,callback);//array of usersghrepo.stargazers(callback);//array of usersghrepo.teams(callback);//array of teamsghrepo.tree('18293abcd72',callback);//treeghrepo.tree('18293abcd72',true,callback);//recursive treeghrepo.destroy();ghrepo.statuses('master',callback);//array of statusesghrepo.status('18e129c213848c7f239b93fe5c67971a64f183ff',{"state": "success","target_url": "http://ci.mycompany.com/job/hub/3","description": "Build success."},callback);// created statusghorg.info(callback);//jsonghorg.update({blog: 'https://blog.com'},callback);// orgThis query supports pagination.
ghorg.repos(callback);//array of reposghorg.repo({name: 'Hello-world',description: 'My first world program'},callback);//repoghorg.teams(callback);//array of teamsghorg.members(callback);//array of github usersghorg.member('pksunkara',callback);//booleanghissue.info(callback);//issueghissue.update({"title": "Found a bug and I am serious",},callback);//issueThis query supports pagination.
ghissue.comments(callback);//array of commentsghpr.info(callback);//pull requestghpr.update({'title': 'Wow this pr'},callback);//pull requestghpr.close(callback);//pull requestghpr.merged(callback);//booleanghpr.commits(callback);//array of commitsghpr.comments(callback);//array of commentsghpr.files(callback);//array of filesThis query supports pagination.
ghgist.list(callback);//array of gistsThis query supports pagination.
ghgist.public(callback);//array of gistsThis query supports pagination.
ghgist.starred(callback);//array of gistsThis query supports pagination.
ghgist.user('pksunkara',callback);//array of gistsghgist.get(37,callback);//gistghgist.create({description: "the description",files: { ... }}),callback);//gistghgist.edit(37,{description: "hello gist"},callback);//gistghgist.delete(37);ghgist.fork(37,callback);//gistghgist.star(37);ghgist.unstar(37);ghgist.check(37);//booleanghgist.comments(37,callback);//array of commentsghgist.comments(37,{body: "Just commenting"},callback);//commentghgist.comment(1,callback);//commentghgist.comment(1,{body: "lol at commenting"},callback);//commentghgist.comment(1);ghteam.info(callback);//jsonghteam.members(callback);//array of github usersghteam.member('pksunkara');//booleanghsearch.issues({q: 'windows+state:open+repo:pksunkara/hub',sort: 'created'order: 'asc'},callback);//array of search resultsghsearch.repos({q: 'hub+language:go',sort: 'created',order: 'asc'},callback);//array of search resultsghsearch.users({q: 'tom+followers:>100',sort: 'created',order: 'asc'},callback);//array of search resultsghsearch.code({q: 'auth+in:file+repo:pksunkara/hub',sort: 'created',order: 'asc'},callback);//array of search resultsnpm test
If you like this project, please watch this and follow me.
Here is a list of Contributors
The following method names use underscore as an example. The library contains camel cased method names.
// public repos for unauthenticated, private and public for authenticatedme.get_watched_repositories(callback);me.is_watching('repo',callback);me.start_watching('repo',callback);me.stop_watching('repo',callback);me.get_issues(params,callback);// organization datavarorg=octonode.Organization('bulletjs');org.update(dict_with_update_properties,callback);org.add_member('user','team',callback);org.remove_member('user',callback);org.get_public_members(callback);org.is_public_member('user',callback);org.make_member_public('user',callback);org.conceal_member('user',callback);org.get_team('team',callback);org.create_team({name:'',repo_names:'',permission:''},callback);org.edit_team({name:'',permission:''},callback);org.delete_team('name',callback);org.get_team_members('team',callback);org.get_team_member('team','user',callback);org.remove_member_from_team('user','team',callback);org.get_repositories(callback);org.create_repository({name: ''},callback);org.get_team_repositories('team',callback);org.get_team_repository('team','name',callback);org.add_team_repository('team','name',callback);org.remove_team_repository('team','name',callback);varrepo=octonode.Repository('pksunkara/octonode');repo.update({name: ''},callback);// collaborator informationrepo.add_collaborator('name',callback);repo.remove_collaborator('name',callback);// commit datarepo.get_commit('sha-id',callback);repo.get_all_comments(callback);repo.get_commit_comments('SHA ID',callback);repo.comment_on_commit({body: '',commit_id: '',line: '',path: '',position: ''},callback);repo.get_single_comment('comment id',callback);repo.edit_single_comment('comment id',callback);repo.delete_single_comment('comment id',callback);// downloadsrepo.get_downloads(callback);repo.get_download(callback);repo.create_download({name: ''},'filepath',callback);repo.delete_download(callback);// keysrepo.get_deploy_keys(callback);repo.get_deploy_key('id',callback);repo.create_deploy_key({title: '',key: ''},callback);repo.edit_deploy_key({title: '',key: ''},callback);repo.delete_deploy_key('id',callback);// watcher datarepo.get_watchers(callback);// pull requestsrepo.get_all_pull_request_comments(callback);repo.get_pull_request_comment('id',callback);repo.create_pull_request_comment('id',{body:'',commit_id:'',path:'',position:''},callback);repo.reply_to_pull_request_comment('id','body',callback);repo.edit_pull_request_comment('id','body',callback);repo.delete_pull_request_comment('id',callback);repo.get_issues(params,callback);repo.get_issue('id',callback);repo.create_issue({title: ''},callback);repo.edit_issue({title: ''},callback);repo.get_issue_comments('issue',callback);repo.get_issue_comment('id',callback);repo.create_issue_comment('id','comment',callback);repo.edit_issue_comment('id','comment',callback);repo.delete_issue_comment('id',callback);repo.get_issue_events('id',callback);repo.get_events(callback);repo.get_event('id',callback);repo.get_labels(callback);repo.get_label('id',callback);repo.create_label('name','color',callback);repo.edit_label('name','color',callback);repo.delete_label('id',callback);repo.get_issue_labels('issue',callback);repo.add_labels_to_issue('issue',['label1','label2'],callback);repo.remove_label_from_issue('issue','labelid',callback);repo.set_labels_for_issue('issue',['label1','label2'],callback);repo.remove_all_labels_from_issue('issue',callback);repo.get_labels_for_milestone_issues('milestone',callback);repo.get_milestones(callback);repo.get_milestone('id',callback);repo.create_milestone('title',callback);repo.edit_milestone('title',callback);repo.delete_milestone('id',callback);// raw git accessrepo.create_blob('content','encoding',callback);repo.get_commit('sha-id',callback);repo.create_commit('message','tree',[parents],callback);repo.get_reference('ref',callback);repo.get_all_references(callback);repo.create_reference('ref','sha',callback);repo.update_reference('ref','sha',force,callback);I accept pull requests and guarantee a reply back within a day
MIT/X11
Report here. Guaranteed reply within a day.
Pavan Kumar Sunkara (pavan.sss1991@gmail.com)