<commit_before><?php
/* Webhook to update a repo for each push on GitHub. */

date_default_timezone_set('Europe/Paris');

// app variables
$app_root = realpath(__DIR__ . '/../');

// git variables
$branch = 'master';
$header = 'HTTP_X_HUB_SIGNATURE';
$secret = parse_ini_file($app_root . '/app/config/config.ini')['github_key'];

// Logging function to output content to /github_log.txt
function logHookResult($message, $success = false)
{
    $log_headers = "$message\n";
    if (! $success) {
        foreach ($_SERVER as $header => $value) {
            $log_headers .= "$header: $value \n";
        }
    }
    file_put_contents(__DIR__ . '/github_log.txt', $log_headers);
}

if (isset($_SERVER[$header])) {
    $validation = hash_hmac(
        'sha1',
        file_get_contents("php://input"),
        $secret
    );

    if ($validation == explode('=', $_SERVER[$header])[1]) {
        // Pull latest changes
        exec("git checkout $branch ; git pull origin $branch");

        // Generate static site
        chdir($app_root);
        exec('php ./sculpin.phar generate --env=prod > /dev/null 2>&1');

        logHookResult('Last update: ' . date('d-m-Y H:i:s'), true);
    } else {
        logHookResult('Invalid GitHub secret');
    }
} else {
    logHookResult("{$header} header missing, define a secret key for your project in GitHub");
}<commit_msg>suggesting new github hook. only get composer and composer deps when there are ones. auto get sculpin.phar to generate the static website<commit_after><?php
/* Webhook to update a repo for each push on GitHub. */

date_default_timezone_set('Europe/Paris');

// app variables
$app_root = realpath(__DIR__ . '/../');
$composer = $app_root . '/composer.phar';
$sculpin = $app_root . '/sculpin.phar';

// git variables
$branch = 'master';
$header = 'HTTP_X_HUB_SIGNATURE';
$secret = parse_ini_file($app_root . '/app/config/config.ini')['github_key'];

// Logging function to output content to /github_log.txt
function logHookResult($message, $success = false)
{
    $log_headers = "$message\n";
    if (! $success) {
        foreach ($_SERVER as $header => $value) {
            $log_headers .= "$header: $value \n";
        }
    }
    file_put_contents(__DIR__ . '/github_log.txt', $log_headers);
}

// CHECK: Download composer in the app root if it is not already there
if (! file_exists($composer) and file_exists($app_root.'/composer.json')) {
    file_put_contents(
        $composer,
        file_get_contents('https://getcomposer.org/composer.phar')
    );
}

#Get Sculpin
if (! file_exists($sculpin) and file_exists($app_root.'/sculpin.json')) {
    file_put_contents(
        $sculpin,
        file_get_contents('https://download.sculpin.io/sculpin.phar')
    );
}

if (isset($_SERVER[$header])) {
    $validation = hash_hmac(
        'sha1',
        file_get_contents("php://input"),
        $secret
    );

    if ($validation == explode('=', $_SERVER[$header])[1]) {
        // Pull latest changes
        exec("git checkout $branch ; git pull origin $branch");

        // Install or update dependencies
        if (file_exists($composer) and file_exists($app_root.'/composer.json')) {
            chdir($app_root);

            // www-data does not have a HOME or COMPOSER_HOME, create one
            if (! is_dir("{$app_root}/cache/.composer")) {
                mkdir("{$app_root}/cache/.composer");
            }

            putenv("COMPOSER_HOME={$app_root}/cache/.composer");

            if (file_exists($app_root . '/vendor')) {
                exec("php {$composer} update > /dev/null 2>&1");
            } else {
                exec("php {$composer} install > /dev/null 2>&1");
            }
        }

        // Generate static site
        if (file_exists($sculpin) and file_exists($app_root.'/sculpin.json')) {
            chdir($app_root);

            exec("php {$sculpin} generate --env=prod > /dev/null 2>&1");
        }

        logHookResult('Last update: ' . date('d-m-Y H:i:s'), true);
    } else {
        logHookResult('Invalid GitHub secret');
    }
} else {
    logHookResult("{$header} header missing, define a secret key for your project in GitHub");
}
{'fraction_non_alphanumeric': 0.1575655520808275, 'fraction_numerical': 0.0028866971373586723, 'mean_word_length': 2.72914798206278, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, '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': '6850370', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1295, 'n_words': 307}