Get list of github contributors in node.js

Recently I needed to send emails to all contributors to my github project, so I wrote a little script in node.js (actually my first try was ruby, but have problems with https). Here is the script using only native modules:


#!/usr/bin/node

var https = require('https');
var path = require('path');
function get(host, path, callback) {
    var options = {
        host: host,
        path: path,
        headers: {
            'User-Agent': 'Node.js' //required by github api
        }
    }
    https.get(options, function(res) {
        var output = '';
        res.setEncoding('utf8');

        res.on('data', function (chunk) {
            output += chunk;
        });

        res.on('end', function() {
            callback(JSON.parse(output));
        });
    });
}

if (process.argv.length == 4) {
    var user = process.argv[2];
    var repo = process.argv[3];
    var path = '/repos/' + user + '/' + repo + '/contributors'
    get('api.github.com', path, function(contributors) {
        contributors.forEach(function(contributor) {
            if (contributor.login != user) {
                var path = contributor.url.replace(/https:\/\/[^\/]+/, '');
                get('api.github.com', path, function(user) {
                    if (user.name) {
                        var email = user.email ? ' <' + user.email + '>' : ''; 
                        console.log(user.name + email);
                    }
                });
            }
        });
    });
} else {
    console.log('usage: \n' + path.basename(process.argv[1]) + ' user repo');
}

, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: