forked from brown-ccv/ccv-website-hugo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
116 lines (95 loc) · 2.75 KB
/
index.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
require('dotenv').config()
const fs = require('fs');
const request = require('request');
const yaml = require('js-yaml');
const token = process.env.GITHUB_TOKEN;
const user = process.env.GITHUB_USER;
const organizationList = process.env.ORGANIZATION.split(',');
const githubGqlRequest = (query) => {
return new Promise((resolve, reject) => {
request({
method: 'POST',
url: `https://api.github.com/graphql`,
headers: {
'Authorization': `token ${token}`,
'User-Agent': user,
'Accept': 'application/vnd.github.v3+json'
},
json: true,
body: query
}, (err, response, body) => {
if (err) {
return reject(err);
}
resolve(response.body)
});
});
};
/**
* The procedure below gets a list of repositories for the organization using
* the repos API, for each repo, it checks if there is a docs folder using the
* contents API. If the repo has a docs folder, then we get the README file
* content using the readme API.
*
*/
let totalOrgs = organizationList.length;
// retrievedData : dictionary containing accumulated repo information from
// seen pages, info only written to file when all pages traversed
let retrievedData = {} // (key: organization, value: [] of repos for that org)
for (let i = 0; i < totalOrgs; i++) {
let organization = organizationList[i];
let filePath = 'data/info-' + organization + '.json';
retrievedData[organization] = [];
getPage(organization, filePath, "");
}
function cursorText(cursor) {
if (cursor.length > 0 ) {
return `, after: "${cursor}"`
} else {
return ""
}
}
function getPage(organization, filePath, cursor) {
let last = false;
let nextCursor = ""
let query = {query: `
{ organization(login: "${organization}") {
repositories(first: 100${cursorText(cursor)}) {
pageInfo {
endCursor
hasNextPage
}
nodes {
name
ready: object(expression: "master:ready.yml") {
oid
... on Blob {
text
}
}
}
}
}
}
`}
githubGqlRequest(query)
.then((values) => {
let repos = values.data.organization.repositories
if (repos.pageInfo.hasNextPage) {
nextCursor = repos.pageInfo.endCursor
} else {
last = true
}
let readyRepos = repos.nodes.filter(repo => repo.ready)
readyRepos.forEach((repo) => retrievedData[organization].push(yaml.safeLoad(repo.ready.text)))
if (last) {
// on last page, write to file
fs.writeFileSync(filePath, JSON.stringify(retrievedData[organization], null, 2));
console.log('Data written to ' + filePath + ", num items: " + retrievedData[organization].length);
} else {
// get data from next page
getPage(organization, filePath, nextCursor);
}
})
.catch((err) => console.error(err));
}