-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
183 lines (167 loc) · 4.86 KB
/
app.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const { Octokit } = require('@octokit/core');
const { paginateGraphql } = require('@octokit/plugin-paginate-graphql');
const fs = require('fs');
const GQLPaginate = Octokit.plugin(paginateGraphql);
const dotenv = require('dotenv');
const assert = require('assert');
const async = require('async');
// Read .env file, if present
dotenv.config();
// Check for required environment variables
assert(process.env.GHE_API_TOKEN, 'GITHUB_TOKEN is required');
// Create Authenticated Octokit instance
const octokit = new GQLPaginate({
auth: process.env.GHE_API_TOKEN,
baseUrl: (process.env.GHE_HOSTNAME ? `https://${process.env.GHE_HOSTNAME}/api/v3` : 'https://api.github.com'),
});
async function getOrgList() {
try {
const res = await octokit.graphql.paginate(
`query paginate($cursor: String) { organizations(first: 100, after: $cursor) {
pageInfo {
hasNextPage
endCursor
}
nodes {
login
}
}}`,
);
return res.organizations.nodes.map((org) => org.login);
} catch (err) {
console.log(err);
return [];
}
}
async function getOrgMetricsData(owner) {
try {
const depth = process.env.GHE_METRICS_DEPTH ? process.env.GHE_METRICS_DEPTH : 10;
const res = await octokit.graphql.paginate(
`query paginate($cursor: String) { organization(login: "${owner}") {
login
repositories(first: 1, after: $cursor) {
pageInfo {
hasNextPage
endCursor
}
nodes {
nameWithOwner
pushedAt
defaultBranchRef {
name
target {
... on Commit {
id
authoredDate
author {
name
date
}
}
}
}
languages(first: ${depth}) {
nodes {
name
}
edges {
size
}
}
issues(last: ${depth}) {
nodes {
number
author {
login
}
lastEditedAt
createdAt
closedAt
}
}
pullRequests(last: ${depth}) {
nodes {
number
author {
login
}
lastEditedAt
createdAt
mergedAt
closedAt
}
}
}
}
}
rateLimit {
remaining
resetAt
}}`,
);
return res;
} catch (err) {
console.log(err);
return [];
}
}
async function jsonFileMerge(outfile, sourcepath) {
try {
fs.writeFileSync(outfile, '[');
fs.readdirSync(sourcepath).forEach((file, index, arr) => {
fs.writeFileSync(outfile, fs.readFileSync(`${sourcepath}/${file}`), { flag: 'a' });
// Do not add a comma after the last element
if (arr.length - index > 1) {
fs.writeFileSync(outfile, ',\n', { flag: 'a' });
}
});
fs.writeFileSync(outfile, ']', { flag: 'a' });
} catch (err) {
console.log(`Error merging JSON files: ${err}`);
}
}
async function getResults(orgs) {
try {
// Process each org in parallel, limiting to 10 at a time
async.eachLimit(orgs, 10, async(org) => {
if (['github-enterprise', 'actions', 'github'].includes(org)) {
console.log(`skipping ${org}`);
} else {
const orgData = await getOrgMetricsData(org);
await processResults(orgData);
}
}).then(() => {
console.log('Fetching org metrics complete');
jsonFileMerge('./data/orgmetrics.json', './data/orgmetrics');
return true;
});
} catch (err) {
console.log(err);
return false;
}
}
async function processResults(res) {
try {
await res.organization.repositories.nodes.forEach((repo, repoIndex, repoArray) => {
console.log(`[${repoArray.length + 1 - (repoArray.length - repoIndex)}/${repoArray.length}]: Processing ${repo.nameWithOwner}`);
const outputPath = './data/orgmetrics';
if (!fs.existsSync(outputPath)){
fs.mkdirSync(outputPath, { recursive: true });
}
const orgName = repo.nameWithOwner.split('/')[0];
const repoName = repo.nameWithOwner.split('/')[1];
const outFile = `${outputPath}/${orgName}_${repoName}.json`;
console.log(`Writing ${outFile}`);
fs.writeFileSync(outFile, JSON.stringify(repo, null, 2));
});
} catch (err) {
console.log(`Error processing results: ${err}`);
return false;
}
}
async function main() {
// Use org list from environment variable, if present
const orgs = process.env.GHE_ORG_LIST ? process.env.GHE_ORG_LIST.split(',').map((org) => org.trim()) : await getOrgList();
await getResults(orgs);
}
main();