<commit_before>'use strict';

var githubRepos = require('github-repositories');
var githubTokenUser = require('github-token-user');
var got = require('got');
var eachAsync = require('each-async');
var objectAssign = require('object-assign');

function addCollab(user, repo, opts, cb) {
	var url = [
		'https://api.github.com/repos/' + repo + '/',
		'collaborators/' + user
	].join('');

	got.put(url, {headers: opts.headers}, function (err) {
		if (err) {
			cb(err);
			return;
		}

		cb();
	});
}

function run(user, repos, opts, cb) {
	eachAsync(repos, function (repo, i, next) {
		addCollab(user, repo, opts, next);
	}, function (err) {
		if (err) {
			cb(err);
			return;
		}

		cb(null, repos);
	});
}

function getRepos(user, login, opts, cb) {
	githubRepos(login, {token: opts.token}, function (err, data) {
		if (err) {
			cb(err);
			return;
		}

		if (opts.addToSources) {
			data = data.filter(function (el) {
				return !el.fork;
			});
		}

		data = data.map(function (el) {
			return el.full_name;
		});

		run(user, data, opts, cb);
	});
}

module.exports = function (user, repos, opts, cb) {
	if (typeof user !== 'string') {
		throw new Error('User required');
	}

	if (!cb && typeof repos === 'object') {
		cb = opts;
		opts = repos;
		repos = [];
	}

	if (!Array.isArray(repos)) {
		throw new Error('Expected an array');
	}

	if (!opts.token) {
		throw new Error('Token is required to authenticate with Github');
	}

	opts = objectAssign({}, opts);
	opts.headers = {
		Accept: 'application/vnd.github.v3+json',
		Authorization: 'token ' + opts.token,
		'Content-Length': 0,
		'User-Agent': 'https://github.com/kevva/github-add-collab'
	};<commit_msg>Throw if `addToAll` or `addToSources` is used with `repos`<commit_after>'use strict';

var githubRepos = require('github-repositories');
var githubTokenUser = require('github-token-user');
var got = require('got');
var eachAsync = require('each-async');
var objectAssign = require('object-assign');

function addCollab(user, repo, opts, cb) {
	var url = [
		'https://api.github.com/repos/' + repo + '/',
		'collaborators/' + user
	].join('');

	got.put(url, {headers: opts.headers}, function (err) {
		if (err) {
			cb(err);
			return;
		}

		cb();
	});
}

function run(user, repos, opts, cb) {
	eachAsync(repos, function (repo, i, next) {
		addCollab(user, repo, opts, next);
	}, function (err) {
		if (err) {
			cb(err);
			return;
		}

		cb(null, repos);
	});
}

function getRepos(user, login, opts, cb) {
	githubRepos(login, {token: opts.token}, function (err, data) {
		if (err) {
			cb(err);
			return;
		}

		if (opts.addToSources) {
			data = data.filter(function (el) {
				return !el.fork;
			});
		}

		data = data.map(function (el) {
			return el.full_name;
		});

		run(user, data, opts, cb);
	});
}

module.exports = function (user, repos, opts, cb) {
	if (typeof user !== 'string') {
		throw new Error('User required');
	}

	if (!cb && typeof repos === 'object') {
		cb = opts;
		opts = repos;
		repos = [];
	}

	if (!Array.isArray(repos)) {
		throw new Error('Expected an array');
	}

	if (!opts.token) {
		throw new Error('Token is required to authenticate with Github');
	}

	if (repos.length && (opts.addToAll || opts.addToSources)) {
		throw new Error('`addToAll` and `addToSources` can not be used with `repos`');
	}

	opts = objectAssign({}, opts);
	opts.headers = {
		Accept: 'application/vnd.github.v3+json',
		Authorization: 'token ' + opts.token,
		'Content-Length': 0,
		'User-Agent': 'https://github.com/kevva/github-add-collab'
	};
{'fraction_non_alphanumeric': 0.18702181921224142, 'fraction_numerical': 0.0011334655709832814, 'mean_word_length': 3.608355091383812, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '689449', 'n_tokens_mistral': 1427, 'n_tokens_neox': 1279, 'n_words': 317}