forked from aarlaud-snyk/github-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·525 lines (450 loc) · 18 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#!/usr/bin/env node
const INTER_CALLS_DELAY = 1000;
var program = require('commander');
const chalk = require('chalk');
var GitHub = require('github-base');
const figlet = require('figlet');
var fs = require('fs');
var nbOfDays = 90;
var roundedNbOfWeeks = Math.floor(nbOfDays/7);
var filePath = __dirname+'/tmp/';
var organization = "";
const EventEmitter = require('events').EventEmitter;
const eventEmitter = new EventEmitter();
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){
var githubHandler = new GitHub({
token: options.token,
apiurl: apiurl,
});
} else if(options.username && options.password){
var githubHandler = new GitHub({
username: username,
password: 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, function (err, res, stream) {
if(err){
reject(err);
} else {
let interpretedResponse = interpretResponseCode(stream.statusCode);
if(interpretedResponse == 'OK'){
resolve(res);
} else {
reject(interpretedResponse);
}
}
});
});
}
const getGithubRepoStats = (githubHandler, orgName, repoName) => {
return new Promise((resolve, reject) => {
githubHandler.paged('/repos/'+orgName+'/'+repoName+'/stats/contributors', function (err, res, stream) {
if(err){
reject(err);
} else {
let interpretedResponse = interpretResponseCode(stream.statusCode);
if(interpretedResponse == 'OK'){
resolve(res);
} else {
console.error("Issue with "+orgName+"/"+repoName);
reject({"error":interpretedResponse, "statusCode":stream.statusCode, "headers":stream.headers});
}
}
});
});
}
const getGithubRepoSummaryStats = (githubHandler, orgName, repoName,isForked) => {
return new Promise((resolve,reject) => {
getGithubRepoStats(githubHandler, orgName, repoName)
.then((data) => {
var rawCount = data.length;
var contributorsList = [];
for(i=0;i<data.length;i++){
var nbOfWeeks = roundedNbOfWeeks;
if(data[i].weeks.length < roundedNbOfWeeks){
nbOfWeeks = data[i].weeks.length;
}
var nbOfCommits = 0;
for(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 repoStatsList = [];
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) {
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) {
return console.log(err);
}
setTimeout(() => {promiseProcess(promiseArray)}, INTER_CALLS_DELAY);
});
});
//promiseProcess(promiseArray);
});
eventEmitter.on('allPromisesCompleted', function () {
if(lastMessage){
console.log(lastMessage);
} else {
//consolidateContributorsList(repoStatsList);
consolidateContributorsList();
}
});
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');
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 consolidateContributorsList = (data = null) => {
var contributorsList = [];
var forkcontributorsList = [];
fs.readFile(filePath+organization, 'utf8', function(err, contents) {
var data = JSON.parse(contents);
for(i=0;i<data.length;i++){
for(j=0;j<data[i].contributorsList.length;j++){
if(data[i].forked){
if(data[i].contributorsList[j].name in forkcontributorsList){
let commitCount = forkcontributorsList[data[i].contributorsList[j].name]['# of commits'];
forkcontributorsList[data[i].contributorsList[j].name] = {'# of commits': commitCount + data[i].contributorsList[j]['# of commits']};
} else {
forkcontributorsList[data[i].contributorsList[j].name] = {"# of commits": data[i].contributorsList[j]['# of commits']}
}
} else {
if(data[i].contributorsList[j].name in contributorsList){
let commitCount = contributorsList[data[i].contributorsList[j].name]['# of commits'];
contributorsList[data[i].contributorsList[j].name] = {'# of commits': commitCount + data[i].contributorsList[j]['# of commits']};
} else {
contributorsList[data[i].contributorsList[j].name] = {"# of commits": data[i].contributorsList[j]['# of commits']}
}
}
}
}
var contributorsCount = 0;
for (x in contributorsList)
{ contributorsCount++; }
console.log(chalk.red("\nTotal active contributors with commit in the last "+ nbOfDays + " days (rounded at "+roundedNbOfWeeks+" weeks) = "+ contributorsCount+"\n"));
if(contributorsCount > 0){
console.log(chalk.blue("Contributors List:"));
console.log(contributorsList);
console.log("\n");
}
var forkContributorsCount = 0;
for (x in forkcontributorsList)
{ forkContributorsCount++; }
console.log(chalk.red("\nTotal forked repo active contributors with commit in the last "+ nbOfDays + " days (rounded at "+roundedNbOfWeeks+" weeks) = "+ forkContributorsCount+"\n"));
if(contributorsCount > 0){
console.log(chalk.blue("Fork Contributors List:"));
console.log(forkcontributorsList);
console.log("\n");
}
});
// for(i=0;i<data.length;i++){
//
// for(j=0;j<data[i].length;j++){
// if(data[i][j].fork){
// if(data[i][j].name in forkcontributorsList){
// let commitCount = forkcontributorsList[data[i][j].name]['# of commits'];
// forkcontributorsList[data[i][j].name] = {'# of commits': commitCount + data[i][j]['# of commits']};
// } else {
// forkcontributorsList[data[i][j].name] = {"# of commits": data[i][j]['# of commits']}
// }
// } else {
// if(data[i][j].name in contributorsList){
// let commitCount = contributorsList[data[i][j].name]['# of commits'];
// contributorsList[data[i][j].name] = {'# of commits': commitCount + data[i][j]['# of commits']};
// } else {
// contributorsList[data[i][j].name] = {"# of commits": data[i][j]['# of commits']}
// }
// }
// }
//
// }
// var contributorsCount = 0;
// for (x in contributorsList)
// { contributorsCount++; }
// console.log(chalk.red("\nTotal active contributors with commit in the last "+ nbOfDays + " days (rounded at "+roundedNbOfWeeks+" weeks) = "+ contributorsCount+"\n"));
//
// if(contributorsCount > 0){
// console.log(chalk.blue("Contributors List:"));
// console.log(contributorsList);
// console.log("\n");
// }
//
// var forkContributorsCount = 0;
// for (x in forkcontributorsList)
// { forkContributorsCount++; }
// console.log(chalk.red("\nTotal forked repo active contributors with commit in the last "+ nbOfDays + " days (rounded at "+roundedNbOfWeeks+" weeks) = "+ forkContributorsCount+"\n"));
//
// if(contributorsCount > 0){
// console.log(chalk.blue("Fork Contributors List:"));
// console.log(forkcontributorsList);
// console.log("\n");
// }
}
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 = authenticate(options);
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 forked_repos = [];
for(i=0;i<data.length;i++){
if(data[i].fork){
forked_repos.push(data[i].name);
} else {
console.log(data[i].name);
}
}
if(!options.raw){
console.log(chalk.blue("\nForked Repo list:"));
}
for(i=0;i<forked_repos.length;i++){
console.log(forked_repos[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 = authenticate(options);
getGithubRepoStats(github, org, repo)
.then((data) => {
var rawCount = data.length;
var contributorsList = [];
for(i=0;i<data.length;i++){
var nbOfWeeks = roundedNbOfWeeks;
if(data[i].weeks.length < roundedNbOfWeeks){
nbOfWeeks = data[i].weeks.length;
}
var nbOfCommits = 0;
for(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 = 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) {
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 {
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;
}