All files / modules/70-pipeline/components/PipelineSteps/AdvancedSteps/FailureStrategyPanel utils.ts

100% Statements 17/17
92.31% Branches 12/13
100% Functions 5/5
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89                  37x 37x                                                     37x 105x       37x 114x   114x   71x                             37x           50x   50x   46x   6x       46x   5x         37x 152x    
/*
 * Copyright 2022 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import type { SetStateAction } from 'react'
import type { FormikErrors, FormikProps } from 'formik'
import { isEmpty } from 'lodash-es'
import { Intent } from '@blueprintjs/core'
 
import type {
  RetryFailureActionConfig,
  IgnoreFailureActionConfig,
  ManualInterventionFailureActionConfig,
  AbortFailureActionConfig,
  StepGroupFailureActionConfig,
  MarkAsSuccessFailureActionConfig,
  StageRollbackFailureActionConfig,
  FailureStrategyConfig,
  OnFailureConfig
} from 'services/cd-ng'
 
export type AllActions =
  | RetryFailureActionConfig
  | IgnoreFailureActionConfig
  | ManualInterventionFailureActionConfig
  | AbortFailureActionConfig
  | StepGroupFailureActionConfig
  | MarkAsSuccessFailureActionConfig
  | StageRollbackFailureActionConfig
 
export interface AllFailureStrategyConfig extends FailureStrategyConfig {
  onFailure: OnFailureConfig & { action: AllActions }
}
 
export function hasItems<T>(data?: T[]): boolean {
  return Array.isArray(data) && data.length > 0
}
 
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function findTabWithErrors(errors?: FormikErrors<any>): number {
  const fsErrors = errors?.failureStrategies
 
  return Array.isArray(fsErrors)
    ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
      fsErrors.findIndex((err: any) => !isEmpty(err))
    : -1
}
 
export interface FormState {
  failureStrategies?: AllFailureStrategyConfig[]
}
 
export interface HandleChangeInStrategiesProps {
  formValues: FormState
  selectedStrategyNum: number
  setSelectedStrategyNum: (value: SetStateAction<number>) => void
  setFormikState: FormikProps<FormState>['setFormikState']
}
 
export function handleChangeInStrategies({
  formValues,
  selectedStrategyNum,
  setSelectedStrategyNum,
  setFormikState
}: HandleChangeInStrategiesProps): void {
  const fs = formValues.failureStrategies
  /* istanbul ignore else */
  if (Array.isArray(fs)) {
    /* istanbul ignore else */
    if (selectedStrategyNum >= fs.length) {
      // select the new last tab, if the last tab was deleted
      setSelectedStrategyNum(Math.max(0, fs.length - 1))
    }
 
    /* istanbul ignore else */
    if (fs.length === 0) {
      // reset errors when all the tabs are deleted
      setFormikState({ errors: {}, submitCount: 0 })
    }
  }
}
 
export function getTabIntent(i: number, selectedStrategyNum: number): Intent {
  return i === selectedStrategyNum ? Intent.PRIMARY : Intent.NONE
}