-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-user.js
More file actions
91 lines (59 loc) · 2.24 KB
/
github-user.js
File metadata and controls
91 lines (59 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict'
const {Octokit} = require('@octokit/rest'),
cacheCalls = require('docloop').cacheCalls
/**
* Class representing minimal Github API wrapper for user interaction.
*
* @memberOf module:githubAdapter
* @alias GithubUser
*
* @property github Instance of GitHubApi (TODO: add link)
*/
class GithubUser {
constructor(){
cacheCalls(this, 'get' , 2000)
cacheCalls(this, 'getInstallations' , 2000)
}
/**
* Get Github user data.
*
* This method is augumented by {@link module:docloop#cache}(2000) return value will be cached for 2 seconds in order to prevent huge numbers of API calls.
*
* @async
* @param {String} token Github user access token
* @return {Object} Github user data
*
* @throws {TypeError} If token is not a string.
*/
async get(token){
if(typeof token != 'string') throw TypeError('GithubUser.get() token must be a string; got:' +token)
console.log(token)
const octokit = new Octokit({auth:token})
var result = await octokit.users.getAuthenticated()
return result.data
}
/**
* Get data for all installations granted access to by provided access token.
*
* This method is augumented by {@link module:docloop#cache}(2000) return value will be cached for 2 seconds in order to prevent huge numbers of API calls.
*
* @param {String} token Github user access token
* @return {Object[]} Installation id of each accessible installation
*/
async getInstallations(token){
if(typeof token != 'string') throw TypeError('GithubUser.getInstallations() token must be a string; got: ' +token)
const octokit = new Octokit({auth:token})
var response = await octokit.apps.listInstallationsForAuthenticatedUser({per_page: 100}),
installations = response.data.installations || []
//TODO: handle more than 100 installations...
return installations.map(installation => installation.id)
}
// async getRepositories(token){
// if(typeof token != 'string') throw TypeError('GithubUser.getIRepositories() token must be a string; got: ' +token)
// const octokit = new Octokit({auth:token})
// var response = await octokit.repos.listForAuthenticatedUser({per_page: 100})
// console.log(response)
// return []
// }
}
module.exports = GithubUser