Skip to content

Instantly share code, notes, and snippets.

@nw
Created October 14, 2016 20:25
Show Gist options
  • Save nw/e3d477272946b4b038905783cfa45b5e to your computer and use it in GitHub Desktop.
Save nw/e3d477272946b4b038905783cfa45b5e to your computer and use it in GitHub Desktop.
// Usage: node getStats.js yarnpkg/yarn '2016-10-11T14:50:00.000Z' username password
// Note: install `async` and `github` packages
var async = require('async')
, GitHubApi = require("github");
var github = new GitHubApi({
// required
version: "3.0.0",
protocol: "https",
timeout: 5000,
})
let [owner, repo] = process.argv[2].split('/')
let since = process.argv[3] // '2016-10-11T14:50:00.000Z'
github.authenticate({
type: "basic",
username: process.argv[4],
password: process.argv[5]
});
let issues_nums = []
let users = {}
let opened = 0;
let closed = 0;
let comments = 0;
let state = {};
let prs = 0;
let prstate = {};
github.issues.getForRepo({
owner: owner
, repo: repo
, since: since
, state: 'all'
}, function(err, resp){
if(err) return console.log(err);
parse_issues(resp)
})
function parse_issues(resp) {
resp.forEach(function(i){
if(!state[i.state]) state[i.state] = 0;
state[i.state]++;
issues_nums.push(i.number);
if(!users[i.user.login]) users[i.user.login] = 0;
users[i.user.login]++
});
if(github.hasNextPage(resp)){
github.getNextPage(resp, function(err, resp){
if(err) return console.log(err);
parse_issues(resp)
})
} else {
getComments()
}
}
function getComments() {
async.each(issues_nums, function(num, cb){
github.issues.getComments({
owner: owner
, repo: repo
, since: since
, number: num
}, function(err, resp){
if(err) return cb(err);
parseComment(resp, cb);
})
}, function(err){
getPRs();
})
}
function parseComment(resp, cb) {
resp.forEach(function(c){
comments++;
if(!users[c.user.login]) users[c.user.login] = 0;
users[c.user.login]++
});
if(github.hasNextPage(resp)) {
github.getNextPage(resp, function(err, resp){
if(err) return cb(err);
parseComment(resp, cb)
})
} else {
return cb()
}
}
function getPRs() {
github.pullRequests.getAll({
owner: owner,
repo: repo,
state: 'all',
sort: 'updated',
direction: 'desc'
}, function(err, resp){
if(err) return console.log(err);
parsePR(resp);
})
}
function parsePR(resp) {
let _last = false
resp.forEach(function(p){
if(p.updated_at > since) {
_last = true;
prs++;
if(!prstate[p.state]) prstate[p.state] = 0;
prstate[p.state]++;
} else _last = false;
});
if(_last && github.hasNextPage(resp)) {
github.getNextPage(resp, function(err, resp){
if(err) return console.log(err);
parsePR(resp)
})
} else {
console.log(issues_nums.length);
console.log(Object.keys(users).length, 'users');
console.log(state);
console.log(comments, 'comments');
console.log(prs, 'PRs');
console.log(prstate)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment