<commit_before>'use strict';

/*
 * Make sure you create a config.js!
 */

try {
	var config = require(__dirname + '/config.js');
} catch(e) {
	throw new Error('***PLEASE CREATE A CONFIG.JS ON YOUR LOCAL SYSTEM. REFER TO CONFIG.EXAMPLE.JS***');
}
var port = process.env.PORT || config.port;

var allRepos = config.repositories;

/*
 * Require modules
 */

var fs 			= require('fs');
var exec 		= require('child_process').exec;
var express 	= require('express');
var bodyParser 	= require('body-parser');
var app 		= express();

/*
 * Middleware
 */

// Read POST Request Variables
app.use(bodyParser.json());

/*
 * Routes
 */

app.post('/', function(req, res) {
	console.log(req.body);
	// gets the repository name from the webhook
	var activeRepo = req.body.repository.full_name;

	try {
		var splitRef = req.body.ref.split("/");
		var activeBranch = splitRef[splitRef.length - 1];
	} catch(e) {
		var activeBranch = "master";
	}

	var owner = req.body.repository.owner.name;
	var repoName = req.body.repository.name;


	var customScriptPath = __dirname + "/../deploy/" + owner + "/" + repoName + "/" + activeBranch + ".sh";

	fs.stat(customScriptPath, function(statErr, stat) {
		if(statErr == null) {
			// there's a custom deploy script? fantastic. let's execute it. (with the webhook request as $WEBHOOK)
			exec("WEBHOOK=" + JSON.stringify(req.body) + "; " + customScriptPath, function(execErr, stdout, stderr) {
				if(execErr) {
					console.log("Error when executing custom deploy script for repository " + activeRepo + " on branch " + activeBranch + "!");
					res.sendStatus(500);
				} else {
					console.log("stdout: " + stdout);
					console.log("stderr: " + stderr);
					console.log("Successfully executed custom deploy script for repository " + activeRepo + " on branch " + activeBranch + "!");
					res.sendStatus(200);
				}
			});
		} else if (statErr.code == 'ENOENT') {
			// custom deploy script doesn't exist? that's fine, just pull.
			
			// grabs the path from the config.js
			var workingPath = allRepos[activeRepo][activeBranch];
			require('simple-git')(workingPath).pull(function(pullErr) {
				if(pullErr) {
				    console.log("Error when pulling from repository " + activeRepo + " on branch " + activeBranch + "!");
				    res.sendStatus(500);
				} else {
				    console.log("Successfully pulled from repository " + activeRepo + " on branch " + activeBranch + "!");<commit_msg>make the deploy script run in the repo<commit_after>'use strict';

/*
 * Make sure you create a config.js!
 */

try {
	var config = require(__dirname + '/config.js');
} catch(e) {
	throw new Error('***PLEASE CREATE A CONFIG.JS ON YOUR LOCAL SYSTEM. REFER TO CONFIG.EXAMPLE.JS***');
}
var port = process.env.PORT || config.port;

var allRepos = config.repositories;

/*
 * Require modules
 */

var fs 			= require('fs');
var exec 		= require('child_process').exec;
var express 	= require('express');
var bodyParser 	= require('body-parser');
var app 		= express();

/*
 * Middleware
 */

// Read POST Request Variables
app.use(bodyParser.json());

/*
 * Routes
 */

app.post('/', function(req, res) {
	console.log(req.body);
	// gets the repository name from the webhook
	var activeRepo = req.body.repository.full_name;

	try {
		var splitRef = req.body.ref.split("/");
		var activeBranch = splitRef[splitRef.length - 1];
	} catch(e) {
		var activeBranch = "master";
	}

	var owner = req.body.repository.owner.name;
	var repoName = req.body.repository.name;

	var workingPath = allRepos[activeRepo][activeBranch];

	var customScriptPath = __dirname + "/../deploy/" + owner + "/" + repoName + "/" + activeBranch + ".sh";

	fs.stat(customScriptPath, function(statErr, stat) {
		if(statErr == null) {
			// there's a custom deploy script? fantastic. let's execute it. (with the webhook request as $WEBHOOK, and in the repo directory)
			exec("(WEBHOOK=" + JSON.stringify(req.body) + "; cd " + workingPath + "; " + customScriptPath + ")", function(execErr, stdout, stderr) {
				if(execErr) {
					console.log("Error when executing custom deploy script for repository " + activeRepo + " on branch " + activeBranch + "!");
					res.sendStatus(500);
				} else {
					console.log("stdout: " + stdout);
					console.log("stderr: " + stderr);
					console.log("Successfully executed custom deploy script for repository " + activeRepo + " on branch " + activeBranch + "!");
					res.sendStatus(200);
				}
			});
		} else if (statErr.code == 'ENOENT') {
			// custom deploy script doesn't exist? that's fine, just pull.
			
			// grabs the path from the config.js
			require('simple-git')(workingPath).pull(function(pullErr) {
				if(pullErr) {
				    console.log("Error when pulling from repository " + activeRepo + " on branch " + activeBranch + "!");
				    res.sendStatus(500);
				} else {
				    console.log("Successfully pulled from repository " + activeRepo + " on branch " + activeBranch + "!");
{'fraction_non_alphanumeric': 0.15367392631793203, 'fraction_numerical': 0.004070832485243233, 'mean_word_length': 3.4713375796178343, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '858940', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1551, 'n_words': 410}