<commit_before>'use strict';

var debug = require('debug')('githook:github'),
    Promise = require('bluebird'),
    Netmask = require('netmask').Netmask;

var Github = function () {};

/*
 * verify the origin of the hook
 * call https://api.github.com/meta
 *
 *   {
 *     "verifiable_password_authentication": true,
 *     "hooks": [
 *       "172.16.31.10/22"
 *     ],
 *     "git": [
 *       "172.16.31.10/22"
 *     ]
 *   }
 */
Github.prototype.checkOrigin = function (ip) {

    return new Promise(function (resolve, reject) {

        var request = require('superagent');

        // download current list from https://api.github.com/meta
        request
            .get('https://api.github.com/meta')
            .set('Accept', 'application/json')
            .end(function (res) {
                if (res.error) {
                    reject(res.error.message);
                } else {
                    // extract hooks
                    var hooks = res.body.hooks;
                    debug('verify ' + ip + ' against ' + JSON.stringify(hooks));

                    var contains = false;

                    // check if the ip is in github range
                    hooks.forEach(function (iprange) {
                        var block = new Netmask(iprange);
                        if (block.contains(ip)) {
                            contains = true;
                        }
                    });

                    if (contains) {
                        debug('ip adress verification successful');
                        resolve();
                    } else {
                        debug('ip adress verification failed');
                        reject();
                    }
                }
            });
    });
};

Github.prototype.checkSignature = function () {
    return new Promise(function (resolve) {
        debug('signature check successful');
        resolve();
    });
};

Github.prototype.verify = function (data) {
    debug('verify github hook');
    return Promise.all([this.checkOrigin(data.ip), this.checkSignature()]);
};

/*
 * extract data from pull request
 */
Github.prototype.extractPushEvent = function (data)  {
    debug('extract github push event');
    return new Promise(function (resolve) {
        // extract commit messages
        var commits = [];<commit_msg>add support for authenticated github requests<commit_after>'use strict';

var debug = require('debug')('githook:github'),
    Promise = require('bluebird'),
    Netmask = require('netmask').Netmask;

var Github = function (options) {
    this.options = options || {};
};

/*
 * verify the origin of the hook
 * call https://api.github.com/meta
 *
 *   {
 *     "verifiable_password_authentication": true,
 *     "hooks": [
 *       "172.16.31.10/22"
 *     ],
 *     "git": [
 *       "172.16.31.10/22"
 *     ]
 *   }
 */
Github.prototype.checkOrigin = function (ip) {
    var self = this;
    return new Promise(function (resolve, reject) {

        var request = require('superagent');

        // download current list from https://api.github.com/meta
        var req = request.get('https://api.github.com/meta');

        req.set('Accept', 'application/json');
        if (self.options.token) {
            req.set('Authorization', 'token ' + self.options.token);
        }
        req.end(function (res) {
                if (res.error) {
                    reject(res.error.message);
                } else {
                    // extract hooks
                    var hooks = res.body.hooks;
                    debug('verify ' + ip + ' against ' + JSON.stringify(hooks));

                    var contains = false;

                    // check if the ip is in github range
                    hooks.forEach(function (iprange) {
                        var block = new Netmask(iprange);
                        if (block.contains(ip)) {
                            contains = true;
                        }
                    });

                    if (contains) {
                        debug('ip adress verification successful');
                        resolve();
                    } else {
                        debug('ip adress verification failed');
                        reject();
                    }
                }
            });
    });
};

Github.prototype.verify = function (data) {
    debug('verify github hook');
    return Promise.all([this.checkOrigin(data.ip)]);
};

/*
 * extract data from pull request
 */
Github.prototype.extractPushEvent = function (data)  {
    debug('extract github push event');
    return new Promise(function (resolve) {
        // extract commit messages
        var commits = [];
{'fraction_non_alphanumeric': 0.1281449893390192, 'fraction_numerical': 0.009381663113006396, 'mean_word_length': 1.482010582010582, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1956030', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1177, 'n_words': 276}