<commit_before>import { extend } from 'marbles/utils';
import Dispatcher from 'dashboard/dispatcher';
import Config from 'dashboard/config';

var createTaffyJob = function (taffyReleaseId, appID, appName, meta) {
	var client = Config.client;

	var cloneURL = meta.clone_url;
	var ref = meta.ref;
	var sha = meta.sha;
	return client.createTaffyJob({
		release: taffyReleaseId,
		release_env: true,
		cmd: [appName, cloneURL, ref, sha],
		meta: extend({}, meta, {
			app: appID,
		})
	});
};

var deployFromGithub = function (meta, appData) {
	var client = Config.client;

	var appId, appName;
	var databaseEnv = {};

	function createDatabase () {
		return client.createAppDatabase({ apps: [appId] }).then(function (args) {
			var res = args[0];
			databaseEnv = res.env;
			return createRelease();
		});
	}

	function createRelease () {
		return client.createRelease({
			env: extend({}, appData.env, databaseEnv)
		}).then(function (args) {
			var res = args[0];
			return createAppRelease(res.id);
		});
	}

	function createAppRelease (releaseId) {
		return client.createAppRelease(appId, {
			id: releaseId
		}).then(function () {
			return getTaffyRelease();
		});
	}

	function getTaffyRelease () {
		return client.getTaffyRelease().then(function (args) {
			var res = args[0];
			return createTaffyJob(res.id, appId, appName, meta);
		});
	}

	function updateOrCreateApp() {
		if (appData.hasOwnProperty('id')) {
			return client.getApp(appData.id).then(function (args) {
				var res = args[0];
				meta = extend({}, res.meta, meta);
				return client.updateApp(res.id, {
					meta: meta
				});
			});
		} else {
			return client.createApp({
				name: appData.name,
				meta: meta
			});
		}
	}

	return updateOrCreateApp().then(function (args) {
		var res = args[0];
		appId = res.id;
		appName = res.name;
		if (appData.dbRequested) {
			return createDatabase();
		} else {
			return createRelease();
		}
	});
};

var deployFromGithubCommit = function (repo, branchName, sha, appData) {
	var meta = {
		type: 'github',
		user_login: repo.ownerLogin,
		repo_name: repo.name,
		ref: branchName,
		sha: sha,<commit_msg>dashboard: Copy env from previous release when deploying commit<commit_after>import { extend } from 'marbles/utils';
import Dispatcher from 'dashboard/dispatcher';
import Config from 'dashboard/config';

var createTaffyJob = function (taffyReleaseId, appID, appName, meta) {
	var client = Config.client;

	var cloneURL = meta.clone_url;
	var ref = meta.ref;
	var sha = meta.sha;
	return client.createTaffyJob({
		release: taffyReleaseId,
		release_env: true,
		cmd: [appName, cloneURL, ref, sha],
		meta: extend({}, meta, {
			app: appID,
		})
	});
};

var deployFromGithub = function (meta, appData) {
	var client = Config.client;

	var appId, appName;
	var databaseEnv = {};

	function createDatabase () {
		return client.createAppDatabase({ apps: [appId] }).then(function (args) {
			var res = args[0];
			databaseEnv = res.env;
			return createRelease();
		});
	}

	function createRelease () {
		return client.createRelease({
			env: extend({}, appData.env, databaseEnv)
		}).then(function (args) {
			var res = args[0];
			return createAppRelease(res.id);
		});
	}

	function createAppRelease (releaseId) {
		return client.createAppRelease(appId, {
			id: releaseId
		}).then(function () {
			return getTaffyRelease();
		});
	}

	function getTaffyRelease () {
		return client.getTaffyRelease().then(function (args) {
			var res = args[0];
			return createTaffyJob(res.id, appId, appName, meta);
		});
	}

	function updateApp() {
		return client.getApp(appData.id).then(function (args) {
			var res = args[0];
			meta = extend({}, res.meta, meta);
			return client.updateApp(res.id, {
				meta: meta
			});
		});
	}

	function updateOrCreateApp() {
		if (appData.hasOwnProperty('id')) {
			return client.getAppRelease(appData.id).then(function (args) {
				appData.env = args[0].env;
			}).then(updateApp);
		} else {
			return client.createApp({
				name: appData.name,
				meta: meta
			});
		}
	}

	return updateOrCreateApp().then(function (args) {
		var res = args[0];
		appId = res.id;
		appName = res.name;
		if (appData.dbRequested) {
			return createDatabase();
		} else {
			return createRelease();
		}
	});
};

var deployFromGithubCommit = function (repo, branchName, sha, appData) {
	var meta = {
		type: 'github',
		user_login: repo.ownerLogin,
		repo_name: repo.name,
		ref: branchName,
		sha: sha,
{'fraction_non_alphanumeric': 0.1446271682811444, 'fraction_numerical': 0.0024780355936021626, 'mean_word_length': 3.76906552094522, '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': '7147591', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1528, 'n_words': 363}