-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
k33g
committed
Oct 25, 2016
1 parent
6c7c596
commit f418efe
Showing
84 changed files
with
9,925 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea/* | ||
This comment has been minimized.
Sorry, something went wrong. |
||
/node_modules/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# GitHub API + ES2015 + node.js | ||
|
||
## Setup | ||
|
||
- see the `package.json` of the `es2015-nodejs` directory | ||
- type `npm install` | ||
- you need the content of `libs/*` | ||
|
||
|
||
## Use `/libs/GitHubClient.js` | ||
|
||
This library can work with :octocat:.com and :octocat: Enterprise | ||
|
||
### Create a GitHub client | ||
|
||
- First, go to your GitHub profile settings and define a **Personal access token** (https://github.com/settings/tokens) | ||
- Then, add the token to the environment variables (eg: `export TOKEN_GITHUB_DOT_COM=token_string`) | ||
- Now you can get the token like that: `process.env.TOKEN_GITHUB_DOT_COM` | ||
|
||
```javascript | ||
const GitHubClient = require('../libs/GitHubClient.js').GitHubClient; | ||
|
||
let githubCliEnterprise = new GitHubClient({ | ||
baseUri: "http://github.at.home/api/v3", | ||
token: process.env.TOKEN_GHITHUB_ENTERPRISE | ||
}); | ||
|
||
let githubCliDotCom = new GitHubClient({ | ||
baseUri:"https://api.github.com", | ||
token: process.env.TOKEN_GITHUB_DOT_COM | ||
}); | ||
|
||
``` | ||
|
||
- if you use GitHub Enterprise, `baseUri` has to be set with `http(s)://your_domain_name/api/v3` | ||
- if you use GitHub.com, `baseUri` has to be set with `https://api.github.com` | ||
|
||
### Use the GitHub client | ||
|
||
For example, you want to get the information about a user: | ||
(see https://developer.github.com/v3/users/#get-a-single-user) | ||
|
||
```javascript | ||
let githubCliEnterprise = new GitHubClient({ | ||
baseUri:"http://github.at.home/api/v3", | ||
token:process.env.TOKEN_GHITHUB_ENTERPRISE | ||
}); | ||
|
||
var handle = "k33g"; | ||
githubCliEnterprise.getData({path:`/users/${handle}`}) | ||
.then(response => { | ||
console.log(response.data); | ||
}); | ||
``` | ||
|
||
## The easier way: adding features | ||
|
||
You can add "features" to `GitHubClient` (like traits): | ||
|
||
```javascript | ||
const GitHubClient = require('../libs/GitHubClient.js').GitHubClient; | ||
const octocat = require('../libs/features/octocat'); | ||
const users = require('../libs/features/users'); | ||
|
||
// add octocat and users features to GitHubClient | ||
let githubCli = new GitHubClient({ | ||
baseUri:"http://github.at.home/api/v3", | ||
token:process.env.TOKEN_GHITHUB_ENTERPRISE | ||
}, octocat, users); | ||
|
||
githubCli.octocat() | ||
.then(data => { | ||
// display the Zen of Octocat | ||
console.log(data); | ||
}) | ||
|
||
githubCli.fetchUser({handle:'k33g'}) | ||
.then(user => { | ||
// all about @k33g | ||
console.log(user); | ||
}) | ||
|
||
``` | ||
|
||
## Recipes (and features) | ||
|
||
This comment has been minimized.
Sorry, something went wrong. |
||
See the `/recipes` directory (more samples to come) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* GitHubClient | ||
* | ||
* Dependencies: node-fetch https://github.com/bitinn/node-fetch | ||
* | ||
*/ | ||
const fetch = require('node-fetch'); | ||
|
||
class HttpException extends Error { | ||
constructor({message, status, statusText, url}) { | ||
super(message); | ||
this.status = status; | ||
this.statusText = statusText; | ||
this.url = url; | ||
} | ||
} | ||
|
||
class GitHubClient { | ||
constructor({baseUri, token}, ...features) { | ||
this.baseUri = baseUri; | ||
this.credentials = token !== null && token.length > 0 ? "token" + ' ' + token : null; | ||
this.headers = { | ||
"Content-Type": "application/json", | ||
"Accept": "application/vnd.github.v3.full+json", | ||
"Authorization": this.credentials | ||
}; | ||
return Object.assign(this, ...features); | ||
} | ||
|
||
callGitHubAPI({method, path, data}) { | ||
let _response = {}; | ||
return fetch(this.baseUri + path, { | ||
method: method, | ||
headers: this.headers, | ||
body: data!==null ? JSON.stringify(data) : null | ||
}) | ||
.then(response => { | ||
_response = response; | ||
// if response is ok transform response.text to json object | ||
// else throw error | ||
if (response.ok) { | ||
return response.json() | ||
} else { | ||
throw new HttpException({ | ||
message: `HttpException[${method}]`, | ||
status:response.status, | ||
statusText:response.statusText, | ||
url: response.url | ||
}); | ||
} | ||
}) | ||
.then(jsonData => { | ||
_response.data = jsonData; | ||
return _response; | ||
}) | ||
|
||
} | ||
|
||
getData({path}) { | ||
return this.callGitHubAPI({method:'GET', path, data:null}); | ||
} | ||
|
||
deleteData({path}) { | ||
return this.callGitHubAPI({method:'DELETE', path, data:null}); | ||
} | ||
|
||
postData({path, data}) { | ||
return this.callGitHubAPI({method:'POST', path, data}); | ||
} | ||
|
||
putData({path, data}) { | ||
return this.callGitHubAPI({method:'PUT', path, data}); | ||
} | ||
} | ||
|
||
module.exports = { | ||
GitHubClient: GitHubClient | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
# Commits features | ||
## Setup | ||
```javascript | ||
const GitHubClient = require('../libs/GitHubClient.js').GitHubClient; | ||
const commits = require('../libs/features/commits'); | ||
let githubCli = new GitHubClient({ | ||
baseUri: "http://github.at.home/api/v3", | ||
token: process.env.TOKEN_GHITHUB_ENTERPRISE | ||
}, commits); //<-- add commits features | ||
``` | ||
*/ | ||
|
||
/* | ||
## fetchCommitBySHA | ||
- parameter: `sha, owner, repository` | ||
- return: `Promise` | ||
### Description | ||
`fetchCommitBySHA` gets a commit by its sha | ||
*/ | ||
function fetchCommitBySHA({sha, owner, repository}){ | ||
return this.getData({path:`/repos/${owner}/${repository}/git/commits/${sha}`}) | ||
.then(response => { | ||
return response.data; | ||
}); | ||
} | ||
|
||
module.exports = { | ||
fetchCommitBySHA: fetchCommitBySHA | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
# Contents features | ||
## Setup | ||
```javascript | ||
const GitHubClient = require('../libs/GitHubClient.js').GitHubClient; | ||
const contents = require('../libs/features/contents'); | ||
let githubCli = new GitHubClient({ | ||
baseUri: "http://github.at.home/api/v3", | ||
token: process.env.TOKEN_GHITHUB_ENTERPRISE | ||
}, contents); //<-- add contents features | ||
``` | ||
*/ | ||
|
||
/* | ||
## fetchContent | ||
- parameter: `path, owner, repository, decode` | ||
- return: `Promise` | ||
### Description | ||
`fetchContent` gets the text content of a source file | ||
*/ | ||
function fetchContent({path, owner, repository, decode}){ | ||
return this.getData({path:`/repos/${owner}/${repository}/contents/${path}`}) | ||
.then(response => { | ||
if(decode==true) { | ||
response.data.contentText = new Buffer(response.data.content, response.data.encoding).toString("ascii") | ||
} | ||
return response.data; | ||
}); | ||
} | ||
|
||
/* | ||
## createFile | ||
- parameter: `file, content, message, branch, owner, repository` | ||
- return: `Promise` | ||
### Description | ||
`createFile` creates a source file | ||
*/ | ||
function createFile({file, content, message, branch, owner, repository}) { | ||
let contentB64 = (new Buffer(content)).toString('base64'); | ||
return this.putData({path:`/repos/${owner}/${repository}/contents/${file}`, data:{ | ||
message, branch, content: contentB64 | ||
}}).then(response => { | ||
return response.data; | ||
}); | ||
} | ||
|
||
/* | ||
## searchCode | ||
- parameter: `q` (query parameters) | ||
- return: `Promise` | ||
### Description | ||
`searchCode` executes a search | ||
*/ | ||
function searchCode({q}) { | ||
return this.getData({path:`/search/code?q=${q}`}) | ||
.then(response => { | ||
return response.data; | ||
}); | ||
} | ||
|
||
module.exports = { | ||
fetchContent: fetchContent, | ||
createFile: createFile, | ||
searchCode: searchCode | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
# Hooks features | ||
## Setup | ||
```javascript | ||
const GitHubClient = require('../libs/GitHubClient.js').GitHubClient; | ||
const hooks = require('../libs/features/hooks'); | ||
let githubCli = new GitHubClient({ | ||
baseUri: "http://github.at.home/api/v3", | ||
token: process.env.TOKEN_GHITHUB_ENTERPRISE | ||
}, hooks); //<-- add hooks features | ||
``` | ||
*/ | ||
|
||
/* | ||
## createHook | ||
- parameter: `owner, repository, hookName, hookConfig, hookEvents, active` | ||
- return: `Promise` | ||
### Description | ||
`createHook` creates a hook for a repository | ||
*/ | ||
function createHook({owner, repository, hookName, hookConfig, hookEvents, active}) { | ||
return this.postData({path:`/repos/${owner}/${repository}/hooks`, data:{ | ||
name: hookName | ||
, config: hookConfig | ||
, events: hookEvents | ||
, active: active | ||
}}).then(response => { | ||
return response.data; | ||
}); | ||
} | ||
|
||
/* | ||
## createOrganizationHook | ||
- parameter: `org, hookName, hookConfig, hookEvents, active` | ||
- return: `Promise` | ||
### Description | ||
`createOrganizationHook` creates a hook for an organization | ||
*/ | ||
function createOrganizationHook({org, hookName, hookConfig, hookEvents, active}) { | ||
return this.postData({path:`/orgs/${org}/hooks`, data:{ | ||
name: hookName | ||
, config: hookConfig | ||
, events: hookEvents | ||
, active: active | ||
}}).then(response => { | ||
return response.data; | ||
}); | ||
} | ||
|
||
module.exports = { | ||
createHook: createHook, | ||
createOrganizationHook: createOrganizationHook | ||
}; |
Oops, something went wrong.
Nomoskar 🙇🏻 💴 💹