<commit_before>var GitHubApi = require('github');
var config = require('config');
var npm = require('npm');
var semver = require('semver');

var github = new GitHubApi({version: '3.0.0'});

if (config.github) {
	github.authenticate({
		type: 'basic',
		username: config.github.username,
		password: <PASSWORD>.github.password
	});
}

// Get a username and repo name for a github repository
function getUserRepo (depName, cb) {
	
	npm.commands.view([depName, 'repository'], true, function(er, data) {
		if (er) {
			return cb(er);
		}
		
		var keys = Object.keys(data);
		var repo = keys.length ? data[keys[0]].repository : null;
		
		if (!repo) {
			return cb(new Error(depName + ' has no repository information'));
		}
		
		var url = Object.prototype.toString.call(data) == '[object String]' ? repo : repo.url;
		
		if (!url || url.indexOf('github.com') == -1) {
			return cb(new Error('Unsupported repository URL'));
		} 
		
		// TODO: Extract user/repo from url
	});
}

/**
 * Get commits for a repo between two dates
 * 
 * @param depName
 * @param from
 * @param to
 * @param cb
 */
module.exports.getCommits = function (depName, from, to, cb) {
	
	npm.load({}, function (er) {
		if (er) {
			return cb(er);
		}
		
		getUserRepo(depName, function (er, user, repo) {
			if (er) {
				return cb (er);
			}
			
			// TODO: Munge the commits data?
			github.repos.getCommits({user: user, repo: repo, since: from, until: to}, cb);
		});
	});
};

/**
 * Get the publish date for a module at a particular version (or range)
 * 
 * @param {String} depName Module name
 * @param {String} depVersion Module version. If range then publish date for lowest satisfying version is given.
 * @param {Function} cb
 */
module.exports.getPublishDate = function (depName, depVersion, cb) {
	
	npm.load({}, function (er) {
		if (er) {
			return cb(er);
		}
		
		npm.commands.view([depName, 'time'], true, function (er, data) {
			if (er) {
				return cb(er);
			}
			
			var keys = Object.keys(data);
			var time = keys.length ? data[keys[0]].time : null;
			<commit_msg>Parse the repository URL to get github username and repo name<commit_after>var GitHubApi = require('github');
var config = require('config');
var npm = require('npm');
var semver = require('semver');
var url = require('url');

var github = new GitHubApi({version: '3.0.0'});

if (config.github) {
	github.authenticate({
		type: 'basic',
		username: config.github.username,
		password: <PASSWORD>.github.password
	});
}

// Get a username and repo name for a github repository
function getUserRepo (depName, cb) {
	
	npm.commands.view([depName, 'repository'], true, function(er, data) {
		if (er) {
			return cb(er);
		}
		
		var keys = Object.keys(data);
		var repo = keys.length ? data[keys[0]].repository : null;
		
		if (!repo) {
			return cb(new Error(depName + ' has no repository information'));
		}
		
		var repoUrl = Object.prototype.toString.call(data) == '[object String]' ? repo : repo.url;
		
		if (!repoUrl || repoUrl.indexOf('github.com') == -1) {
			return cb(new Error('Unsupported repository URL'));
		} 
		
		try {
			var repoPathname = url.parse(repoUrl).pathname.split('/');
			cb(null, repoPathname[1], repoPathname[2].replace('.git', ''));
		} catch (er) {
			cb(new Error('Failed to parse repository URL'));
		}
	});
}

/**
 * Get commits for a repo between two dates
 * 
 * @param {String} depName Module name
 * @param {Date} from
 * @param {Date} to
 * @param {Function} cb
 */
module.exports.getCommits = function (depName, from, to, cb) {
	
	npm.load({}, function (er) {
		if (er) {
			return cb(er);
		}
		
		getUserRepo(depName, function (er, user, repo) {
			if (er) {
				return cb(er);
			}
			
			// TODO: Munge the commits data?
			github.repos.getCommits({user: user, repo: repo, since: from, until: to}, cb);
		});
	});
};

/**
 * Get the publish date for a module at a particular version (or range)
 * 
 * @param {String} depName Module name
 * @param {String} depVersion Module version. If range then publish date for lowest satisfying version is given.
 * @param {Function} cb
 */
module.exports.getPublishDate = function (depName, depVersion, cb) {
	
	npm.load({}, function (er) {
		if (er) {
			return cb(er);
		}
		
		npm.commands.view([depName, 'time'], true, function (er, data) {
			if (er) {
				return cb(er);
			}
			
			var keys = Object.keys(data);
			var time = keys.length ? data[keys[0]].time : null;
			
{'fraction_non_alphanumeric': 0.15975526852481306, 'fraction_numerical': 0.0031724450487196918, 'mean_word_length': 3.392039800995025, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2165700', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1449, 'n_words': 442}