All files / modules/70-pipeline/components/Notifications/Steps PipelineEvents.tsx

72.58% Statements 45/62
54.67% Branches 41/75
56.25% Functions 9/16
77.59% Lines 45/58

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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254              25x                   25x 25x 25x 25x 25x 25x   25x   25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x     25x                                                                                                       1x 1x 1x   1x       1x 2x 2x 2x 2x           1x                           1x           1x             1x               1x               1x                   1x           1x   2x     2x 2x   2x     1x       3x                       30x                                                                                                                 25x  
/*
 * 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 {
  Button,
  ButtonVariation,
  Formik,
  FormInput,
  Layout,
  MultiSelectOption,
  StepProps,
  Text
} from '@wings-software/uicore'
import React from 'react'
import { Color, Intent } from '@harness/design-system'
import * as Yup from 'yup'
import { Form } from 'formik'
import { startCase, isEmpty } from 'lodash-es'
import { useStrings } from 'framework/strings'
import type { NotificationRules, PipelineEvent } from 'services/pipeline-ng'
import css from '../useNotificationModal.module.scss'
 
export enum PipelineEventType {
  ALL_EVENTS = 'AllEvents',
  PipelineSuccess = 'PipelineSuccess',
  PipelineFailed = 'PipelineFailed',
  PipelinePaused = 'PipelinePaused',
  StageSuccess = 'StageSuccess',
  StageFailed = 'StageFailed',
  StageStart = 'StageStart',
  StepFailed = 'StepFailed',
  PipelineEnd = 'PipelineEnd',
  PipelineStart = 'PipelineStart'
}
 
const pipelineEventItems = [
  {
    label: startCase(PipelineEventType.ALL_EVENTS),
    value: PipelineEventType.ALL_EVENTS
  },
  {
    label: startCase(PipelineEventType.PipelineStart),
    value: PipelineEventType.PipelineStart
  },
  {
    label: startCase(PipelineEventType.PipelineEnd),
    value: PipelineEventType.PipelineEnd
  },
 
  {
    label: startCase(PipelineEventType.PipelineSuccess),
    value: PipelineEventType.PipelineSuccess
  },
  {
    label: startCase(PipelineEventType.PipelineFailed),
    value: PipelineEventType.PipelineFailed
  },
  {
    label: startCase(PipelineEventType.PipelinePaused),
    value: PipelineEventType.PipelinePaused
  },
  {
    label: startCase(PipelineEventType.StageFailed),
    value: PipelineEventType.StageFailed
  },
  {
    label: startCase(PipelineEventType.StageSuccess),
    value: PipelineEventType.StageSuccess
  },
  {
    label: startCase(PipelineEventType.StageStart),
    value: PipelineEventType.StageStart
  },
  {
    label: startCase(PipelineEventType.StepFailed),
    value: PipelineEventType.StepFailed
  }
]
 
interface PipelineEventsFormData {
  types: { [key: string]: any }
  [key: string]: any
}
 
type PipelineEventsProps = StepProps<NotificationRules> & { stagesOptions?: MultiSelectOption[] }
 
function PipelineEvents({ nextStep, prevStepData, stagesOptions }: PipelineEventsProps): React.ReactElement {
  const { getString } = useStrings()
  const initialValues: PipelineEventsFormData = { types: {} }
  const types: Required<PipelineEventsFormData>['types'] = {}
 
  const getStageOption = (stageId: string) => {
    return stagesOptions?.find(item => item.value === stageId)
  }
 
  prevStepData?.pipelineEvents?.map(event => {
    const type = event.type
    Eif (type) {
      types[type] = true
      Iif (event.forStages?.length) {
        initialValues[type] = event.forStages.map(stageId => getStageOption(stageId)).filter(item => !!item)
      }
    }
  })
 
  return (
    <Layout.Vertical spacing="xxlarge" padding="small">
      <Text font="medium" color={Color.BLACK}>
        {getString('notifications.pipelineEvents')}
      </Text>
      <Formik<PipelineEventsFormData>
        initialValues={{ ...initialValues, types }}
        formName="pipelineEvents"
        validationSchema={Yup.object()
          .shape({
            types: Yup.object().required()
          })
          .test({
            test: function (val) {
              Iif (isEmpty(val?.types)) {
                return this.createError({
                  path: 'types',
                  message: getString('notifications.eventRequired')
                })
              }
              Iif (Object.keys(val.types).length === 1 && val.types[PipelineEventType.ALL_EVENTS] === false) {
                return this.createError({
                  path: 'types',
                  message: getString('notifications.eventRequired')
                })
              }
 
              Iif (
                val.types[PipelineEventType.StageStart] &&
                (!val[PipelineEventType.StageStart] || !val[PipelineEventType.StageStart].length)
              ) {
                return this.createError({
                  path: PipelineEventType.StageStart,
                  message: getString('notifications.stageRequired')
                })
              } else Iif (
                val.types[PipelineEventType.StageFailed] &&
                (!val[PipelineEventType.StageFailed] || !val[PipelineEventType.StageFailed].length)
              ) {
                return this.createError({
                  path: PipelineEventType.StageFailed,
                  message: getString('notifications.stageRequired')
                })
              } else Iif (
                val.types[PipelineEventType.StageSuccess] &&
                (!val[PipelineEventType.StageSuccess] || !val[PipelineEventType.StageSuccess].length)
              ) {
                return this.createError({
                  path: PipelineEventType.StageSuccess,
                  message: getString('notifications.stageRequired')
                })
              }
 
              return true
            }
          })
          .required()}
        validateOnChange={false}
        onSubmit={values => {
          const pipelineEvents: PipelineEvent[] = Object.keys(values.types)
            .filter(function (k) {
              return values.types[k]
            })
            .map(value => {
              const dataToSubmit: PipelineEvent = { type: value as PipelineEventType }
              Iif (values[value]?.length)
                dataToSubmit['forStages'] = values[value].map((item: { value: string }) => item.value)
              return dataToSubmit
            })
 
          nextStep?.({ ...prevStepData, pipelineEvents })
        }}
      >
        {formikProps => {
          return (
            <Form>
              <Layout.Vertical spacing="medium" className={css.formContent}>
                <Text margin={{ bottom: !isEmpty(formikProps.errors) ? 'small' : 'large' }}>
                  {getString('notifications.selectPipelineEvents')}
                </Text>
                {!isEmpty(formikProps.errors) && (
                  <Text intent={Intent.DANGER} margin={{ top: 'none', bottom: 'small' }}>
                    {getString('notifications.eventRequired')}
                  </Text>
                )}
                {pipelineEventItems.map(event => {
                  return (
                    <Layout.Vertical key={event.label}>
                      <Layout.Horizontal margin={{ bottom: 'small' }} flex>
                        <FormInput.CheckBox
                          className={formikProps.values.types[event.value] ? 'checked' : 'unchecked'}
                          name={`types.${event.value}`}
                          checked={formikProps.values.types[event.label]}
                          label={event.label}
                          padding={{ left: 'xxxlarge' }}
                          onChange={e => {
                            if (e.currentTarget.checked) {
                              if (event.value === PipelineEventType.ALL_EVENTS) {
                                Object.keys(formikProps.values.types).forEach(item => {
                                  item !== PipelineEventType.ALL_EVENTS
                                    ? (formikProps.values.types[item] = false)
                                    : (formikProps.values.types[item] = true)
                                })
                              } else {
                                formikProps.values.types[PipelineEventType.ALL_EVENTS] = false
                              }
                            }
                          }}
                        />
                        {(event.value === PipelineEventType.StageSuccess ||
                          event.value === PipelineEventType.StageFailed ||
                          event.value === PipelineEventType.StageStart) && (
                          <FormInput.MultiSelect
                            disabled={!formikProps.values.types[event.value]}
                            className={css.stagesMultiSelect}
                            items={stagesOptions || []}
                            name={event.value}
                            label={''}
                            multiSelectProps={{
                              placeholder: getString('notifications.selectStagesPlaceholder'),
                              allowCreatingNewItems: false
                            }}
                          />
                        )}
                      </Layout.Horizontal>
                    </Layout.Vertical>
                  )
                })}
              </Layout.Vertical>
              <Button
                type="submit"
                variation={ButtonVariation.PRIMARY}
                rightIcon="chevron-right"
                text={getString('continue')}
              />
            </Form>
          )
        }}
      </Formik>
    </Layout.Vertical>
  )
}
 
export default PipelineEvents