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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 105x 105x 105x 105x 105x 105x 152x 105x 105x 105x 2x 2x 2x 105x 6x 1x 6x 6x 6x 6x 91x 1x 1x 105x 50x 105x 57x 9x 9x 3x 105x 105x 105x 152x 29x 152x 152x 2x | /* * Copyright 2021 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 React from 'react' import { Button } from '@wings-software/uicore' import { FieldArray, FormikProps } from 'formik' import { v4 as uuid } from 'uuid' import { defaultTo, flatMap, get, isEmpty, uniq } from 'lodash-es' import cx from 'classnames' import { String, useStrings } from 'framework/strings' import { StageType } from '@pipeline/utils/stageHelpers' import type { StepMode as Modes } from '@pipeline/utils/stepUtils' import FailureTypeMultiSelect from './FailureTypeMultiSelect' import { allowedStrategiesAsPerStep, errorTypesForStages } from './StrategySelection/StrategyConfig' import StrategySelection from './StrategySelection/StrategySelection' import { findTabWithErrors, hasItems, handleChangeInStrategies, getTabIntent } from './utils' import type { AllFailureStrategyConfig } from './utils' import css from './FailureStrategyPanel.module.scss' /** * https://harness.atlassian.net/wiki/spaces/CDNG/pages/865403111/Failure+Strategy+-+CD+Next+Gen * * https://harness.atlassian.net/wiki/spaces/CDNG/pages/1046806671/NG+Failure+Strategies+Backend */ export interface FailureStrategyPanelProps { formikProps: FormikProps<{ failureStrategies?: AllFailureStrategyConfig[] }> mode: Modes isReadonly: boolean stageType?: StageType } export default function FailureStrategyPanel(props: FailureStrategyPanelProps): React.ReactElement { const { formikProps: { values: formValues, submitForm, errors, isSubmitting, setFormikState }, mode, isReadonly, stageType = StageType.DEPLOY } = props const [selectedStrategyNum, setSelectedStrategyNum] = React.useState(Math.max(findTabWithErrors(errors), 0)) const hasFailureStrategies = hasItems(formValues.failureStrategies) const { getString } = useStrings() const uids = React.useRef<string[]>([]) const filterTypes = uniq( flatMap(defaultTo(formValues.failureStrategies, []), e => defaultTo(e.onFailure?.errors, [])) ) const currentTabHasErrors = !isEmpty(get(errors, `failureStrategies[${selectedStrategyNum}]`)) const addedAllStratgies = filterTypes.length === errorTypesForStages[stageType].length const isAddBtnDisabled = addedAllStratgies || isReadonly || currentTabHasErrors async function handleTabChange(n: number): Promise<void> { await submitForm() // only change tab if current tab has no errors /* istanbul ignore else */ if (isEmpty(get(errors, `failureStrategies[${selectedStrategyNum}]`))) { setSelectedStrategyNum(n) } } function handleAdd(push: (obj: any) => void, strategies: AllFailureStrategyConfig[]) { return async (): Promise<void> => { /* istanbul ignore else */ if (strategies.length > 0) { await submitForm() } // only allow add if current tab has no errors /* istanbul ignore else */ if (isEmpty(get(errors, `failureStrategies[${selectedStrategyNum}]`))) { uids.current.push(uuid()) push({ onFailure: { errors: [], action: {} } }) setSelectedStrategyNum(strategies.length) } } } function handleRemove(remove: (obj: number) => void) { return (): void => { uids.current.splice(selectedStrategyNum, 1) remove(selectedStrategyNum) } } React.useEffect(() => { handleChangeInStrategies({ formValues, selectedStrategyNum, setFormikState, setSelectedStrategyNum }) // eslint-disable-next-line react-hooks/exhaustive-deps }, [formValues.failureStrategies, selectedStrategyNum]) // open errored tab React.useEffect(() => { if (isSubmitting) { const tabNum = findTabWithErrors(errors) if (tabNum > -1) { setSelectedStrategyNum(tabNum) } } }, [isSubmitting, errors]) return ( <div data-testid="failure-strategy-panel" className={css.main}> <String className={css.helpText} stringID="pipeline.failureStrategies.helpText" /> <div className={css.header}> <FieldArray name="failureStrategies"> {({ push, remove }) => { const strategies = defaultTo(formValues.failureStrategies, []) return ( <React.Fragment> <div className={css.tabs}> {hasFailureStrategies ? ( <ul className={css.stepList}> {strategies.map((_, i) => { // generated uuid if they are not present if (!uids.current[i]) { uids.current[i] = uuid() } const key = uids.current[i] return ( <li key={key} className={css.stepListItem}> <Button intent={getTabIntent(i, selectedStrategyNum)} data-selected={i === selectedStrategyNum} onClick={() => handleTabChange(i)} className={css.stepListBtn} data-testid={`failure-strategy-step-${i}`} > {i + 1} </Button> </li> ) })} </ul> ) : null} <Button intent="primary" minimal small icon="plus" iconProps={{ size: 12 }} data-testid="add-failure-strategy" onClick={handleAdd(push, strategies)} disabled={isAddBtnDisabled} tooltip={cx({ [getString('pipeline.failureStrategies.tabHasErrors')]: currentTabHasErrors, [getString('pipeline.failureStrategies.addedAllStrategies')]: !currentTabHasErrors && addedAllStratgies })} > <String stringID="add" /> </Button> </div> {hasFailureStrategies ? ( <Button icon="main-trash" minimal small disabled={isReadonly} onClick={handleRemove(remove)} iconProps={{ size: 12 }} data-testid="remove-failure-strategy" /> ) : null} </React.Fragment> ) }} </FieldArray> </div> {hasFailureStrategies ? ( <React.Fragment> <FailureTypeMultiSelect name={`failureStrategies[${selectedStrategyNum}].onFailure.errors`} label={getString('pipeline.failureStrategies.onFailureOfType')} filterTypes={filterTypes} stageType={stageType} disabled={isReadonly} /> <StrategySelection name={`failureStrategies[${selectedStrategyNum}].onFailure.action`} label={getString('pipeline.failureStrategies.performAction')} allowedStrategies={allowedStrategiesAsPerStep(stageType)[mode]} disabled={isReadonly} /> </React.Fragment> ) : null} </div> ) } |