This repository was archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgithub.js
More file actions
82 lines (75 loc) · 2.26 KB
/
github.js
File metadata and controls
82 lines (75 loc) · 2.26 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
var GitHub = require('github-base')
var chalk = require('chalk')
const interpretResponseCode = (statusCode) => {
var response = ''
switch (statusCode) {
case 200:
case 204:
response = 'OK'
break
case 202:
response = 'Github received listing request and is processing data. Please try again in a moment'
break
case 404:
response = 'Organization cannot be found.'
break
case 403:
response = 'Access Denied'
break
default:
response = 'Unexpected response code: ' + statusCode
}
return response
}
const authenticate = (options) => {
var githubHandler
var apiurl = 'https://api.github.com'
if (options.apiurl) {
if (options.apiurl.substring(0, 8) !== 'https://' || !options.apiurl.includes('/api/v3')) {
console.log('The api url should look like https://mygheinstanceurl.mycompany.com/api/v3')
process.exit(1)
}
// console.log(options.apiurl.includes('/api/v3'));
// if(options.apiurl.substring(0,7))
apiurl = options.apiurl
}
if (options.token) {
githubHandler = new GitHub({
token: options.token,
apiurl: apiurl
})
} else if (options.username && options.password) {
githubHandler = new GitHub({
username: options.username,
password: options.password,
apiurl: apiurl
})
} else {
console.error(chalk.red('Invalid input ! Must provide token or username/password'))
process.exit(1)
}
return githubHandler
}
const getGithubOrgList = (githubHandler, orgName, privateReposOnly) => {
return new Promise((resolve, reject) => {
var url = '/orgs/' + orgName + '/repos?type=all'
if (privateReposOnly) url = '/orgs/' + orgName + '/repos?type=private'
githubHandler.paged(url)
.then((res) => {
var aggregatedPagesRecords = [];
for(var i=0;i<res.pages.length;i++){
let interpretedResponse = interpretResponseCode(res.pages[i].statusCode)
if (interpretedResponse === 'OK') {
aggregatedPagesRecords = aggregatedPagesRecords.concat(res.pages[i].body);
} else {
reject(interpretedResponse);
}
}
resolve(aggregatedPagesRecords);
})
.catch((err) => {
reject(err);
});
})
}
module.exports = { authenticate, getGithubOrgList }