<commit_before>(function() {

"use strict";

angular.module("BitriseWorkflowEditor").controller("EnvVarsController", function($scope, $rootScope, $q, requestService, stringService, appService, Progress, Variable) {

	var viewModel = this;

	var mode;
	var defaultEnvVarConfig = {
		opts: {
			is_expand: true
		}
	};
	var envVarsWatchers;

	viewModel.secretConfigs;
	viewModel.loadEnvVarsProgress = new Progress();
	viewModel.envVarsBySource;

	$scope.$on("$destroy", $rootScope.$on("MainController::appConfigLoaded", function() {
		switch (mode) {
			case "secrets":
				return;
			case "env-vars":
				configureWithAppConfig();
		}
	}));

	$scope.$on("$destroy", $rootScope.$on("MainController::discardChanges", function() {
		switch (mode) {
			case "secrets":
				_.first(viewModel.envVarsBySource).envVars = angular.copy(_.first(viewModel.envVarsBySource).savedEnvVars);

				break;
			case "env-vars":
				configureWithAppConfig();

				break;
		}
	}));

	viewModel.configureWithMode = function(_mode) {
		mode = _mode;

		switch (mode) {
			case "secrets":
				loadSecrets();

				break;
		}

		$rootScope.$emit("MainController::menuContentIsLoadedAndReady");
	};

	function loadSecrets() {
		viewModel.loadEnvVarsProgress.start("<%= data[:strings][:env_vars][:load_progress][:in_progress] %>");

		var canceler = $q.defer();

		$scope.$on("$destroy", function() {
			canceler.resolve();
		});

		requestService.getSecrets({
			timeout: canceler.promise
		}).then(function(secretConfigs) {
			viewModel.secretConfigs = secretConfigs;

			viewModel.envVarsBySource = [{
				type: "secrets",
				notification: "<%= data[:strings][:env_vars][:secrets][:notification] %>",
				envVars: _.map(secretConfigs, function(aSecretConfig) {
					return new Variable(aSecretConfig, defaultEnvVarConfig);
				})
			}];

			_.first(viewModel.envVarsBySource).savedEnvVars = angular.copy(_.first(viewModel.envVarsBySource).envVars);

			configureEnvVarsWatchers();

			viewModel.loadEnvVarsProgress.success();
		}, function(error) {
			viewModel.loadEnvVarsProgress.error(error);
		});
	}

	function configureWithAppConfig() {
		viewModel.envVarsBySource = [];

		if (appService.appConfig.app.envs) {
			viewModel.envVarsBySource.push({
				type: "app",
				warning: "<%= data[:strings][:env_vars][:app][:warning] %>",
				envVars: _.map(appService.appConfig.app.envs, function(anAppEnvVarConfig) {
					return new Variable(anAppEnvVarConfig, defaultEnvVarConfig);
				})
			});
		}

		_.each(appService.appConfig.workflows, function(aWorkflowConfig, aWorkflowID) {
			viewModel.envVarsBySource.push({
				type: "workflow",
				workflowID: aWorkflowID,
				notification: stringService.stringReplacedWithParameters("<%= data[:strings][:env_vars][:workflow][:notification] %>", {
					workflow_id: aWorkflowID
				}),
				envVars: _.map(aWorkflowConfig.envs, function(anEnvVarConfig) {
					return new Variable(anEnvVarConfig, defaultEnvVarConfig);
				})
			});
		});

		configureEnvVarsWatchers();
	}

	function configureEnvVarsWatchers() {
		if (envVarsWatchers) {
			_.each(envVarsWatchers, function(anEnvVarsWatcher) {
				anEnvVarsWatcher();
			});
		}

		envVarsWatchers = _.map(viewModel.envVarsBySource, function(envVarsOfSource) {
			return $scope.$watch(function() {
				return envVarsOfSource;
			}, function(envVarsOfSource) {
				switch (envVarsOfSource.type) {
					case "secrets":
						if (!viewModel.secretConfigs) {<commit_msg>app env vars - bug fixed when "app" or "envs" is not specified in app config<commit_after>(function() {

"use strict";

angular.module("BitriseWorkflowEditor").controller("EnvVarsController", function($scope, $rootScope, $q, requestService, stringService, appService, Progress, Variable) {

	var viewModel = this;

	var mode;
	var defaultEnvVarConfig = {
		opts: {
			is_expand: true
		}
	};
	var envVarsWatchers;

	viewModel.secretConfigs;
	viewModel.loadEnvVarsProgress = new Progress();
	viewModel.envVarsBySource;

	$scope.$on("$destroy", $rootScope.$on("MainController::appConfigLoaded", function() {
		switch (mode) {
			case "secrets":
				return;
			case "env-vars":
				configureWithAppConfig();
		}
	}));

	$scope.$on("$destroy", $rootScope.$on("MainController::discardChanges", function() {
		switch (mode) {
			case "secrets":
				_.first(viewModel.envVarsBySource).envVars = angular.copy(_.first(viewModel.envVarsBySource).savedEnvVars);

				break;
			case "env-vars":
				configureWithAppConfig();

				break;
		}
	}));

	viewModel.configureWithMode = function(_mode) {
		mode = _mode;

		switch (mode) {
			case "secrets":
				loadSecrets();

				break;
		}

		$rootScope.$emit("MainController::menuContentIsLoadedAndReady");
	};

	function loadSecrets() {
		viewModel.loadEnvVarsProgress.start("<%= data[:strings][:env_vars][:load_progress][:in_progress] %>");

		var canceler = $q.defer();

		$scope.$on("$destroy", function() {
			canceler.resolve();
		});

		requestService.getSecrets({
			timeout: canceler.promise
		}).then(function(secretConfigs) {
			viewModel.secretConfigs = secretConfigs;

			viewModel.envVarsBySource = [{
				type: "secrets",
				notification: "<%= data[:strings][:env_vars][:secrets][:notification] %>",
				envVars: _.map(secretConfigs, function(aSecretConfig) {
					return new Variable(aSecretConfig, defaultEnvVarConfig);
				})
			}];

			_.first(viewModel.envVarsBySource).savedEnvVars = angular.copy(_.first(viewModel.envVarsBySource).envVars);

			configureEnvVarsWatchers();

			viewModel.loadEnvVarsProgress.success();
		}, function(error) {
			viewModel.loadEnvVarsProgress.error(error);
		});
	}

	function configureWithAppConfig() {
		viewModel.envVarsBySource = [];

		viewModel.envVarsBySource.push({
			type: "app",
			warning: "<%= data[:strings][:env_vars][:app][:warning] %>",
			envVars: _.map(appService.appConfig.app && appService.appConfig.app.envs ? appService.appConfig.app.envs : [], function(anAppEnvVarConfig) {
				return new Variable(anAppEnvVarConfig, defaultEnvVarConfig);
			})
		});

		_.each(appService.appConfig.workflows, function(aWorkflowConfig, aWorkflowID) {
			viewModel.envVarsBySource.push({
				type: "workflow",
				workflowID: aWorkflowID,
				notification: stringService.stringReplacedWithParameters("<%= data[:strings][:env_vars][:workflow][:notification] %>", {
					workflow_id: aWorkflowID
				}),
				envVars: _.map(aWorkflowConfig.envs, function(anEnvVarConfig) {
					return new Variable(anEnvVarConfig, defaultEnvVarConfig);
				})
			});
		});

		configureEnvVarsWatchers();
	}

	function configureEnvVarsWatchers() {
		if (envVarsWatchers) {
			_.each(envVarsWatchers, function(anEnvVarsWatcher) {
				anEnvVarsWatcher();
			});
		}

		envVarsWatchers = _.map(viewModel.envVarsBySource, function(envVarsOfSource) {
			return $scope.$watch(function() {
				return envVarsOfSource;
			}, function(envVarsOfSource) {
				switch (envVarsOfSource.type) {
					case "secrets":
						if (!viewModel.secretConfigs) {
{'fraction_non_alphanumeric': 0.14867587795048934, 'fraction_numerical': 0.0, 'mean_word_length': 4.7715946843853825, 'pattern_counts': {'":': 12, '<': 11, '<?xml version=': 0, '>': 11, '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': '1886254', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2301, 'n_words': 330}