<commit_before>var GitHubApi = require('github'),
    Travis = require('travis-ci'),
    _ = require('underscore'),
    log = require('./logger').logger,
    RepositoryClient;

function arrayContainsArray(left, right) {
    return _.intersection(left, right).length == right.length;
}

/**
 * An interface to the Github repository. Uses the Github API.
 */
function RepositoryClient(config) {
    this.user = config.username;
    this.password = <PASSWORD>;
    this.org = config.organization;
    this.repo = config.repository;
    this.contributorsUrl = config.contributors;
    this.github = new GitHubApi({
        version: '3.0.0',
        timeout: 5000
    });
    this.github.authenticate({
        type: 'basic',
        username: this.user,
        password: <PASSWORD>
    });
    this.travis = new Travis({ version: '2.0.0' });
    this.travis.authenticate({
        username: this.user,
        password: <PASSWORD>
    }, function() {});
    // Store configured validators.
    this.validators = config.validators;
    // Store configured hooks.
    if (config.hasOwnProperty('hooks')) {
        this.hooks = config.hooks;
    } else {
        this.hooks = {};
    }
}

RepositoryClient.prototype.merge = function(head, base, callback) {
    log.log('merging ' + head + ' into ' + base + '...');
    this.github.repos.merge({
        user: this.org,
        repo: this.repo,
        base: base,
        head: head
    }, callback);
};

RepositoryClient.prototype.isBehindMaster = function(sha, callback) {
    this.github.repos.compareCommits({
        user: this.org,
        repo: this.repo,
        base: 'master',
        head: sha
    }, function(err, data) {
        if (err) {
            callback(err);
        } else {
            callback(err, data.behind_by > 0, data.behind_by);
        }
    });
};

RepositoryClient.prototype.getAllOpenPullRequests = function(callback) {
    this.github.pullRequests.getAll({
        user: this.org,
        repo: this.repo,
        state: 'open'
    }, callback);
};

RepositoryClient.prototype.getContributors = function(callback) {
    var me = this;
    me.github.repos.getContributors({
        user: me.org,
        repo: me.repo
    }, function(err, contributors) {
        if (err) {
            callback(err);
        } else {
            me._getRemainingPages(contributors, null, callback);
        }
    });
};

RepositoryClient.prototype.getCommits = function(callback) {
    var me = this;
    me.github.repos.getCommits({
        user: me.org,
        repo: me.repo
    }, function(err, commits) {
        if (err) {
            callback(err);
        } else {
            me._getRemainingPages(commits, null, callback);
        }
    });
};

RepositoryClient.prototype.getAllStatusesFor = function(sha, callback) {
    this.github.statuses.get({
        user: this.org,
        repo: this.repo,
        sha: sha
    }, function(err, statuses) {
        callback(err, (statuses || []));
    });
};

RepositoryClient.prototype.getCommit = function(sha, callback) {
    this.github.repos.getCommit({
        user: this.org,
        repo: this.repo,
        sha: sha
    }, callback);
};

RepositoryClient.prototype.rateLimit = function(callback) {
    this.github.misc.rateLimit({
        user: this.org,
        repo: this.repo
    }, callback);
};

