Skip to content

Commit f418efe

Browse files
author
k33g
committed
🛠Add some JavaScript tools
1 parent 6c7c596 commit f418efe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+9925
-0
lines changed

‎api/javascript/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/*
2+
/node_modules/*
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# GitHub API + ES2015 + node.js
2+
3+
## Setup
4+
5+
- see the `package.json` of the `es2015-nodejs` directory
6+
- type `npm install`
7+
- you need the content of `libs/*`
8+
9+
10+
## Use `/libs/GitHubClient.js`
11+
12+
This library can work with :octocat:.com and :octocat: Enterprise
13+
14+
### Create a GitHub client
15+
16+
- First, go to your GitHub profile settings and define a **Personal access token** (https://github.com/settings/tokens)
17+
- Then, add the token to the environment variables (eg: `export TOKEN_GITHUB_DOT_COM=token_string`)
18+
- Now you can get the token like that: `process.env.TOKEN_GITHUB_DOT_COM`
19+
20+
```javascript
21+
const GitHubClient = require('../libs/GitHubClient.js').GitHubClient;
22+
23+
let githubCliEnterprise = new GitHubClient({
24+
baseUri: "http://github.at.home/api/v3",
25+
token: process.env.TOKEN_GHITHUB_ENTERPRISE
26+
});
27+
28+
let githubCliDotCom = new GitHubClient({
29+
baseUri:"https://api.github.com",
30+
token: process.env.TOKEN_GITHUB_DOT_COM
31+
});
32+
33+
```
34+
35+
- if you use GitHub Enterprise, `baseUri` has to be set with `http(s)://your_domain_name/api/v3`
36+
- if you use GitHub.com, `baseUri` has to be set with `https://api.github.com`
37+
38+
### Use the GitHub client
39+
40+
For example, you want to get the information about a user:
41+
(see https://developer.github.com/v3/users/#get-a-single-user)
42+
43+
```javascript
44+
let githubCliEnterprise = new GitHubClient({
45+
baseUri:"http://github.at.home/api/v3",
46+
token:process.env.TOKEN_GHITHUB_ENTERPRISE
47+
});
48+
49+
var handle = "k33g";
50+
githubCliEnterprise.getData({path:`/users/${handle}`})
51+
.then(response => {
52+
console.log(response.data);
53+
});
54+
```
55+
56+
## The easier way: adding features
57+
58+
You can add "features" to `GitHubClient` (like traits):
59+
60+
```javascript
61+
const GitHubClient = require('../libs/GitHubClient.js').GitHubClient;
62+
const octocat = require('../libs/features/octocat');
63+
const users = require('../libs/features/users');
64+
65+
// add octocat and users features to GitHubClient
66+
let githubCli = new GitHubClient({
67+
baseUri:"http://github.at.home/api/v3",
68+
token:process.env.TOKEN_GHITHUB_ENTERPRISE
69+
}, octocat, users);
70+
71+
githubCli.octocat()
72+
.then(data => {
73+
// display the Zen of Octocat
74+
console.log(data);
75+
})
76+
77+
githubCli.fetchUser({handle:'k33g'})
78+
.then(user => {
79+
// all about @k33g
80+
console.log(user);
81+
})
82+
83+
```
84+
85+
## Recipes (and features)
86+
87+
See the `/recipes` directory (more samples to come)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* GitHubClient
3+
*
4+
* Dependencies: node-fetch https://github.com/bitinn/node-fetch
5+
*
6+
*/
7+
const fetch = require('node-fetch');
8+
9+
class HttpException extends Error {
10+
constructor({message, status, statusText, url}) {
11+
super(message);
12+
this.status = status;
13+
this.statusText = statusText;
14+
this.url = url;
15+
}
16+
}
17+
18+
class GitHubClient {
19+
constructor({baseUri, token}, ...features) {
20+
this.baseUri = baseUri;
21+
this.credentials = token !== null && token.length > 0 ? "token" + ' ' + token : null;
22+
this.headers = {
23+
"Content-Type": "application/json",
24+
"Accept": "application/vnd.github.v3.full+json",
25+
"Authorization": this.credentials
26+
};
27+
return Object.assign(this, ...features);
28+
}
29+
30+
callGitHubAPI({method, path, data}) {
31+
let _response = {};
32+
return fetch(this.baseUri + path, {
33+
method: method,
34+
headers: this.headers,
35+
body: data!==null ? JSON.stringify(data) : null
36+
})
37+
.then(response => {
38+
_response = response;
39+
// if response is ok transform response.text to json object
40+
// else throw error
41+
if (response.ok) {
42+
return response.json()
43+
} else {
44+
throw new HttpException({
45+
message: `HttpException[${method}]`,
46+
status:response.status,
47+
statusText:response.statusText,
48+
url: response.url
49+
});
50+
}
51+
})
52+
.then(jsonData => {
53+
_response.data = jsonData;
54+
return _response;
55+
})
56+
57+
}
58+
59+
getData({path}) {
60+
return this.callGitHubAPI({method:'GET', path, data:null});
61+
}
62+
63+
deleteData({path}) {
64+
return this.callGitHubAPI({method:'DELETE', path, data:null});
65+
}
66+
67+
postData({path, data}) {
68+
return this.callGitHubAPI({method:'POST', path, data});
69+
}
70+
71+
putData({path, data}) {
72+
return this.callGitHubAPI({method:'PUT', path, data});
73+
}
74+
}
75+
76+
module.exports = {
77+
GitHubClient: GitHubClient
78+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
# Commits features
3+
4+
## Setup
5+
6+
```javascript
7+
const GitHubClient = require('../libs/GitHubClient.js').GitHubClient;
8+
const commits = require('../libs/features/commits');
9+
10+
11+
let githubCli = new GitHubClient({
12+
baseUri: "http://github.at.home/api/v3",
13+
token: process.env.TOKEN_GHITHUB_ENTERPRISE
14+
}, commits); //<-- add commits features
15+
```
16+
*/
17+
18+
/*
19+
## fetchCommitBySHA
20+
21+
- parameter: `sha, owner, repository`
22+
- return: `Promise`
23+
24+
### Description
25+
26+
`fetchCommitBySHA` gets a commit by its sha
27+
28+
*/
29+
function fetchCommitBySHA({sha, owner, repository}){
30+
return this.getData({path:`/repos/${owner}/${repository}/git/commits/${sha}`})
31+
.then(response => {
32+
return response.data;
33+
});
34+
}
35+
36+
module.exports = {
37+
fetchCommitBySHA: fetchCommitBySHA
38+
};
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
# Contents features
3+
4+
## Setup
5+
6+
```javascript
7+
const GitHubClient = require('../libs/GitHubClient.js').GitHubClient;
8+
const contents = require('../libs/features/contents');
9+
10+
11+
let githubCli = new GitHubClient({
12+
baseUri: "http://github.at.home/api/v3",
13+
token: process.env.TOKEN_GHITHUB_ENTERPRISE
14+
}, contents); //<-- add contents features
15+
```
16+
*/
17+
18+
/*
19+
## fetchContent
20+
21+
- parameter: `path, owner, repository, decode`
22+
- return: `Promise`
23+
24+
### Description
25+
26+
`fetchContent` gets the text content of a source file
27+
28+
*/
29+
function fetchContent({path, owner, repository, decode}){
30+
return this.getData({path:`/repos/${owner}/${repository}/contents/${path}`})
31+
.then(response => {
32+
if(decode==true) {
33+
response.data.contentText = new Buffer(response.data.content, response.data.encoding).toString("ascii")
34+
}
35+
return response.data;
36+
});
37+
}
38+
39+
/*
40+
## createFile
41+
42+
- parameter: `file, content, message, branch, owner, repository`
43+
- return: `Promise`
44+
45+
### Description
46+
47+
`createFile` creates a source file
48+
49+
*/
50+
function createFile({file, content, message, branch, owner, repository}) {
51+
let contentB64 = (new Buffer(content)).toString('base64');
52+
return this.putData({path:`/repos/${owner}/${repository}/contents/${file}`, data:{
53+
message, branch, content: contentB64
54+
}}).then(response => {
55+
return response.data;
56+
});
57+
}
58+
59+
/*
60+
## searchCode
61+
62+
- parameter: `q` (query parameters)
63+
- return: `Promise`
64+
65+
### Description
66+
67+
`searchCode` executes a search
68+
69+
*/
70+
function searchCode({q}) {
71+
return this.getData({path:`/search/code?q=${q}`})
72+
.then(response => {
73+
return response.data;
74+
});
75+
}
76+
77+
module.exports = {
78+
fetchContent: fetchContent,
79+
createFile: createFile,
80+
searchCode: searchCode
81+
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
# Hooks features
3+
4+
## Setup
5+
6+
```javascript
7+
const GitHubClient = require('../libs/GitHubClient.js').GitHubClient;
8+
const hooks = require('../libs/features/hooks');
9+
10+
11+
let githubCli = new GitHubClient({
12+
baseUri: "http://github.at.home/api/v3",
13+
token: process.env.TOKEN_GHITHUB_ENTERPRISE
14+
}, hooks); //<-- add hooks features
15+
```
16+
*/
17+
18+
/*
19+
## createHook
20+
21+
- parameter: `owner, repository, hookName, hookConfig, hookEvents, active`
22+
- return: `Promise`
23+
24+
### Description
25+
26+
`createHook` creates a hook for a repository
27+
28+
*/
29+
function createHook({owner, repository, hookName, hookConfig, hookEvents, active}) {
30+
return this.postData({path:`/repos/${owner}/${repository}/hooks`, data:{
31+
name: hookName
32+
, config: hookConfig
33+
, events: hookEvents
34+
, active: active
35+
}}).then(response => {
36+
return response.data;
37+
});
38+
}
39+
40+
/*
41+
## createOrganizationHook
42+
43+
- parameter: `org, hookName, hookConfig, hookEvents, active`
44+
- return: `Promise`
45+
46+
### Description
47+
48+
`createOrganizationHook` creates a hook for an organization
49+
50+
*/
51+
function createOrganizationHook({org, hookName, hookConfig, hookEvents, active}) {
52+
return this.postData({path:`/orgs/${org}/hooks`, data:{
53+
name: hookName
54+
, config: hookConfig
55+
, events: hookEvents
56+
, active: active
57+
}}).then(response => {
58+
return response.data;
59+
});
60+
}
61+
62+
module.exports = {
63+
createHook: createHook,
64+
createOrganizationHook: createOrganizationHook
65+
};

0 commit comments

Comments
 (0)