This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
executable file
·383 lines (339 loc) · 13.1 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env node
'use strict'
const INTER_CALLS_DELAY = 1000
var program = require('commander')
const chalk = require('chalk')
const figlet = require('figlet')
var fs = require('fs')
var path = require('path')
var contributors = require('./contributors.js')
var githubUtils = require('./github');
var nbOfDays = 90
var roundedNbOfWeeks = Math.floor(nbOfDays / 7)
var filePath = path.join(__dirname, '/tmp/')
var organization = ''
const EventEmitter = require('events').EventEmitter
const eventEmitter = new EventEmitter()
// Adding global Exception tracing
process.on('uncaughtException', function onUncaughtException (err) {
console.log('uncaught Exception', err)
})
process.on('unhandledRejection', function onUnhandledRejection (err) {
console.log('unhandled Rejection', err)
})
const getGithubRepoStats = (githubHandler, orgName, repoName) => {
return new Promise((resolve, reject) => {
githubHandler.paged('/repos/' + orgName + '/' + repoName + '/stats/contributors')
.then((res) => {
var aggregatedPagesRecords = [];
for(var i=0;i<res.pages.length;i++){
let interpretedResponse = interpretResponseCode(res.pages[i].statusCode)
if (interpretedResponse === 'OK' && res.pages[i].statusMessage != 'No Content') {
aggregatedPagesRecords = aggregatedPagesRecords.concat(res.pages[i].body);
} else if(interpretedResponse === 'OK' && res.pages[i].statusMessage === 'No Content'){
continue;
} else {
reject({ 'error': interpretedResponse, 'statusCode': res.pages[i].statusCode, 'statusMessage': res.pages[i].statusMessage })
}
}
resolve(aggregatedPagesRecords);
})
.catch((err) => {
reject(err);
});
})
}
const getGithubRepoSummaryStats = (githubHandler, orgName, repoName, isForked) => {
return new Promise((resolve, reject) => {
getGithubRepoStats(githubHandler, orgName, repoName)
.then((data) => {
var contributorsList = []
for (var i = 0; i < data.length; i++) {
var nbOfWeeks = roundedNbOfWeeks
if(!data[i].weeks){
continue;
}
if (data[i].weeks.length < roundedNbOfWeeks) {
nbOfWeeks = data[i].weeks.length
}
var nbOfCommits = 0
for (var j = data[i].weeks.length - nbOfWeeks; j < data[i].weeks.length; j++) {
// weeksList.push(res[i].weeks[j].w+"-#commits "+res[i].weeks[j].c);
if (data[i].weeks[j].c > 0) {
nbOfCommits += data[i].weeks[j].c
}
}
if (nbOfCommits > 0 && isForked === false) {
contributorsList.push({ 'name': data[i].author.login, '# of commits': nbOfCommits })
} else if (nbOfCommits > 0 && isForked === true) {
contributorsList.push({ 'fork': true, 'name': data[i].author.login, '# of commits': nbOfCommits })
}
}
eventEmitter.emit('promiseCompleted', repoName, contributorsList)
resolve(contributorsList)
})
.catch((error) => {
reject(error)
})
})
}
const registerEventListeners = (promiseArray) => {
var lastMessage
// register a listener for the 'randomString' event
eventEmitter.on('promiseCompleted', function (repoName, list) {
console.log(list.length + ' contributors for \t' + repoName)
// repoStatsList.push(list);
fs.readFile(filePath + organization, 'utf8', function (err, contents) {
if (err) {
throw new Error(err.message)
}
var repoListArray = JSON.parse(contents)
var newContent = []
for (var i = 0; i < repoListArray.length; i++) {
if (repoListArray[i].name === repoName) {
// console.log({"name":repoListArray[i].name, "forked":repoListArray[i].forked, "contributorsList":list});
newContent.push({ 'name': repoListArray[i].name, 'forked': repoListArray[i].forked, 'contributorsList': list })
} else {
newContent.push(repoListArray[i])
}
}
fs.writeFile(filePath + organization, JSON.stringify(newContent), function (err) {
if (err) {
throw new Error(err.message)
}
setTimeout(() => { promiseProcess(promiseArray) }, INTER_CALLS_DELAY)
})
})
})
eventEmitter.on('allPromisesCompleted', function () {
if (lastMessage) {
console.log(chalk.red(lastMessage))
} else {
// consolidateContributorsList(repoStatsList);
contributors.processLists(filePath, organization)
}
})
eventEmitter.on('setLastMessage', (message) => {
lastMessage = message
})
// eventEmitter.on('promiseFailed', function (repoName, list, error) {
// console.error("Promise failed");
// //TODO: Handle retry with proper backoff X-retry value or exponential if no retry
// console.error(error);
// });
}
const promiseProcess = (promiseArray) => {
if (promiseArray.length > 0) {
promiseArray[0]()
.then(() => promiseArray.shift())
.catch((error) => {
if (error && error.statusCode === 202) {
console.log(error.error)
eventEmitter.emit('setLastMessage', '\nGithub is processing data, please try again in a moment.\nIt may take multiple runs to go through all the repos.\nPlease rerun until you see the contributors count displayed')
promiseArray.shift()
promiseProcess(promiseArray)
} else if (error && error.statusCode === 403) {
// TODO: if X-Retry header value retry then, otherwise exponential backoff
console.error('403!')
console.error(error)
} else {
console.error(error)
}
// eventEmitter.emit('promiseFailed', repoName, error)
})
} else if (promiseArray.length === 0) {
eventEmitter.emit('allPromisesCompleted')
}
}
const introText = () => {
figlet.text('SNYK', {
font: 'Star Wars',
horizontalLayout: 'default',
verticalLayout: 'default'
}, function (err, data) {
if (err) {
console.log('Something went wrong...')
console.dir(err)
return
}
console.log(data)
console.log('\n')
console.log('Snyk tool for counting active contributors')
console.log('Respecting Github API Rate limiting best practices, so be patient :)')
})
}
program
.version('1.0.0')
.description('Snyk\'s Github contributors counter (active in the last 3 months)')
.usage('<command> [options] \n options: -t <GHToken> (2FA setup) or -u <username> -pwd <password> --private for commands on private repos only \n --apiurl <apiUrl if not https://api.github.com. Includes scheme an api path, ie https://myusualgheurl.company.com/api/v3>')
program
.command('repoList <org>')
.description('List all repos under an organization')
.option('-p, --private', 'private repos only')
.option('-t, --token [GHToken]', 'Running command with Personal Github Token (for 2FA setup)')
.option('-u, --username [username]', 'username (use Token if you set 2FA on Github)')
.option('-pwd, --password [password]', 'password')
.option('-r, --raw', 'Raw output')
.option('-apiurl, --apiurl [apiurl]', 'API url if not https://api.Github.com')
.action((org, options) => {
if (!options.raw) {
introText()
}
var github = githubUtils.authenticate(options)
githubUtils.getGithubOrgList(github, org, options.private)
.then((data) => {
if (!options.raw) {
if (options.private) console.log(chalk.red('\nPrivate Repos Only'))
console.log(chalk.blue('\nTotal # of repos = ' + data.length))
console.log(chalk.blue('\nRepo list:'))
}
var forkedRepos = []
for (var i = 0; i < data.length; i++) {
if (data[i].fork) {
forkedRepos.push(data[i].name)
} else {
console.log(data[i].name)
}
}
if (!options.raw) {
console.log(chalk.blue('\nForked Repo list:'))
}
for (i = 0; i < forkedRepos.length; i++) {
console.log(forkedRepos[i])
}
console.log('\n')
})
.catch((error) => {
console.error(error)
})
})
program
.command('repoContributorCount [org] [repo]')
.description('Count number of active contributors to Github repo')
.option('-t, --token [GHToken]', 'Running command with Personal Github Token (for 2FA setup)')
.option('-u, --username [username]', 'username (use Token if you set 2FA on Github)')
.option('-pwd, --password [password]', 'password')
.option('-r, --raw', 'raw output')
.option('-apiurl, --apiurl [apiurl]', 'API url if not https://api.Github.com')
.action((org, repo, options) => {
if (!options.raw) introText()
var github = githubUtils.authenticate(options)
getGithubRepoStats(github, org, repo)
.then((data) => {
var rawCount = data.length
var contributorsList = []
console.log(data);
for (var i = 0; i < data.length; i++) {
var nbOfWeeks = roundedNbOfWeeks
if (data[i].weeks.length < roundedNbOfWeeks) {
nbOfWeeks = data[i].weeks.length
}
var nbOfCommits = 0
for (var j = data[i].weeks.length - nbOfWeeks; j < data[i].weeks.length; j++) {
// weeksList.push(res[i].weeks[j].w+"-#commits "+res[i].weeks[j].c);
if (data[i].weeks[j].c > 0) {
nbOfCommits += data[i].weeks[j].c
}
}
if (nbOfCommits > 0) {
if (options.raw) {
contributorsList.push(data[i].author.login)
} else {
contributorsList.push({ 'name': data[i].author.login, '# of commits': nbOfCommits })
}
}
}
if (!options.raw) {
console.log(chalk.red('\nTotal active contributors in the last ' + nbOfDays + ' days = ' + contributorsList.length))
console.log(chalk.blue('\nTotal contributors since first commit = ' + rawCount))
console.log(chalk.blue('\nDetails for the last ' + nbOfDays + ' days (rounded at ' + roundedNbOfWeeks + ' weeks): '))
console.log(contributorsList)
console.log('\n')
} else {
console.log(contributorsList)
console.log('\n')
}
})
.catch((error) => {
console.error(error)
})
})
program
.command('orgContributorCount [org]')
.description('Count number of active contributors to Github repo across an entire organization')
.option('-t, --token [GHToken]', 'Running command with Personal Github Token (for 2FA setup)')
.option('-u, --username [username]', 'username (use Token if you set 2FA on Github)')
.option('-pwd, --password [password]', 'password')
.option('-p, --private', 'private repos only')
.option('-apiurl, --apiurl [apiurl]', 'API url if not https://api.github.com')
.action((org, options) => {
introText()
var github = githubUtils.authenticate(options)
var promiseArray = []
organization = org
if (!fs.existsSync(filePath)) {
fs.mkdirSync(filePath)
}
if (fs.existsSync(filePath + organization)) {
console.log(chalk.red('\nWorking off file repoList in tmp folder. Delete file to restart counting from scratch'))
fs.readFile(filePath + organization, 'utf8', function (err, contents) {
if (err) {
throw new Error(err.message)
}
var repoListArray = JSON.parse(contents)
var repoToBeProcessed = []
for (var i = 0; i < repoListArray.length; i++) {
if (!repoListArray[i].hasOwnProperty('contributorsList')) {
repoToBeProcessed.push(repoListArray[i])
}
}
var repoArray = repoToBeProcessed.map(repo => () =>
getGithubRepoSummaryStats(github, org, repo.name, repo.forked)
)
registerEventListeners(repoArray)
promiseProcess(repoArray)
// console.log("done !");
})
} else {
githubUtils.getGithubOrgList(github, org, options.private)
.then((data) => {
if (options.private) console.log(chalk.red('\nPrivate Repos Only'))
console.log(chalk.blue('\nTotal # of repos = ' + data.length))
var repoArray = data.map(repo => { return { 'name': repo.name, 'forked': repo.fork } })
fs.writeFile(filePath + organization, JSON.stringify(repoArray), function (err) {
if (err) {
return console.log(err)
}
})
promiseArray = data.map(repo => () =>
getGithubRepoSummaryStats(github, org, repo.name, repo.fork)
)
registerEventListeners(promiseArray)
promiseProcess(promiseArray)
})
.catch((err) => { console.error(err) })
}
})
program.parse(process.argv)
if (program.args.length === 0) program.help()
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
}