RepositoryClient.prototype.confirmWebhookExists = function(url, events, callback) {
    var me = this;
    log.verbose('%s/%s web hook check', this.org, this.repo);
    this.github.repos.getHooks({
        user: this.org,
        repo: this.repo
    }, function(err, hooks) {
        var found = false;
        if (err) {
            console.error(err);
            return callback(err);
        }
        hooks.forEach(function(hook) {
            if (hook.config && url == hook.config.url && arrayContainsArray(hook.events, events)) {
                log.verbose(hook.config.url);
                found = true;
            }
        });
        if (! found) {
            log.debug('Creating web hook for "%s"', events.join(', '));
            me.github.repos.createHook({
                user: me.org,
                repo: me.repo,
                name: 'web',
                config: {
                    url: url
                },
                events: events
            }, function(err, data) {
                if (err) {
                    return callback(err);
                }<commit_msg>Removes webhooks that don't match event signature.<commit_after>var GitHubApi = require('github'),
    Travis = require('travis-ci'),
    _ = require('underscore'),
    log = require('./logger').logger,
    RepositoryClient;

function arrayContainsArray(left, right) {
    return _.intersection(left, right).length == right.length;
}

/**
 * An interface to the Github repository. Uses the Github API.
 */
function RepositoryClient(config) {
    this.user = config.username;
    this.password = <PASSWORD>;
    this.org = config.organization;
    this.repo = config.repository;
    this.contributorsUrl = config.contributors;
    this.github = new GitHubApi({
        version: '3.0.0',
        timeout: 5000
    });
    this.github.authenticate({
        type: 'basic',
        username: this.user,
        password: <PASSWORD>
    });
    this.travis = new Travis({ version: '2.0.0' });
    this.travis.authenticate({
        username: this.user,
        password: <PASSWORD>
    }, function() {});
    // Store configured validators.
    this.validators = config.validators;
    // Store configured hooks.
    if (config.hasOwnProperty('hooks')) {
        this.hooks = config.hooks;
    } else {
        this.hooks = {};
    }
}

RepositoryClient.prototype.merge = function(head, base, callback) {
    log.log('merging ' + head + ' into ' + base + '...');
    this.github.repos.merge({
        user: this.org,
        repo: this.repo,
        base: base,
        head: head
    }, callback);
};

RepositoryClient.prototype.isBehindMaster = function(sha, callback) {
    this.github.repos.compareCommits({
        user: this.org,
        repo: this.repo,
        base: 'master',
        head: sha
    }, function(err, data) {
        if (err) {
            callback(err);
        } else {
            callback(err, data.behind_by > 0, data.behind_by);
        }
    });
};

RepositoryClient.prototype.getAllOpenPullRequests = function(callback) {
    this.github.pullRequests.getAll({
        user: this.org,
        repo: this.repo,
        state: 'open'
    }, callback);
};

RepositoryClient.prototype.getContributors = function(callback) {
    var me = this;
    me.github.repos.getContributors({
        user: me.org,
        repo: me.repo
    }, function(err, contributors) {
        if (err) {
            callback(err);
        } else {
            me._getRemainingPages(contributors, null, callback);
        }
    });
};

RepositoryClient.prototype.getCommits = function(callback) {
    var me = this;
    me.github.repos.getCommits({
        user: me.org,
        repo: me.repo
    }, function(err, commits) {
        if (err) {
            callback(err);
        } else {
            me._getRemainingPages(commits, null, callback);
        }
    });
};

RepositoryClient.prototype.getAllStatusesFor = function(sha, callback) {
    this.github.statuses.get({
        user: this.org,
        repo: this.repo,
        sha: sha
    }, function(err, statuses) {
        callback(err, (statuses || []));
    });
};

RepositoryClient.prototype.getCommit = function(sha, callback) {
    this.github.repos.getCommit({
        user: this.org,
        repo: this.repo,
        sha: sha
    }, callback);
};

RepositoryClient.prototype.rateLimit = function(callback) {
    this.github.misc.rateLimit({
        user: this.org,
        repo: this.repo
    }, callback);
};

RepositoryClient.prototype.confirmWebhookExists = function(url, events, callback) {
    var me = this;
    log.info('%s/%s web hook check', this.org, this.repo);
    this.github.repos.getHooks({
        user: this.org,
        repo: this.repo
    }, function(err, hooks) {
        var found = false;
        if (err) {
            console.error(err);
            return callback(err);
        }
        hooks.forEach(function(hook) {
            if (hook.config && url == hook.config.url) {
                log.warn(hook.events);
                log.warn(events);
                // So there is a webhook for this repo, but it might not have the events we want.
                if (arrayContainsArray(hook.events, events)) {
                    found = true;
                } else {
                    // Remove the old webhook
                    log.warn('Removing old webhook for %s.', url);
                    me.github.repos.deleteHook({
                        user: me.org,
                        repo: me.repo,
                        id: hook.id
                    });
                }
            }
        });
        if (! found) {
            log.warn('Creating web hook for "%s"', events.join(', '));
            me.github.repos.createHook({
                user: me.org,
                repo: me.repo,
                name: 'web',
                config: {
                    url: url
                },
                events: events
            }, function(err, data) {
                if (err) {
                    return callback(err);
                }
{'fraction_non_alphanumeric': 0.1334470262204221, 'fraction_numerical': 0.0023449157962055, 'mean_word_length': 1.860670731707317, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '6774713', 'n_tokens_mistral': 2782, 'n_tokens_neox': 2709, 'n_words': 567}