<commit_before>/**
 * Created by desaroger on 25/02/17.
 */

let _ = require('lodash');
let co = require('co');
let childProcess = require('child_process');
let WebhookServer = require('./WebhookServer');
let { log, c } = require('./utils');

class Pm2Module {

    constructor(processes = [], options = {}) {
        options.routes = Pm2Module._parseProcesses(processes);
        this.routes = options.routes;
        this.webhookServer = new WebhookServer(options);
    }

    start() {
        return this.webhookServer.start()
            .then(() => {
                let msg = 'Started. Routes:\n';
                _.forOwn(this.routes, (route, name) => {
                    msg += ` - ${name}: ${JSON.stringify(route)}\n`;
                });
                log(msg);
            });
    }

    stop() {
        return this.webhookServer.stop()
            .then(() => {
                log('Stopped.');
            });
    }

    /**
     * Converts an array of PM2 processes to an object structured
     * for the WebhookServer routes. It internally uses the _parseProcess
     * method
     *
     * Example 1:
     * - input:
     * [
     *      { pm2_env: { env_hook: { name: 'api', type: 'bitbucket' } } },
     *      { pm2_env: { env_hook: { name: 'panel', type: 'github' } } }
     * ]
     * - output:
     * {
     *      api: { type: 'bitbucket' },
     *      panel: { type: 'github' }
     * }
     *
     * @param processes
     * @returns {*}
     * @private
     */
    static _parseProcesses(processes) {
        return processes
            .map(p => Pm2Module._parseProcess(p))
            .filter(p => !!p)
            .reduce((routes, app) => {
                routes[app.name] = app;
                delete app.name;
                return routes;
            }, {});
    }

    /**
     * Converts a PM2 process object to an object for WebhookServer
     * route.
     *
     * Example 1:
     * - input: { pm2_env: { env_hook: { name: 'api', type: 'bitbucket' } } }
     * - output: { name: 'api', type: 'bitbucket' }
     * Example 2:
     * - input: { pm2_env: { env_hook: { type: 'bitbucket' } } }
     * - output: { name: 'unknown', type: 'bitbucket' }
     *
     * @param process The Pm2 process
     * @returns {object|null} The route object, or null if invalid
     * @private
     */
    static _parseProcess(app) {
        // Check data
        if (!app) {
            return null;
        }
        console.log('');
        console.log('app', app);
        console.log('');
        let processOptions = _.get(app, 'pm2_env.env_hook');
        if (!processOptions) {
            console.log('nope');
            return null;
        }
        console.log('found', processOptions);
        let data = _.get(app, 'pm2_env.env_hook');
        if (data === true) {
            data = {};
        }

        // Data to WebhookServer route
        let self = this;
        let name = app.name || 'unknown';
        let commandOptions = Object.assign({}, { cwd: data.cwd || app.pm_cwd }, data.commandOptions || {});
        let route = {
            name,
            type: data.type,
            method: c(function* () {
                try {
                    if (data.command) {
                        log(`Running command: ${data.command}`);<commit_msg>[wip] Tests on real webhook<commit_after>/**
 * Created by desaroger on 25/02/17.
 */

let _ = require('lodash');
let co = require('co');
let childProcess = require('child_process');
let WebhookServer = require('./WebhookServer');
let { log, c } = require('./utils');

class Pm2Module {

    constructor(processes = [], options = {}) {
        options.routes = Pm2Module._parseProcesses(processes);
        this.routes = options.routes;
        this.webhookServer = new WebhookServer(options);
    }

    start() {
        return this.webhookServer.start()
            .then(() => {
                let msg = 'Started. Routes:\n';
                _.forOwn(this.routes, (route, name) => {
                    msg += ` - ${name}: ${JSON.stringify(route)}\n`;
                });
                log(msg);
            });
    }

    stop() {
        return this.webhookServer.stop()
            .then(() => {
                log('Stopped.');
            });
    }

    /**
     * Converts an array of PM2 processes to an object structured
     * for the WebhookServer routes. It internally uses the _parseProcess
     * method
     *
     * Example 1:
     * - input:
     * [
     *      { pm2_env: { env_hook: { name: 'api', type: 'bitbucket' } } },
     *      { pm2_env: { env_hook: { name: 'panel', type: 'github' } } }
     * ]
     * - output:
     * {
     *      api: { type: 'bitbucket' },
     *      panel: { type: 'github' }
     * }
     *
     * @param processes
     * @returns {*}
     * @private
     */
    static _parseProcesses(processes) {
        return processes
            .map(p => Pm2Module._parseProcess(p))
            .filter(p => !!p)
            .reduce((routes, app) => {
                routes[app.name] = app;
                delete app.name;
                return routes;
            }, {});
    }

    /**
     * Converts a PM2 process object to an object for WebhookServer
     * route.
     *
     * Example 1:
     * - input: { pm2_env: { env_hook: { name: 'api', type: 'bitbucket' } } }
     * - output: { name: 'api', type: 'bitbucket' }
     * Example 2:
     * - input: { pm2_env: { env_hook: { type: 'bitbucket' } } }
     * - output: { name: 'unknown', type: 'bitbucket' }
     *
     * @param process The Pm2 process
     * @returns {object|null} The route object, or null if invalid
     * @private
     */
    static _parseProcess(app) {
        // Check data
        if (!app) {
            return null;
        }
        let processOptions = _.get(app, 'pm2_env.env_hook');
        if (!processOptions) {
            console.log('nope');
            return null;
        }
        console.log('options', processOptions);
        let data = _.get(app, 'pm2_env.env_hook');
        if (data === true) {
            data = {};
        }

        // Data to WebhookServer route
        let self = this;
        let name = app.name || 'unknown';
        let commandOptions = Object.assign({}, { cwd: data.cwd || app.pm_cwd }, data.commandOptions || {});
        let route = {
            name,
            type: data.type,
            method: c(function* () {
                try {
                    if (data.command) {
                        log(`Running command: ${data.command}`);
{'fraction_non_alphanumeric': 0.1505590442640527, 'fraction_numerical': 0.006432838106907643, 'mean_word_length': 1.5933280381254964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '1599693', 'n_tokens_mistral': 2001, 'n_tokens_neox': 1963, 'n_words': 455}