<commit_before>'use strict';

let angular = require('angular');

module.exports = angular.module('spinnaker.executionDetails.controller', [
  require('angular-ui-router'),
  require('../../pipeline/config/pipelineConfigProvider.js')
])
  .controller('executionDetails', function($scope, $stateParams, $state, pipelineConfig) {
    var controller = this;

    function getCurrentStage() {
      return parseInt($stateParams.stage);
    }

    function getCurrentStep() {
      return parseInt($stateParams.step);
    }

    controller.close = function() {
      $state.go('^');
    };

    controller.toggleDetails = function(index) {
      var newStepDetails = getCurrentStep() === index ? null : index;
      if (newStepDetails !== null) {
        $state.go('.', {
          stage: getCurrentStage(),
          step: newStepDetails,
        });
      }
    };

    controller.isStageCurrent = function(index) {
      return index === getCurrentStage();
    };

    controller.isStepCurrent = function(index) {
      return index === getCurrentStep();
    };

    controller.closeDetails = function(){
      $state.go('.', { step: null });
    };

    controller.getException = function(stage) {
      if (stage.context) {
        if (stage.context.exception && stage.context.exception.details.errors.length) {
          return stage.context.exception.details.errors.join(', ');
        }
        if (stage.context['kato.tasks'] && stage.context['kato.tasks'].length) {
          var lastTask = stage.context['kato.tasks'][stage.context['kato.tasks'].length-1];
          return lastTask.exception ? lastTask.exception.message : null;
        }
      }
      return null;
    };

    controller.getDetailsSourceUrl = function() {
      if ($stateParams.step !== undefined) {
        var stageSummary = $scope.execution.stageSummaries[getCurrentStage()];
        if (stageSummary) {
          var step = stageSummary.stages[getCurrentStep()] || stageSummary.masterStage;
          $scope.stageSummary = stageSummary;
          $scope.stage = step;
          var stageConfig = pipelineConfig.getStageConfig(step);
          if (stageConfig) {
            return stageConfig.executionDetailsUrl || null;
          }
        }
      }
      return null;
    };

    controller.getSummarySourceUrl = function() {
      if ($stateParams.stage !== undefined) {
        let currentStage = getCurrentStage();
        let stageSummary = $scope.execution.stageSummaries.length > currentStage ?
          $scope.execution.stageSummaries[currentStage] :
          null;
        if (stageSummary) {
          $scope.stageSummary = stageSummary;
          $scope.stage = stageSummary.stages[0];
          var stageConfig = pipelineConfig.getStageConfig(stageSummary);
          if (stageConfig && stageConfig.executionSummaryUrl) {
            return stageConfig.executionSummaryUrl;
          }
        }<commit_msg>protect against missing execution stages<commit_after>'use strict';

let angular = require('angular');

module.exports = angular.module('spinnaker.executionDetails.controller', [
  require('angular-ui-router'),
  require('../../pipeline/config/pipelineConfigProvider.js')
])
  .controller('executionDetails', function($scope, $stateParams, $state, pipelineConfig) {
    var controller = this;

    function getCurrentStage() {
      return parseInt($stateParams.stage);
    }

    function getCurrentStep() {
      return parseInt($stateParams.step);
    }

    controller.close = function() {
      $state.go('^');
    };

    controller.toggleDetails = function(index) {
      var newStepDetails = getCurrentStep() === index ? null : index;
      if (newStepDetails !== null) {
        $state.go('.', {
          stage: getCurrentStage(),
          step: newStepDetails,
        });
      }
    };

    controller.isStageCurrent = function(index) {
      return index === getCurrentStage();
    };

    controller.isStepCurrent = function(index) {
      return index === getCurrentStep();
    };

    controller.closeDetails = function(){
      $state.go('.', { step: null });
    };

    controller.getException = function(stage) {
      if (stage.context) {
        if (stage.context.exception && stage.context.exception.details.errors.length) {
          return stage.context.exception.details.errors.join(', ');
        }
        if (stage.context['kato.tasks'] && stage.context['kato.tasks'].length) {
          var lastTask = stage.context['kato.tasks'][stage.context['kato.tasks'].length-1];
          return lastTask.exception ? lastTask.exception.message : null;
        }
      }
      return null;
    };

    controller.getDetailsSourceUrl = function() {
      if ($stateParams.step !== undefined) {
        let stages = $scope.execution.stageSummaries || [];
        var stageSummary = stages[getCurrentStage()];
        if (stageSummary) {
          var step = stageSummary.stages[getCurrentStep()] || stageSummary.masterStage;
          $scope.stageSummary = stageSummary;
          $scope.stage = step;
          var stageConfig = pipelineConfig.getStageConfig(step);
          if (stageConfig) {
            return stageConfig.executionDetailsUrl || null;
          }
        }
      }
      return null;
    };

    controller.getSummarySourceUrl = function() {
      if ($stateParams.stage !== undefined) {
        let currentStage = getCurrentStage();
        let stages = $scope.execution.stageSummaries || [];
        let stageSummary = stages.length > currentStage ?
          stages[currentStage] :
          null;
        if (stageSummary) {
          $scope.stageSummary = stageSummary;
          $scope.stage = stageSummary.stages[0];
          var stageConfig = pipelineConfig.getStageConfig(stageSummary);
          if (stageConfig && stageConfig.executionSummaryUrl) {
            return stageConfig.executionSummaryUrl;
          }
        }
{'fraction_non_alphanumeric': 0.13053097345132744, 'fraction_numerical': 0.0006807351940095302, 'mean_word_length': 2.5944954128440365, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7152236', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1550, 'n_words': 282}